feat(Binary): add Writer, ByteBuffer, DecodeBudget, and explicit endian codecs (M-BINARY-02, M-BINARY-03)
This commit is contained in:
1 parent
9ac76bbcf9
commit
eacb8a4602
10 files changed
+1036
-36
No files matched your search
@@ -0,0 +1,149 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Functional/Error.hpp>
|
||||||
|
#include <Void/Functional/Result.hpp>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace Void::Binary
|
||||||
|
{
|
||||||
|
|
||||||
|
enum class BinaryErrorCode : std::int32_t
|
||||||
|
{
|
||||||
|
truncated = 1,
|
||||||
|
buffer_full,
|
||||||
|
out_of_bounds,
|
||||||
|
unsupported_endian,
|
||||||
|
budget_exceeded,
|
||||||
|
depth_exceeded,
|
||||||
|
allocation_failed,
|
||||||
|
invalid_bool,
|
||||||
|
size_overflow
|
||||||
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr Void::Result::ErrorCode
|
||||||
|
to_error_code(BinaryErrorCode code) noexcept
|
||||||
|
{
|
||||||
|
return Void::Result::ErrorCode{
|
||||||
|
Void::Result::kBinaryErrorDomain,
|
||||||
|
static_cast<std::int32_t>(code)};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] inline Void::Result::Error
|
||||||
|
make_error(Void::Result::ErrorKind kind,
|
||||||
|
BinaryErrorCode code,
|
||||||
|
std::string message)
|
||||||
|
{
|
||||||
|
return Void::Result::Error{
|
||||||
|
kind,
|
||||||
|
std::move(message),
|
||||||
|
to_error_code(code)};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DecodeLimits
|
||||||
|
{
|
||||||
|
std::size_t max_field_bytes = 16 * 1024 * 1024;
|
||||||
|
std::size_t max_total_bytes = 64 * 1024 * 1024;
|
||||||
|
unsigned max_depth = 64;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DecodeBudget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class DepthGuard
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DepthGuard() noexcept = default;
|
||||||
|
explicit DepthGuard(DecodeBudget* budget) noexcept : budget_{budget} {}
|
||||||
|
DepthGuard(const DepthGuard&) = delete;
|
||||||
|
DepthGuard& operator=(const DepthGuard&) = delete;
|
||||||
|
|
||||||
|
DepthGuard(DepthGuard&& other) noexcept
|
||||||
|
: budget_{std::exchange(other.budget_, nullptr)}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
DepthGuard& operator=(DepthGuard&& other) noexcept
|
||||||
|
{
|
||||||
|
if (this != &other) {
|
||||||
|
release();
|
||||||
|
budget_ = std::exchange(other.budget_, nullptr);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~DepthGuard() { release(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void release() noexcept
|
||||||
|
{
|
||||||
|
if (budget_ && budget_->depth_ > 0) {
|
||||||
|
--budget_->depth_;
|
||||||
|
}
|
||||||
|
budget_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
DecodeBudget* budget_{nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
DecodeBudget() noexcept = default;
|
||||||
|
explicit DecodeBudget(DecodeLimits limits) noexcept : limits_{limits} {}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> consume_field(std::size_t bytes)
|
||||||
|
{
|
||||||
|
if (bytes > limits_.max_field_bytes) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::budget_exceeded,
|
||||||
|
"Binary::DecodeBudget: field byte count " +
|
||||||
|
std::to_string(bytes) +
|
||||||
|
" exceeds max_field_bytes (" +
|
||||||
|
std::to_string(limits_.max_field_bytes) + ")")};
|
||||||
|
}
|
||||||
|
if (bytes > limits_.max_total_bytes - consumed_bytes_) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::budget_exceeded,
|
||||||
|
"Binary::DecodeBudget: total byte budget exceeded")};
|
||||||
|
}
|
||||||
|
consumed_bytes_ += bytes;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<DepthGuard> enter_scope()
|
||||||
|
{
|
||||||
|
if (depth_ >= limits_.max_depth) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::depth_exceeded,
|
||||||
|
"Binary::DecodeBudget: nesting depth exceeds max_depth (" +
|
||||||
|
std::to_string(limits_.max_depth) + ")")};
|
||||||
|
}
|
||||||
|
++depth_;
|
||||||
|
return DepthGuard{this};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t consumed_bytes() const noexcept
|
||||||
|
{
|
||||||
|
return consumed_bytes_;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t remaining_bytes() const noexcept
|
||||||
|
{
|
||||||
|
return limits_.max_total_bytes - consumed_bytes_;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] unsigned depth() const noexcept { return depth_; }
|
||||||
|
|
||||||
|
[[nodiscard]] DecodeLimits limits() const noexcept { return limits_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
DecodeLimits limits_{};
|
||||||
|
std::size_t consumed_bytes_{0};
|
||||||
|
unsigned depth_{0};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Void::Binary
|
||||||
@@ -0,0 +1,257 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Binary/Budget.hpp>
|
||||||
|
#include <Void/Binary/Reader.hpp>
|
||||||
|
#include <Void/Functional/Error.hpp>
|
||||||
|
#include <Void/Functional/Result.hpp>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <bit>
|
||||||
|
#include <concepts>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <format>
|
||||||
|
#include <functional>
|
||||||
|
#include <limits>
|
||||||
|
#include <memory_resource>
|
||||||
|
#include <span>
|
||||||
|
#include <string_view>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Void::Binary
|
||||||
|
{
|
||||||
|
|
||||||
|
inline constexpr std::size_t kDefaultMaxByteBufferBytes = 16 * 1024 * 1024;
|
||||||
|
|
||||||
|
/// Immutable owning byte container produced by zero-copy ByteBuffer::freeze().
|
||||||
|
class FrozenBytes
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FrozenBytes() noexcept = default;
|
||||||
|
|
||||||
|
explicit FrozenBytes(std::pmr::vector<std::byte> storage) noexcept
|
||||||
|
: m_storage{std::move(storage)}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::span<const std::byte> view() const noexcept
|
||||||
|
{
|
||||||
|
return m_storage;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] const std::byte* data() const noexcept
|
||||||
|
{
|
||||||
|
return m_storage.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t size() const noexcept
|
||||||
|
{
|
||||||
|
return m_storage.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool empty() const noexcept { return m_storage.empty(); }
|
||||||
|
|
||||||
|
[[nodiscard]] Reader reader() const noexcept { return Reader{view()}; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::pmr::vector<std::byte> m_storage;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// PMR-backed growing binary output buffer bounded by an explicit byte ceiling.
|
||||||
|
/// Allocation failure and capacity exhaustion preserve previously committed bytes.
|
||||||
|
class ByteBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ByteBuffer() noexcept
|
||||||
|
: ByteBuffer(kDefaultMaxByteBufferBytes, std::pmr::get_default_resource())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit ByteBuffer(
|
||||||
|
std::size_t max_bytes,
|
||||||
|
std::pmr::memory_resource* resource = std::pmr::get_default_resource()) noexcept
|
||||||
|
: m_data{resource ? resource : std::pmr::get_default_resource()},
|
||||||
|
m_max_bytes{max_bytes}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename R>
|
||||||
|
requires(std::convertible_to<R, std::pmr::memory_resource*> &&
|
||||||
|
!std::integral<std::remove_cvref_t<R>> &&
|
||||||
|
!std::same_as<std::remove_cvref_t<R>, std::nullptr_t>)
|
||||||
|
explicit ByteBuffer(R resource,
|
||||||
|
std::size_t max_bytes = kDefaultMaxByteBufferBytes) noexcept
|
||||||
|
: ByteBuffer(max_bytes, static_cast<std::pmr::memory_resource*>(resource))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> reserve(std::size_t capacity)
|
||||||
|
{
|
||||||
|
if (capacity > m_max_bytes) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::budget_exceeded,
|
||||||
|
std::format(
|
||||||
|
"Binary::ByteBuffer::reserve: requested {} bytes exceeds max_bytes ({})",
|
||||||
|
capacity, m_max_bytes))};
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
m_data.reserve(capacity);
|
||||||
|
} catch (const std::bad_alloc&) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::allocation_failed,
|
||||||
|
"Binary::ByteBuffer::reserve: upstream allocation failed")};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
append(std::span<const std::byte> bytes)
|
||||||
|
{
|
||||||
|
if (bytes.empty()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const std::size_t old_size = m_data.size();
|
||||||
|
if (bytes.size() > m_max_bytes - old_size) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::budget_exceeded,
|
||||||
|
std::format(
|
||||||
|
"Binary::ByteBuffer::append: adding {} bytes at size {} exceeds max_bytes ({})",
|
||||||
|
bytes.size(), old_size, m_max_bytes))};
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::byte* const cur_begin = m_data.data();
|
||||||
|
const std::byte* const cur_end = cur_begin + old_size;
|
||||||
|
const bool self_aliased =
|
||||||
|
!m_data.empty() &&
|
||||||
|
!std::less<const std::byte*>{}(bytes.data(), cur_begin) &&
|
||||||
|
std::less<const std::byte*>{}(bytes.data(), cur_end);
|
||||||
|
const std::size_t alias_offset =
|
||||||
|
self_aliased ? static_cast<std::size_t>(bytes.data() - cur_begin) : 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
m_data.reserve(old_size + bytes.size());
|
||||||
|
} catch (const std::bad_alloc&) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::allocation_failed,
|
||||||
|
"Binary::ByteBuffer::append: upstream allocation failed")};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self_aliased) {
|
||||||
|
const std::byte* const src = m_data.data() + alias_offset;
|
||||||
|
m_data.insert(m_data.end(), src, src + bytes.size());
|
||||||
|
} else {
|
||||||
|
m_data.insert(m_data.end(), bytes.begin(), bytes.end());
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
write(T value, std::endian byteOrder)
|
||||||
|
{
|
||||||
|
if (byteOrder != std::endian::little && byteOrder != std::endian::big) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::invalid_argument,
|
||||||
|
BinaryErrorCode::unsupported_endian,
|
||||||
|
"Binary::ByteBuffer::write: unsupported byte order")};
|
||||||
|
}
|
||||||
|
if constexpr (std::endian::native != std::endian::little &&
|
||||||
|
std::endian::native != std::endian::big) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::unsupported,
|
||||||
|
BinaryErrorCode::unsupported_endian,
|
||||||
|
"Binary::ByteBuffer::write: mixed-endian targets are unsupported")};
|
||||||
|
} else {
|
||||||
|
if (byteOrder != std::endian::native) {
|
||||||
|
value = std::byteswap(value);
|
||||||
|
}
|
||||||
|
const auto bytes =
|
||||||
|
std::bit_cast<std::array<std::byte, sizeof(T)>>(value);
|
||||||
|
return append(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeLittle(T value)
|
||||||
|
{
|
||||||
|
return write<T>(value, std::endian::little);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeBig(T value)
|
||||||
|
{
|
||||||
|
return write<T>(value, std::endian::big);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeBool(bool value)
|
||||||
|
{
|
||||||
|
return write<std::uint8_t>(
|
||||||
|
value ? std::uint8_t{1} : std::uint8_t{0}, std::endian::little);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
writeF32(float value, std::endian byteOrder)
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<float>::is_iec559 &&
|
||||||
|
sizeof(float) == sizeof(std::uint32_t));
|
||||||
|
return write<std::uint32_t>(std::bit_cast<std::uint32_t>(value), byteOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
writeF64(double value, std::endian byteOrder)
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<double>::is_iec559 &&
|
||||||
|
sizeof(double) == sizeof(std::uint64_t));
|
||||||
|
return write<std::uint64_t>(std::bit_cast<std::uint64_t>(value), byteOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeString(std::string_view value)
|
||||||
|
{
|
||||||
|
return append(std::as_bytes(std::span{value.data(), value.size()}));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] FrozenBytes freeze() noexcept
|
||||||
|
{
|
||||||
|
std::pmr::vector<std::byte> moved{m_data.get_allocator()};
|
||||||
|
moved.swap(m_data);
|
||||||
|
return FrozenBytes{std::move(moved)};
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() noexcept { m_data.clear(); }
|
||||||
|
|
||||||
|
[[nodiscard]] std::span<const std::byte> view() const noexcept
|
||||||
|
{
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t size() const noexcept { return m_data.size(); }
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t capacity() const noexcept
|
||||||
|
{
|
||||||
|
return m_data.capacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool empty() const noexcept { return m_data.empty(); }
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t max_bytes() const noexcept { return m_max_bytes; }
|
||||||
|
|
||||||
|
[[nodiscard]] std::size_t remaining_budget() const noexcept
|
||||||
|
{
|
||||||
|
return m_max_bytes - m_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::pmr::vector<std::byte> m_data;
|
||||||
|
std::size_t m_max_bytes{kDefaultMaxByteBufferBytes};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Void::Binary
|
||||||
+179
-29
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Binary/Budget.hpp>
|
||||||
#include <Void/Functional/Error.hpp>
|
#include <Void/Functional/Error.hpp>
|
||||||
#include <Void/Functional/Result.hpp>
|
#include <Void/Functional/Result.hpp>
|
||||||
|
|
||||||
@@ -11,15 +12,62 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <limits>
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace Void::Binary
|
namespace Void::Binary
|
||||||
{
|
{
|
||||||
/// Bounds-checked, non-owning reader for binary formats.
|
/// Bounds-checked, non-owning reader for binary formats.
|
||||||
class Reader
|
class Reader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class Transaction
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
explicit Transaction(Reader& reader) noexcept
|
||||||
|
: m_reader{&reader}, m_saved{reader.checkpoint()}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Transaction(const Transaction&) = delete;
|
||||||
|
Transaction& operator=(const Transaction&) = delete;
|
||||||
|
|
||||||
|
Transaction(Transaction&& other) noexcept
|
||||||
|
: m_reader{std::exchange(other.m_reader, nullptr)},
|
||||||
|
m_saved{other.m_saved},
|
||||||
|
m_committed{std::exchange(other.m_committed, true)}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Transaction& operator=(Transaction&& other) noexcept
|
||||||
|
{
|
||||||
|
if (this != &other) {
|
||||||
|
rollback_if_uncommitted();
|
||||||
|
m_reader = std::exchange(other.m_reader, nullptr);
|
||||||
|
m_saved = other.m_saved;
|
||||||
|
m_committed = std::exchange(other.m_committed, true);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Transaction() { rollback_if_uncommitted(); }
|
||||||
|
|
||||||
|
void commit() noexcept { m_committed = true; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void rollback_if_uncommitted() noexcept
|
||||||
|
{
|
||||||
|
if (m_reader && !m_committed) {
|
||||||
|
(void)m_reader->seek(m_saved);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Reader* m_reader{nullptr};
|
||||||
|
std::size_t m_saved{0};
|
||||||
|
bool m_committed{false};
|
||||||
|
};
|
||||||
|
|
||||||
explicit constexpr Reader(std::span<const std::byte> bytes) noexcept
|
explicit constexpr Reader(std::span<const std::byte> bytes) noexcept
|
||||||
: m_bytes(bytes)
|
: m_bytes(bytes)
|
||||||
@@ -46,12 +94,24 @@ namespace Void::Binary
|
|||||||
return remaining() == 0;
|
return remaining() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr std::size_t checkpoint() const noexcept
|
||||||
|
{
|
||||||
|
return m_position;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> rollback(std::size_t saved_position)
|
||||||
|
{
|
||||||
|
return seek(saved_position);
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] Void::Result::Result<void> seek(std::size_t position)
|
[[nodiscard]] Void::Result::Result<void> seek(std::size_t position)
|
||||||
{
|
{
|
||||||
if (position > m_bytes.size()) {
|
if (position > m_bytes.size()) {
|
||||||
return Void::Result::fail(std::format(
|
return std::unexpected{make_error(
|
||||||
"Binary::Reader::seek: offset {} exceeds size {}",
|
Void::Result::ErrorKind::invalid_argument,
|
||||||
position, m_bytes.size()));
|
BinaryErrorCode::out_of_bounds,
|
||||||
|
std::format("Binary::Reader::seek: offset {} exceeds size {}",
|
||||||
|
position, m_bytes.size()))};
|
||||||
}
|
}
|
||||||
m_position = position;
|
m_position = position;
|
||||||
return {};
|
return {};
|
||||||
@@ -77,6 +137,25 @@ namespace Void::Binary
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<std::span<const std::byte>>
|
||||||
|
readBytesChecked(std::size_t count, DecodeBudget& budget)
|
||||||
|
{
|
||||||
|
if (count > budget.limits().max_field_bytes ||
|
||||||
|
count > budget.remaining_bytes()) {
|
||||||
|
auto over = budget.consume_field(count);
|
||||||
|
return std::unexpected{std::move(over).error()};
|
||||||
|
}
|
||||||
|
auto bytes = readBytes(count);
|
||||||
|
if (!bytes) {
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
if (auto charged = budget.consume_field(count); !charged) {
|
||||||
|
m_position -= count;
|
||||||
|
return std::unexpected{std::move(charged).error()};
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] Void::Result::Result<std::span<const std::byte>>
|
[[nodiscard]] Void::Result::Result<std::span<const std::byte>>
|
||||||
peekBytes(std::size_t count) const
|
peekBytes(std::size_t count) const
|
||||||
{
|
{
|
||||||
@@ -87,10 +166,23 @@ namespace Void::Binary
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <std::integral T>
|
template <std::integral T>
|
||||||
requires(!std::same_as<T, bool>)
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
[[nodiscard]] Void::Result::Result<T>
|
[[nodiscard]] Void::Result::Result<T>
|
||||||
read(std::endian byteOrder = std::endian::native)
|
read(std::endian byteOrder = std::endian::native)
|
||||||
{
|
{
|
||||||
|
if (byteOrder != std::endian::little && byteOrder != std::endian::big) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::invalid_argument,
|
||||||
|
BinaryErrorCode::unsupported_endian,
|
||||||
|
"Binary::Reader::read: unsupported byte order")};
|
||||||
|
}
|
||||||
|
if constexpr (std::endian::native != std::endian::little &&
|
||||||
|
std::endian::native != std::endian::big) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::unsupported,
|
||||||
|
BinaryErrorCode::unsupported_endian,
|
||||||
|
"Binary::Reader::read: mixed-endian targets are unsupported")};
|
||||||
|
} else {
|
||||||
auto bytes = readBytes(sizeof(T));
|
auto bytes = readBytes(sizeof(T));
|
||||||
if (!bytes) {
|
if (!bytes) {
|
||||||
return std::unexpected{bytes.error()};
|
return std::unexpected{bytes.error()};
|
||||||
@@ -98,30 +190,79 @@ namespace Void::Binary
|
|||||||
std::array<std::byte, sizeof(T)> storage{};
|
std::array<std::byte, sizeof(T)> storage{};
|
||||||
std::ranges::copy(*bytes, storage.begin());
|
std::ranges::copy(*bytes, storage.begin());
|
||||||
T value = std::bit_cast<T>(storage);
|
T value = std::bit_cast<T>(storage);
|
||||||
if constexpr (std::endian::native != std::endian::little &&
|
if (byteOrder != std::endian::native) {
|
||||||
std::endian::native != std::endian::big) {
|
|
||||||
return Void::Result::fail(
|
|
||||||
"Binary::Reader::read: mixed-endian targets are "
|
|
||||||
"unsupported");
|
|
||||||
} else if (byteOrder != std::endian::native) {
|
|
||||||
value = std::byteswap(value);
|
value = std::byteswap(value);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[[nodiscard]] Void::Result::Result<std::string>
|
template <std::integral T>
|
||||||
readString(std::size_t count)
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<T> readLittle()
|
||||||
|
{
|
||||||
|
return read<T>(std::endian::little);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<T> readBig()
|
||||||
|
{
|
||||||
|
return read<T>(std::endian::big);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<bool> readBool()
|
||||||
|
{
|
||||||
|
auto byte = peekBytes(1);
|
||||||
|
if (!byte) {
|
||||||
|
return std::unexpected{byte.error()};
|
||||||
|
}
|
||||||
|
const auto raw = std::to_integer<std::uint8_t>((*byte)[0]);
|
||||||
|
if (raw > 1) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::corrupt,
|
||||||
|
BinaryErrorCode::invalid_bool,
|
||||||
|
std::format(
|
||||||
|
"Binary::Reader::readBool: invalid byte 0x{:02x} at offset {}",
|
||||||
|
raw, m_position))};
|
||||||
|
}
|
||||||
|
++m_position;
|
||||||
|
return raw == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<float> readF32(std::endian byteOrder)
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<float>::is_iec559 &&
|
||||||
|
sizeof(float) == sizeof(std::uint32_t));
|
||||||
|
auto bits = read<std::uint32_t>(byteOrder);
|
||||||
|
if (!bits) {
|
||||||
|
return std::unexpected{bits.error()};
|
||||||
|
}
|
||||||
|
return std::bit_cast<float>(*bits);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<double> readF64(std::endian byteOrder)
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<double>::is_iec559 &&
|
||||||
|
sizeof(double) == sizeof(std::uint64_t));
|
||||||
|
auto bits = read<std::uint64_t>(byteOrder);
|
||||||
|
if (!bits) {
|
||||||
|
return std::unexpected{bits.error()};
|
||||||
|
}
|
||||||
|
return std::bit_cast<double>(*bits);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<std::string> readString(std::size_t count)
|
||||||
{
|
{
|
||||||
auto bytes = readBytes(count);
|
auto bytes = readBytes(count);
|
||||||
if (!bytes) {
|
if (!bytes) {
|
||||||
return std::unexpected{bytes.error()};
|
return std::unexpected{bytes.error()};
|
||||||
}
|
}
|
||||||
return std::string{reinterpret_cast<const char*>(bytes->data()),
|
return std::string{
|
||||||
bytes->size()};
|
reinterpret_cast<const char*>(bytes->data()), bytes->size()};
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] Void::Result::Result<Reader>
|
[[nodiscard]] Void::Result::Result<Reader> subreader(std::size_t count)
|
||||||
subreader(std::size_t count)
|
|
||||||
{
|
{
|
||||||
auto bytes = readBytes(count);
|
auto bytes = readBytes(count);
|
||||||
if (!bytes) {
|
if (!bytes) {
|
||||||
@@ -130,27 +271,36 @@ namespace Void::Binary
|
|||||||
return Reader{*bytes};
|
return Reader{*bytes};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
[[nodiscard]] Void::Result::Result<Reader>
|
||||||
|
subreaderChecked(std::size_t count, DecodeBudget& budget)
|
||||||
|
{
|
||||||
|
auto bytes = readBytesChecked(count, budget);
|
||||||
|
if (!bytes) {
|
||||||
|
return std::unexpected{bytes.error()};
|
||||||
|
}
|
||||||
|
return Reader{*bytes};
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
std::span<const std::byte> m_bytes;
|
std::span<const std::byte> m_bytes;
|
||||||
std::size_t m_position{0};
|
std::size_t m_position{0};
|
||||||
|
|
||||||
[[nodiscard]] Void::Result::Error
|
[[nodiscard]] Void::Result::Error
|
||||||
truncatedError(std::string_view operation,
|
truncatedError(std::string_view operation, std::size_t requested) const
|
||||||
std::size_t requested) const
|
|
||||||
{
|
{
|
||||||
return Void::Result::Error{
|
return make_error(
|
||||||
std::format("Binary::Reader::{}: requested {} bytes at "
|
Void::Result::ErrorKind::corrupt,
|
||||||
"offset {}, only {} "
|
BinaryErrorCode::truncated,
|
||||||
"remain",
|
std::format(
|
||||||
operation, requested, m_position, remaining())};
|
"Binary::Reader::{}: requested {} bytes at offset {}, only {} remain",
|
||||||
|
operation, requested, m_position, remaining()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] Void::Result::Result<void>
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
truncated(std::string_view operation,
|
truncated(std::string_view operation, std::size_t requested) const
|
||||||
std::size_t requested) const
|
|
||||||
{
|
{
|
||||||
return std::unexpected{truncatedError(operation, requested)};
|
return std::unexpected{truncatedError(operation, requested)};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Void::Binary
|
} // namespace Void::Binary
|
||||||
@@ -0,0 +1,183 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Binary/Budget.hpp>
|
||||||
|
#include <Void/Functional/Error.hpp>
|
||||||
|
#include <Void/Functional/Result.hpp>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <bit>
|
||||||
|
#include <concepts>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <format>
|
||||||
|
#include <limits>
|
||||||
|
#include <span>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace Void::Binary
|
||||||
|
{
|
||||||
|
|
||||||
|
/// Non-owning, bounds-checked writer into caller-owned mutable bytes.
|
||||||
|
class Writer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit constexpr Writer(std::span<std::byte> output) noexcept
|
||||||
|
: m_output(output)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr std::size_t position() const noexcept
|
||||||
|
{
|
||||||
|
return m_position;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr std::size_t size() const noexcept
|
||||||
|
{
|
||||||
|
return m_output.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr std::size_t remaining() const noexcept
|
||||||
|
{
|
||||||
|
return m_output.size() - m_position;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr bool empty() const noexcept
|
||||||
|
{
|
||||||
|
return remaining() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr std::span<const std::byte> written() const noexcept
|
||||||
|
{
|
||||||
|
return m_output.subspan(0, m_position);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> seek(std::size_t position)
|
||||||
|
{
|
||||||
|
if (position > m_output.size()) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::invalid_argument,
|
||||||
|
BinaryErrorCode::out_of_bounds,
|
||||||
|
std::format("Binary::Writer::seek: offset {} exceeds size {}",
|
||||||
|
position, m_output.size()))};
|
||||||
|
}
|
||||||
|
m_position = position;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr std::size_t checkpoint() const noexcept
|
||||||
|
{
|
||||||
|
return m_position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rolls back the cursor position only; does not restore overwritten bytes.
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
rollbackPosition(std::size_t saved_position)
|
||||||
|
{
|
||||||
|
return seek(saved_position);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
writeBytes(std::span<const std::byte> bytes)
|
||||||
|
{
|
||||||
|
if (bytes.size() > remaining()) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::buffer_full,
|
||||||
|
std::format(
|
||||||
|
"Binary::Writer::writeBytes: requested {} bytes at offset {}, only {} remain",
|
||||||
|
bytes.size(), m_position, remaining()))};
|
||||||
|
}
|
||||||
|
if (!bytes.empty()) {
|
||||||
|
std::memmove(m_output.data() + m_position, bytes.data(), bytes.size());
|
||||||
|
m_position += bytes.size();
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
write(T value, std::endian byteOrder)
|
||||||
|
{
|
||||||
|
if (byteOrder != std::endian::little && byteOrder != std::endian::big) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::invalid_argument,
|
||||||
|
BinaryErrorCode::unsupported_endian,
|
||||||
|
"Binary::Writer::write: unsupported byte order")};
|
||||||
|
}
|
||||||
|
if constexpr (std::endian::native != std::endian::little &&
|
||||||
|
std::endian::native != std::endian::big) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::unsupported,
|
||||||
|
BinaryErrorCode::unsupported_endian,
|
||||||
|
"Binary::Writer::write: mixed-endian targets are unsupported")};
|
||||||
|
} else {
|
||||||
|
if (sizeof(T) > remaining()) {
|
||||||
|
return std::unexpected{make_error(
|
||||||
|
Void::Result::ErrorKind::exhausted,
|
||||||
|
BinaryErrorCode::buffer_full,
|
||||||
|
std::format(
|
||||||
|
"Binary::Writer::write: requested {} bytes at offset {}, only {} remain",
|
||||||
|
sizeof(T), m_position, remaining()))};
|
||||||
|
}
|
||||||
|
if (byteOrder != std::endian::native) {
|
||||||
|
value = std::byteswap(value);
|
||||||
|
}
|
||||||
|
const auto bytes =
|
||||||
|
std::bit_cast<std::array<std::byte, sizeof(T)>>(value);
|
||||||
|
std::ranges::copy(bytes, m_output.begin() +
|
||||||
|
static_cast<std::ptrdiff_t>(m_position));
|
||||||
|
m_position += sizeof(T);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeLittle(T value)
|
||||||
|
{
|
||||||
|
return write<T>(value, std::endian::little);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeBig(T value)
|
||||||
|
{
|
||||||
|
return write<T>(value, std::endian::big);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeBool(bool value)
|
||||||
|
{
|
||||||
|
return write<std::uint8_t>(
|
||||||
|
value ? std::uint8_t{1} : std::uint8_t{0}, std::endian::little);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
writeF32(float value, std::endian byteOrder)
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<float>::is_iec559 &&
|
||||||
|
sizeof(float) == sizeof(std::uint32_t));
|
||||||
|
return write<std::uint32_t>(std::bit_cast<std::uint32_t>(value), byteOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
writeF64(double value, std::endian byteOrder)
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<double>::is_iec559 &&
|
||||||
|
sizeof(double) == sizeof(std::uint64_t));
|
||||||
|
return write<std::uint64_t>(std::bit_cast<std::uint64_t>(value), byteOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] Void::Result::Result<void> writeString(std::string_view value)
|
||||||
|
{
|
||||||
|
return writeBytes(std::as_bytes(std::span{value.data(), value.size()}));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::span<std::byte> m_output;
|
||||||
|
std::size_t m_position{0};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Void::Binary
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
void_add_module(Binary
|
void_add_module(Binary
|
||||||
INTERNAL_DEPS Functional
|
INTERNAL_DEPS Functional Memory
|
||||||
)
|
)
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
|
#include <Void/Binary/ByteBuffer.hpp>
|
||||||
#include <Void/Binary/Reader.hpp>
|
#include <Void/Binary/Reader.hpp>
|
||||||
|
#include <Void/Binary/Writer.hpp>
|
||||||
|
#include <Void/Memory/BudgetResource.hpp>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <bit>
|
#include <bit>
|
||||||
@@ -7,8 +10,31 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const std::array bytes{std::byte{0x01}, std::byte{0x02}};
|
std::array<std::byte, 16> scratch{};
|
||||||
Void::Binary::Reader reader{bytes};
|
Void::Binary::Writer writer{scratch};
|
||||||
const auto value = reader.read<std::uint16_t>(std::endian::big);
|
if (!writer.writeBig<std::uint16_t>(0x0102) ||
|
||||||
return value && *value == 0x0102 ? 0 : 1;
|
!writer.writeBool(true) ||
|
||||||
|
!writer.writeF64(3.5, std::endian::little)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Binary::Reader reader{writer.written()};
|
||||||
|
const auto u16 = reader.readBig<std::uint16_t>();
|
||||||
|
const auto b = reader.readBool();
|
||||||
|
const auto d = reader.readF64(std::endian::little);
|
||||||
|
if (!u16 || *u16 != 0x0102 || !b || !*b || !d || *d != 3.5) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Memory::BudgetResource budget{1024};
|
||||||
|
Void::Binary::ByteBuffer buf{&budget, 64};
|
||||||
|
if (!buf.writeLittle<std::uint32_t>(0x11223344u) ||
|
||||||
|
!buf.writeString("ok")) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const auto frozen = buf.freeze();
|
||||||
|
auto fr = frozen.reader();
|
||||||
|
const auto v = fr.readLittle<std::uint32_t>();
|
||||||
|
const auto s = fr.readString(2);
|
||||||
|
return v && *v == 0x11223344u && s && *s == "ok" ? 0 : 1;
|
||||||
}
|
}
|
||||||
@@ -91,13 +91,13 @@ if(_has_memory)
|
|||||||
run_consumer(MemoryOnly "${install_prefix}" "MemoryOnly" ${_no_external_deps})
|
run_consumer(MemoryOnly "${install_prefix}" "MemoryOnly" ${_no_external_deps})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
has_modules(_has_binary Binary)
|
has_modules(_has_binary Binary Memory Functional Core)
|
||||||
if(_has_binary)
|
if(_has_binary)
|
||||||
run_consumer(Binary "${install_prefix}" "Binary" ${_no_external_deps})
|
run_consumer(Binary "${install_prefix}" "Binary" ${_no_external_deps})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Core)
|
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||||
if(_has_foundation)
|
if(_has_foundation)
|
||||||
run_consumer(Foundation "${install_prefix}" "Foundation" ${_no_external_deps})
|
run_consumer(Foundation "${install_prefix}" "Foundation" ${_no_external_deps})
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#include <Void/Binary/ByteBuffer.hpp>
|
||||||
|
#include <Void/Memory/BudgetResource.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <bit>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
Void::Testing::Test binary_byte_buffer_limits_oom_preserve_and_zero_copy_freeze()
|
||||||
|
{
|
||||||
|
using Void::Binary::BinaryErrorCode;
|
||||||
|
using Void::Binary::ByteBuffer;
|
||||||
|
|
||||||
|
// Zero ceiling permits empty output only.
|
||||||
|
ByteBuffer zero_cap{std::size_t{0}};
|
||||||
|
Void::Testing::expect(zero_cap.writeString("").has_value());
|
||||||
|
const auto zero_fail = zero_cap.writeBool(true);
|
||||||
|
Void::Testing::expect(!zero_fail.has_value());
|
||||||
|
Void::Testing::expect(zero_fail.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::budget_exceeded));
|
||||||
|
|
||||||
|
// Upstream BudgetResource failure preserves previously committed bytes.
|
||||||
|
Void::Memory::BudgetResource budget{16};
|
||||||
|
ByteBuffer buf{&budget, 1024};
|
||||||
|
Void::Testing::require(buf.writeBig<std::uint32_t>(0x01020304u).has_value());
|
||||||
|
const auto before_oom_size = buf.size();
|
||||||
|
const std::array<std::byte, 64> big_chunk{};
|
||||||
|
const auto oom = buf.append(big_chunk);
|
||||||
|
Void::Testing::expect(!oom.has_value());
|
||||||
|
Void::Testing::expect(oom.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::allocation_failed));
|
||||||
|
Void::Testing::expect_eq(buf.size(), before_oom_size);
|
||||||
|
|
||||||
|
// Self-aliasing append works safely even across buffer growth.
|
||||||
|
ByteBuffer growable{256};
|
||||||
|
Void::Testing::require(growable.writeString("abc").has_value());
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
Void::Testing::require(growable.append(growable.view()).has_value());
|
||||||
|
}
|
||||||
|
Void::Testing::expect_eq(growable.size(), std::size_t{48});
|
||||||
|
const std::byte* const pre_freeze_ptr = growable.view().data();
|
||||||
|
|
||||||
|
const auto frozen = growable.freeze();
|
||||||
|
Void::Testing::expect(growable.empty());
|
||||||
|
Void::Testing::expect(frozen.data() == pre_freeze_ptr);
|
||||||
|
Void::Testing::expect_eq(frozen.size(), std::size_t{48});
|
||||||
|
auto r = frozen.reader();
|
||||||
|
const auto prefix = r.readString(6);
|
||||||
|
Void::Testing::require(prefix.has_value());
|
||||||
|
Void::Testing::expect_eq(*prefix, std::string{"abcabc"});
|
||||||
|
}
|
||||||
@@ -29,3 +29,100 @@ Void::Testing::Test binary_reader_subreader_is_bounded()
|
|||||||
Void::Testing::expect(!child->readBytes(1).has_value());
|
Void::Testing::expect(!child->readBytes(1).has_value());
|
||||||
Void::Testing::expect_eq(reader.remaining(), static_cast<std::size_t>(1));
|
Void::Testing::expect_eq(reader.remaining(), static_cast<std::size_t>(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test binary_reader_explicit_endian_bool_floats_and_transactions()
|
||||||
|
{
|
||||||
|
const std::array raw{
|
||||||
|
std::byte{0x12}, std::byte{0x34}, std::byte{0x56}, std::byte{0x78},
|
||||||
|
std::byte{0x78}, std::byte{0x56}, std::byte{0x34}, std::byte{0x12},
|
||||||
|
std::byte{0x01}, std::byte{0x02},
|
||||||
|
std::byte{0x3F}, std::byte{0x80}, std::byte{0x00}, std::byte{0x00},
|
||||||
|
std::byte{0x00}, std::byte{0x00}, std::byte{0x00}, std::byte{0x00},
|
||||||
|
std::byte{0x00}, std::byte{0x00}, std::byte{0x08}, std::byte{0x40}};
|
||||||
|
|
||||||
|
Void::Binary::Reader reader{raw};
|
||||||
|
const auto big32 = reader.readBig<std::uint32_t>();
|
||||||
|
const auto lit32 = reader.readLittle<std::uint32_t>();
|
||||||
|
Void::Testing::require(big32.has_value() && lit32.has_value());
|
||||||
|
Void::Testing::expect_eq(*big32, std::uint32_t{0x12345678u});
|
||||||
|
Void::Testing::expect_eq(*lit32, std::uint32_t{0x12345678u});
|
||||||
|
|
||||||
|
// Unsupported endian fails without moving cursor.
|
||||||
|
const auto saved_pos = reader.checkpoint();
|
||||||
|
const auto invalid_endian = static_cast<std::endian>(99);
|
||||||
|
const auto bad_end = reader.read<std::uint16_t>(invalid_endian);
|
||||||
|
Void::Testing::expect(!bad_end.has_value());
|
||||||
|
Void::Testing::expect(bad_end.error().code() ==
|
||||||
|
Void::Binary::to_error_code(Void::Binary::BinaryErrorCode::unsupported_endian));
|
||||||
|
Void::Testing::expect_eq(reader.position(), saved_pos);
|
||||||
|
|
||||||
|
// Valid bool advances, invalid bool (0x02) leaves cursor untouched.
|
||||||
|
const auto b_true = reader.readBool();
|
||||||
|
Void::Testing::require(b_true.has_value());
|
||||||
|
Void::Testing::expect(*b_true);
|
||||||
|
const std::size_t before_bad_bool = reader.position();
|
||||||
|
const auto b_bad = reader.readBool();
|
||||||
|
Void::Testing::expect(!b_bad.has_value());
|
||||||
|
Void::Testing::expect(b_bad.error().code() ==
|
||||||
|
Void::Binary::to_error_code(Void::Binary::BinaryErrorCode::invalid_bool));
|
||||||
|
Void::Testing::expect_eq(reader.position(), before_bad_bool);
|
||||||
|
|
||||||
|
// Skip the invalid bool byte and verify transaction rollback/commit & floats.
|
||||||
|
Void::Testing::require(reader.skip(1).has_value());
|
||||||
|
{
|
||||||
|
Void::Binary::Reader::Transaction tx{reader};
|
||||||
|
const auto f1 = reader.readF32(std::endian::big);
|
||||||
|
Void::Testing::require(f1.has_value());
|
||||||
|
Void::Testing::expect_eq(*f1, 1.0F);
|
||||||
|
} // auto-rollback
|
||||||
|
Void::Testing::expect_eq(reader.position(), before_bad_bool + 1);
|
||||||
|
|
||||||
|
{
|
||||||
|
Void::Binary::Reader::Transaction tx{reader};
|
||||||
|
const auto f1 = reader.readF32(std::endian::big);
|
||||||
|
const auto d1 = reader.readF64(std::endian::little);
|
||||||
|
Void::Testing::require(f1.has_value() && d1.has_value());
|
||||||
|
Void::Testing::expect_eq(*f1, 1.0F);
|
||||||
|
Void::Testing::expect_eq(*d1, 3.0);
|
||||||
|
tx.commit();
|
||||||
|
}
|
||||||
|
Void::Testing::expect(reader.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test binary_reader_cumulative_decode_budget_across_children()
|
||||||
|
{
|
||||||
|
using Void::Binary::BinaryErrorCode;
|
||||||
|
const std::array bytes{
|
||||||
|
std::byte{1}, std::byte{2}, std::byte{3},
|
||||||
|
std::byte{4}, std::byte{5}, std::byte{6},
|
||||||
|
std::byte{7}, std::byte{8}};
|
||||||
|
Void::Binary::Reader root{bytes};
|
||||||
|
Void::Binary::DecodeBudget budget{
|
||||||
|
Void::Binary::DecodeLimits{.max_field_bytes = 5, .max_total_bytes = 6, .max_depth = 2}};
|
||||||
|
|
||||||
|
auto g1 = budget.enter_scope();
|
||||||
|
auto g2 = budget.enter_scope();
|
||||||
|
auto g3 = budget.enter_scope();
|
||||||
|
Void::Testing::require(g1.has_value() && g2.has_value());
|
||||||
|
Void::Testing::expect(!g3.has_value());
|
||||||
|
Void::Testing::expect(g3.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::depth_exceeded));
|
||||||
|
|
||||||
|
auto child = root.subreaderChecked(4, budget);
|
||||||
|
Void::Testing::require(child.has_value());
|
||||||
|
Void::Testing::expect_eq(budget.consumed_bytes(), std::size_t{4});
|
||||||
|
Void::Testing::expect_eq(budget.remaining_bytes(), std::size_t{2});
|
||||||
|
|
||||||
|
// Shared cumulative limit rejects child consuming past remaining total bytes.
|
||||||
|
const auto over_total = child->readBytesChecked(3, budget);
|
||||||
|
Void::Testing::expect(!over_total.has_value());
|
||||||
|
Void::Testing::expect(over_total.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::budget_exceeded));
|
||||||
|
Void::Testing::expect_eq(budget.consumed_bytes(), std::size_t{4});
|
||||||
|
|
||||||
|
// Truncated read in child fails with truncated BEFORE charging budget.
|
||||||
|
auto ok_slice = child->readBytesChecked(2, budget);
|
||||||
|
Void::Testing::require(ok_slice.has_value());
|
||||||
|
Void::Testing::expect_eq(budget.consumed_bytes(), std::size_t{6});
|
||||||
|
Void::Testing::expect(!root.subreaderChecked(3, budget).has_value());
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#include <Void/Binary/Reader.hpp>
|
||||||
|
#include <Void/Binary/Writer.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <bit>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
Void::Testing::Test binary_writer_roundtrip_and_golden_bytes()
|
||||||
|
{
|
||||||
|
std::array<std::byte, 32> buf{};
|
||||||
|
Void::Binary::Writer writer{buf};
|
||||||
|
|
||||||
|
Void::Testing::require(writer.writeBig<std::uint16_t>(0x1234).has_value());
|
||||||
|
Void::Testing::require(writer.writeLittle<std::uint32_t>(0x89ABCDEFu).has_value());
|
||||||
|
Void::Testing::require(writer.writeBool(false).has_value());
|
||||||
|
Void::Testing::require(writer.writeBool(true).has_value());
|
||||||
|
Void::Testing::require(writer.writeF32(-2.5F, std::endian::big).has_value());
|
||||||
|
Void::Testing::require(writer.writeF64(6.25, std::endian::little).has_value());
|
||||||
|
Void::Testing::require(writer.writeString("hi").has_value());
|
||||||
|
|
||||||
|
const auto written = writer.written();
|
||||||
|
Void::Testing::expect_eq(written.size(), std::size_t{2 + 4 + 1 + 1 + 4 + 8 + 2});
|
||||||
|
Void::Testing::expect(written[0] == std::byte{0x12});
|
||||||
|
Void::Testing::expect(written[1] == std::byte{0x34});
|
||||||
|
Void::Testing::expect(written[2] == std::byte{0xEF});
|
||||||
|
Void::Testing::expect(written[5] == std::byte{0x89});
|
||||||
|
|
||||||
|
Void::Binary::Reader reader{written};
|
||||||
|
Void::Testing::expect_eq(*reader.readBig<std::uint16_t>(), std::uint16_t{0x1234});
|
||||||
|
Void::Testing::expect_eq(*reader.readLittle<std::uint32_t>(), std::uint32_t{0x89ABCDEFu});
|
||||||
|
Void::Testing::expect(!*reader.readBool());
|
||||||
|
Void::Testing::expect(*reader.readBool());
|
||||||
|
Void::Testing::expect_eq(*reader.readF32(std::endian::big), -2.5F);
|
||||||
|
Void::Testing::expect_eq(*reader.readF64(std::endian::little), 6.25);
|
||||||
|
Void::Testing::expect_eq(*reader.readString(2), std::string{"hi"});
|
||||||
|
Void::Testing::expect(reader.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test binary_writer_failure_atomicity_overlap_and_rollback()
|
||||||
|
{
|
||||||
|
using Void::Binary::BinaryErrorCode;
|
||||||
|
|
||||||
|
std::array<std::byte, 6> buf{
|
||||||
|
std::byte{0xAA}, std::byte{0xBB}, std::byte{0xCC},
|
||||||
|
std::byte{0xDD}, std::byte{0xEE}, std::byte{0xFF}};
|
||||||
|
Void::Binary::Writer writer{buf};
|
||||||
|
|
||||||
|
// Overlapping source slice copied forward inside same backing array.
|
||||||
|
Void::Testing::require(
|
||||||
|
writer.writeBytes(std::span<const std::byte>{buf.data() + 1, 3}).has_value());
|
||||||
|
Void::Testing::expect_eq(writer.position(), std::size_t{3});
|
||||||
|
Void::Testing::expect(buf[0] == std::byte{0xBB});
|
||||||
|
Void::Testing::expect(buf[1] == std::byte{0xCC});
|
||||||
|
Void::Testing::expect(buf[2] == std::byte{0xDD});
|
||||||
|
|
||||||
|
// Insufficient remaining capacity leaves position and untouched tail unchanged.
|
||||||
|
const auto cp = writer.checkpoint();
|
||||||
|
const auto full_err = writer.writeBig<std::uint32_t>(0x11223344u);
|
||||||
|
Void::Testing::expect(!full_err.has_value());
|
||||||
|
Void::Testing::expect(full_err.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::buffer_full));
|
||||||
|
Void::Testing::expect_eq(writer.position(), cp);
|
||||||
|
Void::Testing::expect(buf[3] == std::byte{0xDD});
|
||||||
|
|
||||||
|
// Unsupported endian fails without moving cursor or touching output.
|
||||||
|
const auto end_err =
|
||||||
|
writer.write<std::uint16_t>(0x1122, static_cast<std::endian>(77));
|
||||||
|
Void::Testing::expect(!end_err.has_value());
|
||||||
|
Void::Testing::expect(end_err.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::unsupported_endian));
|
||||||
|
Void::Testing::expect_eq(writer.position(), cp);
|
||||||
|
|
||||||
|
// Rollback position only; out-of-bounds seek returns out_of_bounds.
|
||||||
|
Void::Testing::require(writer.rollbackPosition(1).has_value());
|
||||||
|
Void::Testing::expect_eq(writer.position(), std::size_t{1});
|
||||||
|
const auto oob = writer.seek(99);
|
||||||
|
Void::Testing::expect(!oob.has_value());
|
||||||
|
Void::Testing::expect(oob.error().code() ==
|
||||||
|
Void::Binary::to_error_code(BinaryErrorCode::out_of_bounds));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user