feat(Binary): add Writer, ByteBuffer, DecodeBudget, and explicit endian codecs (M-BINARY-02, M-BINARY-03)

This commit is contained in:
faumaray committed 2026-11-24 03:21:02 +03:00
1 parent 9ac76bbcf9
commit eacb8a4602
10 files changed
+1130 -130

No files matched your search

+31 -5
View File
@@ -1,4 +1,7 @@
#include <Void/Binary/ByteBuffer.hpp>
#include <Void/Binary/Reader.hpp>
#include <Void/Binary/Writer.hpp>
#include <Void/Memory/BudgetResource.hpp>
#include <array>
#include <bit>
@@ -7,8 +10,31 @@
int main()
{
const std::array bytes{std::byte{0x01}, std::byte{0x02}};
Void::Binary::Reader reader{bytes};
const auto value = reader.read<std::uint16_t>(std::endian::big);
return value && *value == 0x0102 ? 0 : 1;
}
std::array<std::byte, 16> scratch{};
Void::Binary::Writer writer{scratch};
if (!writer.writeBig<std::uint16_t>(0x0102) ||
!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;
}
+2 -2
View File
@@ -91,13 +91,13 @@ if(_has_memory)
run_consumer(MemoryOnly "${install_prefix}" "MemoryOnly" ${_no_external_deps})
endif()
has_modules(_has_binary Binary)
has_modules(_has_binary Binary Memory Functional Core)
if(_has_binary)
run_consumer(Binary "${install_prefix}" "Binary" ${_no_external_deps})
endif()
# 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)
run_consumer(Foundation "${install_prefix}" "Foundation" ${_no_external_deps})
endif()
+54
View File
@@ -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"});
}
+97
View File
@@ -29,3 +29,100 @@ Void::Testing::Test binary_reader_subreader_is_bounded()
Void::Testing::expect(!child->readBytes(1).has_value());
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());
}
+84
View File
@@ -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));
}