feat: adopt VoidLibrary VL-001..VL-013, self-building build.cpp, live logs, and generated CLI help
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s

This commit is contained in:
faumaray committed 2026-11-27 01:43:06 +03:00
1 parent ab64147f9d
commit 363fb8200e
33 files changed
+1754 -3155

No files matched your search

+42 -156
View File
@@ -1,127 +1,45 @@
#include <Void/Build/Util/Sha256.hpp>
#include <Void/System/Types.hpp>
#include <Void/Text/Hex.hpp>
#include <algorithm>
#include <cstring>
#include <fcntl.h>
#include <format>
#include <unistd.h>
namespace Void::Build::Util {
namespace {
constexpr std::uint32_t kRoundConstants[64] = {
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu, 0x59f111f1u, 0x923f82a4u,
0xab1c5ed5u, 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu,
0x9bdc06a7u, 0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu, 0x2de92c6fu,
0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u, 0xa2bfe8a1u, 0xa81a664bu,
0xc24b8b70u, 0xc76c51a3u, 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u,
0x1e376c08u, 0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u, 0x90befffau, 0xa4506cebu, 0xbef9a3f7u,
0xc67178f2u};
constexpr std::uint32_t rotr(std::uint32_t x, unsigned n) noexcept {
return (x >> n) | (x << (32u - n));
Sha256Digest copy_digest(const Void::Crypto::Digest &src) noexcept {
Sha256Digest out{};
if (src.size() == out.size()) {
std::memcpy(out.data(), src.data(), out.size());
}
return out;
}
} // namespace
Sha256Hasher::Sha256Hasher() noexcept
: state_{0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au,
0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u} {}
void Sha256Hasher::transform(const std::uint8_t block[64]) noexcept {
std::uint32_t w[64];
for (int i = 0; i < 16; ++i) {
w[i] = (static_cast<std::uint32_t>(block[i * 4]) << 24) |
(static_cast<std::uint32_t>(block[i * 4 + 1]) << 16) |
(static_cast<std::uint32_t>(block[i * 4 + 2]) << 8) |
static_cast<std::uint32_t>(block[i * 4 + 3]);
}
for (int i = 16; i < 64; ++i) {
const std::uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
const std::uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
std::uint32_t a = state_[0];
std::uint32_t b = state_[1];
std::uint32_t c = state_[2];
std::uint32_t d = state_[3];
std::uint32_t e = state_[4];
std::uint32_t f = state_[5];
std::uint32_t g = state_[6];
std::uint32_t h = state_[7];
for (int i = 0; i < 64; ++i) {
const std::uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
const std::uint32_t ch = (e & f) ^ ((~e) & g);
const std::uint32_t temp1 = h + S1 + ch + kRoundConstants[i] + w[i];
const std::uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
const std::uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
const std::uint32_t temp2 = S0 + maj;
h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
state_[0] += a;
state_[1] += b;
state_[2] += c;
state_[3] += d;
state_[4] += e;
state_[5] += f;
state_[6] += g;
state_[7] += h;
}
void Sha256Hasher::update(std::span<const std::uint8_t> data) noexcept {
if (finalized_ || data.empty()) {
return;
}
const std::uint8_t *ptr = data.data();
std::size_t remaining = data.size();
bit_count_ += static_cast<std::uint64_t>(remaining) * 8u;
if (buffer_len_ > 0) {
const std::size_t take = std::min(remaining, std::size_t{64} - buffer_len_);
std::memcpy(buffer_ + buffer_len_, ptr, take);
buffer_len_ += take;
ptr += take;
remaining -= take;
if (buffer_len_ == 64) {
transform(buffer_);
buffer_len_ = 0;
}
}
while (remaining >= 64) {
transform(ptr);
ptr += 64;
remaining -= 64;
}
if (remaining > 0) {
std::memcpy(buffer_, ptr, remaining);
buffer_len_ = remaining;
Sha256Hasher::Sha256Hasher() {
if (auto h = Void::Crypto::Hasher::create(Void::Crypto::HashAlgorithm::Sha256); h) {
hasher_.emplace(std::move(*h));
}
}
void Sha256Hasher::update(std::span<const std::byte> data) noexcept {
update(std::span<const std::uint8_t>{reinterpret_cast<const std::uint8_t *>(data.data()),
data.size()});
if (finalized_ || !hasher_ || data.empty()) {
return;
}
(void)hasher_->update(data);
}
void Sha256Hasher::update(std::span<const std::uint8_t> data) noexcept {
update(std::as_bytes(data));
}
void Sha256Hasher::update(std::string_view text) noexcept {
update(std::span<const std::uint8_t>{reinterpret_cast<const std::uint8_t *>(text.data()),
text.size()});
if (finalized_ || !hasher_ || text.empty()) {
return;
}
(void)hasher_->update(text);
}
void Sha256Hasher::update_u64(std::uint64_t value) noexcept {
@@ -141,31 +59,14 @@ void Sha256Hasher::update_length_prefixed(std::string_view text) noexcept {
Sha256Digest Sha256Hasher::finalize() noexcept {
if (!finalized_) {
finalized_ = true;
buffer_[buffer_len_++] = 0x80u;
if (buffer_len_ > 56) {
std::memset(buffer_ + buffer_len_, 0, 64 - buffer_len_);
transform(buffer_);
buffer_len_ = 0;
if (hasher_) {
if (auto d = hasher_->finalize(); d) {
cached_digest_ = copy_digest(*d);
}
hasher_.reset();
}
std::memset(buffer_ + buffer_len_, 0, 56 - buffer_len_);
std::uint64_t bits = bit_count_;
for (int i = 7; i >= 0; --i) {
buffer_[56 + i] = static_cast<std::uint8_t>(bits & 0xffu);
bits >>= 8;
}
transform(buffer_);
}
Sha256Digest out{};
for (int i = 0; i < 8; ++i) {
out[static_cast<std::size_t>(i * 4)] = static_cast<std::uint8_t>((state_[i] >> 24) & 0xffu);
out[static_cast<std::size_t>(i * 4 + 1)] =
static_cast<std::uint8_t>((state_[i] >> 16) & 0xffu);
out[static_cast<std::size_t>(i * 4 + 2)] =
static_cast<std::uint8_t>((state_[i] >> 8) & 0xffu);
out[static_cast<std::size_t>(i * 4 + 3)] = static_cast<std::uint8_t>(state_[i] & 0xffu);
}
return out;
return cached_digest_;
}
std::string Sha256Hasher::finalize_hex() noexcept { return to_hex(finalize()); }
@@ -175,45 +76,30 @@ std::string to_hex(const Sha256Digest &digest) {
}
Sha256Digest sha256_bytes(std::span<const std::byte> data) noexcept {
Sha256Hasher hasher;
hasher.update(data);
return hasher.finalize();
if (auto d = Void::Crypto::sha256(data); d) {
return copy_digest(*d);
}
return Sha256Digest{};
}
std::string sha256_hex(std::string_view text) {
Sha256Hasher hasher;
hasher.update(text);
return hasher.finalize_hex();
if (auto d = Void::Crypto::sha256(text); d) {
return Void::Encoding::Hex::encode(std::span<const std::uint8_t>{
reinterpret_cast<const std::uint8_t *>(d->data()), d->size()});
}
return {};
}
std::string sha256_hex(std::span<const std::byte> data) { return to_hex(sha256_bytes(data)); }
Void::Result::Result<std::string, Void::Result::Error>
sha256_file_hex(const std::filesystem::path &path) {
const int fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) {
const int err = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(err),
std::format("sha256_file_hex: cannot open '{}'", path.string()), err)};
auto d = Void::Crypto::sha256_file(path);
if (!d) {
return std::unexpected{std::move(d.error())};
}
Void::System::OwnedFd guard{fd};
Sha256Hasher hasher;
std::array<std::uint8_t, 65536> buf{};
for (;;) {
const ssize_t n = ::read(fd, buf.data(), buf.size());
if (n > 0) {
hasher.update(std::span<const std::uint8_t>{buf.data(), static_cast<std::size_t>(n)});
} else if (n == 0) {
break;
} else if (errno != EINTR) {
const int err = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(err),
std::format("sha256_file_hex: read failed for '{}'", path.string()), err)};
}
}
return hasher.finalize_hex();
return Void::Encoding::Hex::encode(std::span<const std::uint8_t>{
reinterpret_cast<const std::uint8_t *>(d->data()), d->size()});
}
bool constant_time_equal(std::string_view a, std::string_view b) noexcept {