3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Buffer.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/basics/safe_cast.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/protocol/HashPrefix.h>
11#include <xrpl/protocol/SField.h>
122 "a length the encoder writes must be one a header can state");
135 XRPL_ASSERT(
data,
"xrpl::Serializer::Serializer(void const*) : non-null input");
152 [[nodiscard]]
void const*
160 add8(
unsigned char byteValue);
164 template <
typename T>
169 int const ret =
data_.size();
170 data_.push_back(
static_cast<unsigned char>((i >> 24) & 0xff));
171 data_.push_back(
static_cast<unsigned char>((i >> 16) & 0xff));
172 data_.push_back(
static_cast<unsigned char>((i >> 8) & 0xff));
173 data_.push_back(
static_cast<unsigned char>(i & 0xff));
180 template <
typename T>
185 int const ret =
data_.size();
186 data_.push_back(
static_cast<unsigned char>((i >> 56) & 0xff));
187 data_.push_back(
static_cast<unsigned char>((i >> 48) & 0xff));
188 data_.push_back(
static_cast<unsigned char>((i >> 40) & 0xff));
189 data_.push_back(
static_cast<unsigned char>((i >> 32) & 0xff));
190 data_.push_back(
static_cast<unsigned char>((i >> 24) & 0xff));
191 data_.push_back(
static_cast<unsigned char>((i >> 16) & 0xff));
192 data_.push_back(
static_cast<unsigned char>((i >> 8) & 0xff));
193 data_.push_back(
static_cast<unsigned char>(i & 0xff));
197 template <
typename Integer>
200 template <std::
size_t Bits,
class Tag>
212 addRaw(
void const* ptr,
int len);
220 template <
class Iter>
224 addVL(
void const* ptr,
int len);
228 get8(
int&,
int offset)
const;
230 template <
typename Integer>
234 static auto const kBytes =
sizeof(Integer);
235 if ((offset + kBytes) >
data_.size())
239 auto ptr = &
data_[offset];
240 for (
auto i = 0; i < kBytes; ++i)
249 template <std::
size_t Bits,
typename Tag =
void>
253 auto success = (offset + (Bits / 8)) <=
data_.size();
255 memcpy(
data.begin(), &(
data_.front()) + offset, (Bits / 8));
272 [[nodiscard]]
Blob const&
293 [[nodiscard]]
void const*
325 return data_.begin();
332 [[nodiscard]] Blob::const_iterator
335 return data_.begin();
337 [[nodiscard]] Blob::const_iterator
355 return data_.capacity();
466 len -=
begin->size();
469 XRPL_ASSERT(len == 0,
"xrpl::Serializer::addVL : length matches distance");
495 static_assert(N > 0);
510 return static_cast<int>(
remain_);
530 template <std::
size_t Bits,
class Tag =
void>
595template <std::
size_t Bits,
class Tag>
599 auto const n = Bits / 8;
Integers of any length that is a multiple of 32-bits.
static BaseUInt fromVoid(void const *data)
static constexpr std::size_t size()
Like std::vector<char> but better.
int getBytesLeft() const noexcept
SerialIter(Slice const &slice)
int getVLDataLength()
Reads the length header at the read position and steps past it.
BaseUInt< Bits, Tag > getBitString()
bool empty() const noexcept
Slice getSlice(std::size_t bytes)
void getFieldID(int &type, int &name)
SerialIter(void const *data, std::size_t size) noexcept
SerialIter(std::uint8_t const (&data)[N])
int addFieldID(int type, int name)
Blob::const_iterator begin() const
void const * getDataPtr() const
static int decodeLengthLength(std::byte firstByte)
Works out how long a header is, from its first byte.
static constexpr int kMaxValueOfFirstByteFor1ByteHeader
Blob const & peekData() const
bool getInteger(Integer &number, int offset)
int addBitString(BaseUInt< Bits, Tag > const &v)
static constexpr int kMaxValueOfFirstByteFor2ByteHeader
static constexpr int kMinValueOfLengthFor2ByteHeader
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 kMaxValueOfLengthFor1ByteHeader
int add16(std::uint16_t i)
static constexpr int kMaxNumberOfBytesInHeader
A header is never longer than this.
static constexpr int kMinValueOfLengthFor1ByteHeader
A 1 byte header holds the length in the byte itself, so both ends of this range are the same numbers ...
static constexpr int kMaxValueOfLengthFor3ByteHeader
The largest length the encoder will write.
bool getBitString(BaseUInt< Bits, Tag > &data, int offset) const
std::string getString() const
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 kNumberOfValuesInOneByte
UInt256 getSHA512Half() const
int addVL(Blob const &vector)
int add8(unsigned char byteValue)
Serializer(void const *data, std::size_t size)
static constexpr int kMinValueOfFirstByteFor2ByteHeader
int addFieldID(SerializedTypeID type, int name)
int addEncoded(int length)
Appends the length header for a field of the given length.
int addRaw(Blob const &vector)
Slice slice() const noexcept
bool operator==(Blob const &v) const
bool operator==(Serializer const &v) const
static constexpr int kNumberOfValuesInTwoBytes
static int decodeVLLength(std::byte firstByte)
Reads the field length out of a 1 byte header.
static constexpr int kMaxValueOfFirstByteFor3ByteHeader
std::size_t size() const noexcept
bool get8(int &, int offset) const
int getDataLength() const
Blob::const_iterator end() const
static int encodeLengthLength(int length)
Works out how many bytes the header needs for the given length.
static constexpr int kMinValueOfFirstByteFor3ByteHeader
void const * data() const noexcept
static constexpr int kMinValueOfFirstByteFor1ByteHeader
static constexpr int kMinValueOfLengthFor3ByteHeader
An immutable linear range of bytes.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr Dest safeCast(Src s) noexcept
HashPrefix
Prefix for hashing functions.
std::vector< unsigned char > Blob
Storage for linear binary data.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)