144 lines
4.0 KiB
C++
144 lines
4.0 KiB
C++
#include "reloadable_host/checkpoint.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace ReloadableHost
|
|
{
|
|
namespace
|
|
{
|
|
constexpr std::array<std::byte, 4> kMagic{
|
|
std::byte{'V'}, std::byte{'C'}, std::byte{'H'}, std::byte{'K'}};
|
|
|
|
[[nodiscard]] Void::Result::Error invalid(std::string message)
|
|
{
|
|
return Void::Result::Error{Void::Result::ErrorKind::corrupt,
|
|
std::move(message)};
|
|
}
|
|
|
|
void append_u32(std::vector<std::byte>& out, std::uint32_t value)
|
|
{
|
|
for (int shift = 24; shift >= 0; shift -= 8) {
|
|
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
|
|
}
|
|
}
|
|
|
|
void append_u64(std::vector<std::byte>& out, std::uint64_t value)
|
|
{
|
|
for (int shift = 56; shift >= 0; shift -= 8) {
|
|
out.push_back(static_cast<std::byte>((value >> shift) & 0xFFU));
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] bool read_u32(std::span<const std::byte> in,
|
|
std::size_t& pos,
|
|
std::uint32_t& value) noexcept
|
|
{
|
|
if (pos + 4 > in.size()) {
|
|
return false;
|
|
}
|
|
value = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
value = (value << 8) |
|
|
std::to_integer<std::uint32_t>(in[pos + i]);
|
|
}
|
|
pos += 4;
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] bool read_u64(std::span<const std::byte> in,
|
|
std::size_t& pos,
|
|
std::uint64_t& value) noexcept
|
|
{
|
|
if (pos + 8 > in.size()) {
|
|
return false;
|
|
}
|
|
value = 0;
|
|
for (int i = 0; i < 8; ++i) {
|
|
value = (value << 8) |
|
|
std::to_integer<std::uint64_t>(in[pos + i]);
|
|
}
|
|
pos += 8;
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
Void::Result::Result<std::vector<std::byte>>
|
|
encode_checkpoint(const CheckpointState& state, CheckpointLimits limits)
|
|
{
|
|
if (state.workflow.size() > limits.max_workflow_bytes ||
|
|
state.workflow.size() > 0xFFFFFFFFULL) {
|
|
return std::unexpected{
|
|
invalid("Checkpoint: workflow exceeds the size limit")};
|
|
}
|
|
std::vector<std::byte> out;
|
|
out.reserve(4 + 4 + 8 + 4 + state.workflow.size() + 8);
|
|
out.insert(out.end(), kMagic.begin(), kMagic.end());
|
|
append_u32(out, kCurrentCheckpointVersion);
|
|
append_u64(out, state.counter);
|
|
append_u32(out, static_cast<std::uint32_t>(state.workflow.size()));
|
|
for (const char ch : state.workflow) {
|
|
out.push_back(static_cast<std::byte>(ch));
|
|
}
|
|
append_u64(out, state.generation);
|
|
if (out.size() > limits.max_bytes) {
|
|
return std::unexpected{
|
|
invalid("Checkpoint: encoded state exceeds the size limit")};
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Void::Result::Result<CheckpointState>
|
|
decode_checkpoint(std::span<const std::byte> bytes,
|
|
CheckpointLimits limits)
|
|
{
|
|
if (bytes.size() > limits.max_bytes ||
|
|
bytes.size() < kMagic.size() + 4 + 8) {
|
|
return std::unexpected{
|
|
invalid("Checkpoint: truncated or oversized payload")};
|
|
}
|
|
if (!std::equal(kMagic.begin(), kMagic.end(), bytes.begin())) {
|
|
return std::unexpected{invalid("Checkpoint: bad magic")};
|
|
}
|
|
|
|
std::size_t pos = kMagic.size();
|
|
std::uint32_t version = 0;
|
|
if (!read_u32(bytes, pos, version)) {
|
|
return std::unexpected{invalid("Checkpoint: truncated version")};
|
|
}
|
|
if (version == 0 || version > kCurrentCheckpointVersion) {
|
|
return std::unexpected{invalid(
|
|
"Checkpoint: unsupported schema version")};
|
|
}
|
|
|
|
CheckpointState state;
|
|
if (!read_u64(bytes, pos, state.counter)) {
|
|
return std::unexpected{invalid("Checkpoint: truncated counter")};
|
|
}
|
|
if (version >= 2) {
|
|
std::uint32_t workflow_size = 0;
|
|
if (!read_u32(bytes, pos, workflow_size) ||
|
|
workflow_size > limits.max_workflow_bytes ||
|
|
pos + workflow_size > bytes.size()) {
|
|
return std::unexpected{
|
|
invalid("Checkpoint: invalid workflow payload")};
|
|
}
|
|
state.workflow.assign(
|
|
reinterpret_cast<const char*>(bytes.data() + pos),
|
|
workflow_size);
|
|
pos += workflow_size;
|
|
}
|
|
if (version >= 3 && !read_u64(bytes, pos, state.generation)) {
|
|
return std::unexpected{
|
|
invalid("Checkpoint: truncated generation")};
|
|
}
|
|
if (pos != bytes.size()) {
|
|
return std::unexpected{
|
|
invalid("Checkpoint: trailing bytes after payload")};
|
|
}
|
|
return state;
|
|
}
|
|
} // namespace ReloadableHost
|