xrpld
Loading...
Searching...
No Matches
xrpl::Serializer Class Reference

#include <Serializer.h>

Collaboration diagram for xrpl::Serializer:

Public Member Functions

 Serializer (int n=256)
 Serializer (void const *data, std::size_t size)
Slice slice () const noexcept
std::size_t size () const noexcept
void const * data () const noexcept
int add8 (unsigned char byteValue)
int add16 (std::uint16_t i)
template<typename T>
requires (std::is_same_v<std::make_unsigned_t<std::remove_cv_t<T>>, std::uint32_t>)
int add32 (T i)
int add32 (HashPrefix p)
template<typename T>
requires (std::is_same_v<std::make_unsigned_t<std::remove_cv_t<T>>, std::uint64_t>)
int add64 (T i)
template<typename Integer>
int addInteger (Integer)
template<std::size_t Bits, class Tag>
int addBitString (BaseUInt< Bits, Tag > const &v)
int addRaw (Blob const &vector)
int addRaw (Slice slice)
int addRaw (void const *ptr, int len)
int addRaw (Serializer const &s)
int addVL (Blob const &vector)
int addVL (Slice const &slice)
template<class Iter>
int addVL (Iter begin, Iter end, int len)
int addVL (void const *ptr, int len)
bool get8 (int &, int offset) const
template<typename Integer>
bool getInteger (Integer &number, int offset)
template<std::size_t Bits, typename Tag = void>
bool getBitString (BaseUInt< Bits, Tag > &data, int offset) const
int addFieldID (int type, int name)
int addFieldID (SerializedTypeID type, int name)
UInt256 getSHA512Half () const
Blob const & peekData () const
Blob getData () const
Blob & modData ()
int getDataLength () const
void const * getDataPtr () const
void * getDataPtr ()
int getLength () const
std::string getString () const
void erase ()
bool chop (int num)
Blob::iterator begin ()
Blob::iterator end ()
Blob::const_iterator begin () const
Blob::const_iterator end () const
void reserve (size_t n)
void resize (size_t n)
size_t capacity () const
bool operator== (Blob const &v) const
bool operator== (Serializer const &v) const
template<>
int addInteger (unsigned char i)
template<>
int addInteger (std::uint16_t i)
template<>
int addInteger (std::uint32_t i)
template<>
int addInteger (std::uint64_t i)
template<>
int addInteger (std::int32_t i)

Static Public Member Functions

static int decodeLengthLength (std::byte firstByte)
 Works out how long a header is, from its first byte.
static int decodeVLLength (std::byte firstByte)
 Reads the field length out of a 1 byte header.
static int decodeVLLength (std::byte firstByte, std::byte secondByte)
 Reads the field length out of a 2 byte header.
static int decodeVLLength (std::byte firstByte, std::byte secondByte, std::byte thirdByte)
 Reads the field length out of a 3 byte header.

Static Public Attributes

static constexpr int kMaxNumberOfBytesInHeader = 3
 A header is never longer than this.
static constexpr int kMinValueOfFirstByteFor1ByteHeader = 0
static constexpr int kMaxValueOfFirstByteFor1ByteHeader = 192
static constexpr int kMinValueOfFirstByteFor2ByteHeader
static constexpr int kMaxValueOfFirstByteFor2ByteHeader = 240
static constexpr int kMinValueOfFirstByteFor3ByteHeader
static constexpr int kMaxValueOfFirstByteFor3ByteHeader = 254
static constexpr int kNumberOfValuesInOneByte = 256
static constexpr int kNumberOfValuesInTwoBytes
static constexpr int kMinValueOfLengthFor1ByteHeader = kMinValueOfFirstByteFor1ByteHeader
 A 1 byte header holds the length in the byte itself, so both ends of this range are the same numbers as the first byte's own range.
static constexpr int kMaxValueOfLengthFor1ByteHeader = kMaxValueOfFirstByteFor1ByteHeader
static constexpr int kMinValueOfLengthFor2ByteHeader = kMaxValueOfLengthFor1ByteHeader + 1
static constexpr int kMaxValueOfLengthFor2ByteHeader
 48 values of the first byte mean a 2 byte header, and each of them covers 256 lengths.
static constexpr int kMinValueOfLengthFor3ByteHeader = kMaxValueOfLengthFor2ByteHeader + 1
static constexpr int kMaxRepresentableLength
 14 values of the first byte mean a 3 byte header, and each of them covers 65,536 lengths.
static constexpr int kMaxValueOfLengthFor3ByteHeader = 918744
 The largest length the encoder will write.

Private Member Functions

int addEncoded (int length)
 Appends the length header for a field of the given length.

Static Private Member Functions

static int encodeLengthLength (int length)
 Works out how many bytes the header needs for the given length.

Private Attributes

Blob data_

Detailed Description

Definition at line 22 of file Serializer.h.

Constructor & Destructor Documentation

◆ Serializer() [1/2]

xrpl::Serializer::Serializer ( int n = 256)
explicit

Definition at line 124 of file Serializer.h.

◆ Serializer() [2/2]

xrpl::Serializer::Serializer ( void const * data,
std::size_t size )

Definition at line 129 of file Serializer.h.

Member Function Documentation

◆ slice()

Slice xrpl::Serializer::slice ( ) const
nodiscardnoexcept

Definition at line 141 of file Serializer.h.

◆ size()

std::size_t xrpl::Serializer::size ( ) const
nodiscardnoexcept

Definition at line 147 of file Serializer.h.

◆ data()

void const * xrpl::Serializer::data ( ) const
nodiscardnoexcept

Definition at line 153 of file Serializer.h.

◆ add8()

int xrpl::Serializer::add8 ( unsigned char byteValue)

Definition at line 146 of file libxrpl/protocol/Serializer.cpp.

◆ add16()

int xrpl::Serializer::add16 ( std::uint16_t i)

Definition at line 25 of file libxrpl/protocol/Serializer.cpp.

◆ add32() [1/2]

template<typename T>
requires (std::is_same_v<std::make_unsigned_t<std::remove_cv_t<T>>, std::uint32_t>)
int xrpl::Serializer::add32 ( T i)

Definition at line 167 of file Serializer.h.

◆ add32() [2/2]

int xrpl::Serializer::add32 ( HashPrefix p)

Definition at line 34 of file libxrpl/protocol/Serializer.cpp.

◆ add64()

template<typename T>
requires (std::is_same_v<std::make_unsigned_t<std::remove_cv_t<T>>, std::uint64_t>)
int xrpl::Serializer::add64 ( T i)

Definition at line 183 of file Serializer.h.

◆ addInteger() [1/6]

template<typename Integer>
int xrpl::Serializer::addInteger ( Integer )

◆ addBitString()

template<std::size_t Bits, class Tag>
int xrpl::Serializer::addBitString ( BaseUInt< Bits, Tag > const & v)

Definition at line 202 of file Serializer.h.

◆ addRaw() [1/4]

int xrpl::Serializer::addRaw ( Blob const & vector)

Definition at line 75 of file libxrpl/protocol/Serializer.cpp.

◆ addRaw() [2/4]

int xrpl::Serializer::addRaw ( Slice slice)

Definition at line 83 of file libxrpl/protocol/Serializer.cpp.

◆ addRaw() [3/4]

int xrpl::Serializer::addRaw ( void const * ptr,
int len )

Definition at line 99 of file libxrpl/protocol/Serializer.cpp.

◆ addRaw() [4/4]

int xrpl::Serializer::addRaw ( Serializer const & s)

Definition at line 91 of file libxrpl/protocol/Serializer.cpp.

◆ addVL() [1/4]

int xrpl::Serializer::addVL ( Blob const & vector)

Definition at line 180 of file libxrpl/protocol/Serializer.cpp.

◆ addVL() [2/4]

int xrpl::Serializer::addVL ( Slice const & slice)

Definition at line 191 of file libxrpl/protocol/Serializer.cpp.

◆ addVL() [3/4]

template<class Iter>
int xrpl::Serializer::addVL ( Iter begin,
Iter end,
int len )

Definition at line 459 of file Serializer.h.

◆ addVL() [4/4]

int xrpl::Serializer::addVL ( void const * ptr,
int len )

Definition at line 200 of file libxrpl/protocol/Serializer.cpp.

◆ get8()

bool xrpl::Serializer::get8 ( int & byte,
int offset ) const

Definition at line 154 of file libxrpl/protocol/Serializer.cpp.

◆ getInteger()

template<typename Integer>
bool xrpl::Serializer::getInteger ( Integer & number,
int offset )

Definition at line 232 of file Serializer.h.

◆ getBitString()

template<std::size_t Bits, typename Tag = void>
bool xrpl::Serializer::getBitString ( BaseUInt< Bits, Tag > & data,
int offset ) const

Definition at line 251 of file Serializer.h.

◆ addFieldID() [1/2]

int xrpl::Serializer::addFieldID ( int type,
int name )

Definition at line 107 of file libxrpl/protocol/Serializer.cpp.

◆ addFieldID() [2/2]

int xrpl::Serializer::addFieldID ( SerializedTypeID type,
int name )

Definition at line 262 of file Serializer.h.

◆ getSHA512Half()

UInt256 xrpl::Serializer::getSHA512Half ( ) const
nodiscard

Definition at line 174 of file libxrpl/protocol/Serializer.cpp.

◆ peekData()

Blob const & xrpl::Serializer::peekData ( ) const
nodiscard

Definition at line 273 of file Serializer.h.

◆ getData()

Blob xrpl::Serializer::getData ( ) const
nodiscard

Definition at line 278 of file Serializer.h.

◆ modData()

Blob & xrpl::Serializer::modData ( )

Definition at line 283 of file Serializer.h.

◆ getDataLength()

int xrpl::Serializer::getDataLength ( ) const
nodiscard

Definition at line 289 of file Serializer.h.

◆ getDataPtr() [1/2]

void const * xrpl::Serializer::getDataPtr ( ) const
nodiscard

Definition at line 294 of file Serializer.h.

◆ getDataPtr() [2/2]

void * xrpl::Serializer::getDataPtr ( )

Definition at line 299 of file Serializer.h.

◆ getLength()

int xrpl::Serializer::getLength ( ) const
nodiscard

Definition at line 304 of file Serializer.h.

◆ getString()

std::string xrpl::Serializer::getString ( ) const
nodiscard

Definition at line 309 of file Serializer.h.

◆ erase()

void xrpl::Serializer::erase ( )

Definition at line 314 of file Serializer.h.

◆ chop()

bool xrpl::Serializer::chop ( int num)

Definition at line 164 of file libxrpl/protocol/Serializer.cpp.

◆ begin() [1/2]

Blob::iterator xrpl::Serializer::begin ( )

Definition at line 323 of file Serializer.h.

◆ end() [1/2]

Blob::iterator xrpl::Serializer::end ( )

Definition at line 328 of file Serializer.h.

◆ begin() [2/2]

Blob::const_iterator xrpl::Serializer::begin ( ) const
nodiscard

Definition at line 333 of file Serializer.h.

◆ end() [2/2]

Blob::const_iterator xrpl::Serializer::end ( ) const
nodiscard

Definition at line 338 of file Serializer.h.

◆ reserve()

void xrpl::Serializer::reserve ( size_t n)

Definition at line 343 of file Serializer.h.

◆ resize()

void xrpl::Serializer::resize ( size_t n)

Definition at line 348 of file Serializer.h.

◆ capacity()

size_t xrpl::Serializer::capacity ( ) const
nodiscard

Definition at line 353 of file Serializer.h.

◆ operator==() [1/2]

bool xrpl::Serializer::operator== ( Blob const & v) const

Definition at line 359 of file Serializer.h.

◆ operator==() [2/2]

bool xrpl::Serializer::operator== ( Serializer const & v) const

Definition at line 364 of file Serializer.h.

◆ decodeLengthLength()

int xrpl::Serializer::decodeLengthLength ( std::byte firstByte)
static

Works out how long a header is, from its first byte.

Each overload of decodeVLLength below reads one header length, so call this first to learn which of them to call.

Parameters
firstByteFirst byte of the header, as read from the stream.
Returns
How many bytes the whole header takes, counting firstByte: 1, 2 or 3.
Exceptions
std::overflow_errorif firstByte is the one value that starts no header.

Definition at line 276 of file libxrpl/protocol/Serializer.cpp.

◆ decodeVLLength() [1/3]

int xrpl::Serializer::decodeVLLength ( std::byte firstByte)
static

Reads the field length out of a 1 byte header.

Parameters
firstByteThe single header byte, which is the length itself.
Returns
Field length in bytes, from kMinValueOfLengthFor1ByteHeader to kMaxValueOfLengthFor1ByteHeader.
Exceptions
std::overflow_errorif firstByte is big enough to mean a longer header, in which case it is not a length by itself.

Definition at line 293 of file libxrpl/protocol/Serializer.cpp.

◆ decodeVLLength() [2/3]

int xrpl::Serializer::decodeVLLength ( std::byte firstByte,
std::byte secondByte )
static

Reads the field length out of a 2 byte header.

Parameters
firstByteFirst header byte. Its value means a 2 byte header, and how far it sits into that range gives the top part of the length.
secondByteSecond header byte, holding the rest of the length.
Returns
Field length in bytes, from kMinValueOfLengthFor2ByteHeader to kMaxValueOfLengthFor2ByteHeader.
Exceptions
std::overflow_errorif firstByte is outside the range that means a 2 byte header.

Definition at line 305 of file libxrpl/protocol/Serializer.cpp.

◆ decodeVLLength() [3/3]

int xrpl::Serializer::decodeVLLength ( std::byte firstByte,
std::byte secondByte,
std::byte thirdByte )
static

Reads the field length out of a 3 byte header.

Parameters
firstByteFirst header byte. Its value means a 3 byte header, and how far it sits into that range gives the top part of the length.
secondByteSecond header byte, holding the middle part of the length.
thirdByteThird header byte, holding the low part.
Returns
Field length in bytes, from kMinValueOfLengthFor3ByteHeader to kMaxValueOfLengthFor3ByteHeader.
Exceptions
std::overflow_errorif firstByte is outside the range that means a 3 byte header, or if the three bytes together state a length above kMaxValueOfLengthFor3ByteHeader, which the encoder would not write back.

Definition at line 323 of file libxrpl/protocol/Serializer.cpp.

◆ encodeLengthLength()

int xrpl::Serializer::encodeLengthLength ( int length)
staticprivate

Works out how many bytes the header needs for the given length.

This deliberately repeats the width choice addEncoded makes, so that addVL's assertion can compare the two. It has no other caller; do not reach for it as a utility.

Parameters
lengthField length in bytes.
Returns
How many header bytes it needs: 1, 2 or 3.
Exceptions
std::overflow_errorif length is negative, or above kMaxValueOfLengthFor3ByteHeader.

Definition at line 255 of file libxrpl/protocol/Serializer.cpp.

◆ addEncoded()

int xrpl::Serializer::addEncoded ( int length)
private

Appends the length header for a field of the given length.

The field's own data is not written; the caller appends it next.

Parameters
lengthField length in bytes.
Returns
Offset within this Serializer at which the header was written.
Exceptions
std::overflow_errorif length is negative, or above kMaxValueOfLengthFor3ByteHeader.

Definition at line 211 of file libxrpl/protocol/Serializer.cpp.

◆ addInteger() [2/6]

template<>
int xrpl::Serializer::addInteger ( unsigned char i)

Definition at line 45 of file libxrpl/protocol/Serializer.cpp.

◆ addInteger() [3/6]

template<>
int xrpl::Serializer::addInteger ( std::uint16_t i)

Definition at line 51 of file libxrpl/protocol/Serializer.cpp.

◆ addInteger() [4/6]

template<>
int xrpl::Serializer::addInteger ( std::uint32_t i)

Definition at line 57 of file libxrpl/protocol/Serializer.cpp.

◆ addInteger() [5/6]

template<>
int xrpl::Serializer::addInteger ( std::uint64_t i)

Definition at line 63 of file libxrpl/protocol/Serializer.cpp.

◆ addInteger() [6/6]

template<>
int xrpl::Serializer::addInteger ( std::int32_t i)

Definition at line 69 of file libxrpl/protocol/Serializer.cpp.

Member Data Documentation

◆ data_

Blob xrpl::Serializer::data_
private

Definition at line 26 of file Serializer.h.

◆ kMaxNumberOfBytesInHeader

int xrpl::Serializer::kMaxNumberOfBytesInHeader = 3
staticconstexpr

A header is never longer than this.

The encoder fills a buffer of this size and writes only the bytes it used.

Definition at line 33 of file Serializer.h.

◆ kMinValueOfFirstByteFor1ByteHeader

int xrpl::Serializer::kMinValueOfFirstByteFor1ByteHeader = 0
staticconstexpr

Definition at line 47 of file Serializer.h.

◆ kMaxValueOfFirstByteFor1ByteHeader

int xrpl::Serializer::kMaxValueOfFirstByteFor1ByteHeader = 192
staticconstexpr

Definition at line 48 of file Serializer.h.

◆ kMinValueOfFirstByteFor2ByteHeader

int xrpl::Serializer::kMinValueOfFirstByteFor2ByteHeader
staticconstexpr
Initial value:
=
static constexpr int kMaxValueOfFirstByteFor1ByteHeader
Definition Serializer.h:48

Definition at line 50 of file Serializer.h.

◆ kMaxValueOfFirstByteFor2ByteHeader

int xrpl::Serializer::kMaxValueOfFirstByteFor2ByteHeader = 240
staticconstexpr

Definition at line 52 of file Serializer.h.

◆ kMinValueOfFirstByteFor3ByteHeader

int xrpl::Serializer::kMinValueOfFirstByteFor3ByteHeader
staticconstexpr
Initial value:
=
static constexpr int kMaxValueOfFirstByteFor2ByteHeader
Definition Serializer.h:52

Definition at line 54 of file Serializer.h.

◆ kMaxValueOfFirstByteFor3ByteHeader

int xrpl::Serializer::kMaxValueOfFirstByteFor3ByteHeader = 254
staticconstexpr

Definition at line 57 of file Serializer.h.

◆ kNumberOfValuesInOneByte

int xrpl::Serializer::kNumberOfValuesInOneByte = 256
staticconstexpr

Definition at line 67 of file Serializer.h.

◆ kNumberOfValuesInTwoBytes

int xrpl::Serializer::kNumberOfValuesInTwoBytes
staticconstexpr
Initial value:

Definition at line 68 of file Serializer.h.

◆ kMinValueOfLengthFor1ByteHeader

int xrpl::Serializer::kMinValueOfLengthFor1ByteHeader = kMinValueOfFirstByteFor1ByteHeader
staticconstexpr

A 1 byte header holds the length in the byte itself, so both ends of this range are the same numbers as the first byte's own range.

Definition at line 83 of file Serializer.h.

◆ kMaxValueOfLengthFor1ByteHeader

int xrpl::Serializer::kMaxValueOfLengthFor1ByteHeader = kMaxValueOfFirstByteFor1ByteHeader
staticconstexpr

Definition at line 84 of file Serializer.h.

◆ kMinValueOfLengthFor2ByteHeader

int xrpl::Serializer::kMinValueOfLengthFor2ByteHeader = kMaxValueOfLengthFor1ByteHeader + 1
staticconstexpr

Definition at line 86 of file Serializer.h.

◆ kMaxValueOfLengthFor2ByteHeader

int xrpl::Serializer::kMaxValueOfLengthFor2ByteHeader
staticconstexpr
Initial value:

48 values of the first byte mean a 2 byte header, and each of them covers 256 lengths.

The 48 is worked out from the two range ends above, so it stays right if either of them changes.

Definition at line 93 of file Serializer.h.

◆ kMinValueOfLengthFor3ByteHeader

int xrpl::Serializer::kMinValueOfLengthFor3ByteHeader = kMaxValueOfLengthFor2ByteHeader + 1
staticconstexpr

Definition at line 98 of file Serializer.h.

◆ kMaxRepresentableLength

int xrpl::Serializer::kMaxRepresentableLength
staticconstexpr
Initial value:
1
static constexpr int kNumberOfValuesInTwoBytes
Definition Serializer.h:68
static constexpr int kMaxValueOfFirstByteFor3ByteHeader
Definition Serializer.h:57
static constexpr int kMinValueOfLengthFor3ByteHeader
Definition Serializer.h:98

14 values of the first byte mean a 3 byte header, and each of them covers 65,536 lengths.

Counted the same way, that gives the largest length any header can state.

Nothing is accepted or rejected against this. The assertion below uses it to check that every length the encoder writes is one a header can state.

Definition at line 108 of file Serializer.h.

◆ kMaxValueOfLengthFor3ByteHeader

int xrpl::Serializer::kMaxValueOfLengthFor3ByteHeader = 918744
staticconstexpr

The largest length the encoder will write.

This is the one number here that is picked rather than worked out. The decoder accepts nothing above it, so both sides agree on the same set of lengths.

Definition at line 118 of file Serializer.h.