6#ifndef ALT_INTEGRATION_VERIBLOCK_READ_STREAM_HPP
7#define ALT_INTEGRATION_VERIBLOCK_READ_STREAM_HPP
15#include "validation_state.hpp"
28 explicit ReadStream(
const std::vector<uint8_t> &v);
45 std::vector<uint8_t> assertRead(
size_t size);
47 bool readSlice(
size_t size,
55 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
56 T assertReadBE(
size_t bytes =
sizeof(T)) {
59 bool result = readBE<T>(t, state, bytes);
60 VBK_ASSERT_MSG(result,
"Can't readBE: %s", state.toString());
67 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
69 if (!hasMore(bytes)) {
72 format(
"Tried to read {} bytes from stream, but it has {} bytes",
77 for (
size_t i = 0, shift = (bytes - 1) * 8; i < bytes; i++, shift -= 8) {
78 t +=
static_cast<T
>((
static_cast<T
>(m_Buffer[m_Pos++])) << shift);
85 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
87 ValidationState state;
89 bool result = readLE<T>(t, state);
90 VBK_ASSERT_MSG(result,
"Can't readLE: %s", state.toString());
97 typename =
typename std::enable_if<std::is_integral<T>::value>::type>
98 bool readLE(T &t, ValidationState &state) {
99 if (!hasMore(
sizeof(T))) {
100 return state.Invalid(
102 format(
"Tried to read {} bytes from stream, but it has {} bytes",
108 for (
size_t i = 0, shift = 0; i <
sizeof(T); i++, shift += 8) {
109 t +=
static_cast<T
>((
static_cast<T
>(m_Buffer[m_Pos++])) << shift);
114 uint32_t getVersion() const noexcept;
115 void setVersion(uint32_t version) noexcept;
116 size_t position() const noexcept;
117 void setPosition(const
size_t &) noexcept;
118 size_t remaining() const noexcept {
return (m_Size - m_Pos); }
119 bool hasMore(
size_t nbytes)
const noexcept {
return (remaining() >= nbytes); }
120 void reset() noexcept;
121 Slice<const uint8_t> data() const;
122 Slice<const uint8_t> remainingBytes() const;
125 void assign(const
void *buff,
size_t numOfBytes);
128 ReadStream(const ReadStream &) = default;
129 ReadStream &operator=(const ReadStream &) = default;
132 uint32_t m_version{0};
134 const uint8_t *m_Buffer{
nullptr};
Class that is used for storing validation state.
Binary reading stream, that is useful during binary deserialization.
bool read(size_t size, uint8_t *out, ValidationState &state)
Read type T of 'size' bytes.
Non-owning contiguous array.