feat(crypto,archive): implement VL-007 built-in hashes and VL-006 archive creation/gzip/tar fallback

This commit is contained in:
faumaray committed 2026-11-26 20:55:45 +03:00
1 parent b50131f52e
commit 22c8538754
19 files changed
+3326 -133

No files matched your search

+101 -14
View File
@@ -3,8 +3,10 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <array>
#include <atomic>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <memory>
@@ -13,23 +15,95 @@
#include <string_view>
#include <system_error>
#include <thread>
#include <vector>
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
#include <archive.h>
#include <archive_entry.h>
#endif
namespace
{
enum class FixtureEntryType { Regular, Directory, Symlink };
struct ArchiveEntry {
std::string_view path;
std::string_view contents;
unsigned int type{AE_IFREG};
FixtureEntryType type{FixtureEntryType::Regular};
std::string_view link;
};
void write_archive(const std::filesystem::path &path,
std::span<const ArchiveEntry>
entries)
void write_octal(std::span<char> dst, std::uint64_t val)
{
dst.back() = '\0';
std::size_t pos = dst.size() - 1;
while (pos > 0) {
--pos;
dst[pos] = static_cast<char>('0' + (val & 7ULL));
val >>= 3ULL;
}
}
void write_raw_tar(const std::filesystem::path& path,
std::span<const ArchiveEntry> entries)
{
std::ofstream out{path, std::ios::binary | std::ios::trunc};
Void::Testing::require(out.is_open());
for (const auto& spec : entries) {
std::array<char, 512> hdr{};
std::memcpy(hdr.data(), spec.path.data(),
std::min<std::size_t>(spec.path.size(), 100));
const std::uint32_t perm =
spec.type == FixtureEntryType::Directory ? 0755U : 0644U;
write_octal(std::span{hdr}.subspan(100, 8), perm);
write_octal(std::span{hdr}.subspan(108, 8), 0);
write_octal(std::span{hdr}.subspan(116, 8), 0);
const std::uint64_t sz =
spec.type == FixtureEntryType::Regular ? spec.contents.size()
: 0ULL;
write_octal(std::span{hdr}.subspan(124, 12), sz);
write_octal(std::span{hdr}.subspan(136, 12), 0);
std::fill(hdr.begin() + 148, hdr.begin() + 156, ' ');
hdr[156] = spec.type == FixtureEntryType::Directory
? '5'
: (spec.type == FixtureEntryType::Symlink ? '2' : '0');
if (!spec.link.empty()) {
std::memcpy(hdr.data() + 157, spec.link.data(),
std::min<std::size_t>(spec.link.size(), 100));
}
std::memcpy(hdr.data() + 257, "ustar\0", 6);
hdr[263] = '0';
hdr[264] = '0';
std::uint32_t sum = 0;
for (const char ch : hdr) {
sum += static_cast<unsigned char>(ch);
}
write_octal(std::span{hdr}.subspan(148, 7), sum);
hdr[155] = ' ';
out.write(hdr.data(), hdr.size());
if (spec.type == FixtureEntryType::Regular && !spec.contents.empty()) {
out.write(spec.contents.data(),
static_cast<std::streamsize>(spec.contents.size()));
const std::size_t rem = spec.contents.size() % 512U;
if (rem != 0) {
std::array<char, 512> pad{};
out.write(pad.data(),
static_cast<std::streamsize>(512U - rem));
}
}
}
std::array<char, 1024> eof{};
out.write(eof.data(), eof.size());
out.close();
Void::Testing::require(out.good());
}
void write_archive(const std::filesystem::path& path,
std::span<const ArchiveEntry> entries)
{
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
using Writer = std::unique_ptr<archive, decltype(&archive_write_free)>;
using Entry =
std::unique_ptr<archive_entry, decltype(&archive_entry_free)>;
@@ -47,14 +121,21 @@ namespace
writer.get(), path.c_str()) == ARCHIVE_OK);
#endif
for (const auto &specification : entries) {
for (const auto& specification : entries) {
Entry entry{archive_entry_new(), &archive_entry_free};
const std::string entryPath{specification.path};
archive_entry_set_pathname(entry.get(), entryPath.c_str());
archive_entry_set_filetype(entry.get(), specification.type);
const unsigned int ae_type =
specification.type == FixtureEntryType::Directory
? AE_IFDIR
: (specification.type == FixtureEntryType::Symlink
? AE_IFLNK
: AE_IFREG);
archive_entry_set_filetype(entry.get(), ae_type);
archive_entry_set_perm(
entry.get(), specification.type == AE_IFDIR ? 0755 : 0644);
if (specification.type == AE_IFREG) {
entry.get(),
specification.type == FixtureEntryType::Directory ? 0755 : 0644);
if (specification.type == FixtureEntryType::Regular) {
archive_entry_set_size(
entry.get(),
static_cast<la_int64_t>(specification.contents.size()));
@@ -78,18 +159,22 @@ namespace
ARCHIVE_OK);
}
Void::Testing::require(archive_write_close(writer.get()) == ARCHIVE_OK);
#else
write_raw_tar(path, entries);
#endif
}
void write_archive(const std::filesystem::path &path,
void write_archive(const std::filesystem::path& path,
std::string_view entryPath, std::string_view contents,
unsigned int type = AE_IFREG, std::string_view link = {})
FixtureEntryType type = FixtureEntryType::Regular,
std::string_view link = {})
{
const std::array entries{ArchiveEntry{entryPath, contents, type, link}};
write_archive(path, entries);
}
[[nodiscard]] bool
extraction_file_visible(const std::filesystem::path &destination)
extraction_file_visible(const std::filesystem::path& destination)
{
std::error_code error;
if (std::filesystem::exists(destination / "anchor", error)) {
@@ -129,8 +214,9 @@ Void::Testing::Test archive_accepts_explicit_directory_entries()
Void::FileSystem::TempDir temporary{"void_archive_"};
const auto source = temporary / "directory.tar.zst";
const auto destination = temporary / "output";
const std::array entries{ArchiveEntry{"bin/", {}, AE_IFDIR},
ArchiveEntry{"bin/tool", "payload"}};
const std::array entries{
ArchiveEntry{"bin/", {}, FixtureEntryType::Directory},
ArchiveEntry{"bin/tool", "payload"}};
write_archive(source, entries);
auto extracted = Void::Archive::extract(source, destination);
@@ -154,7 +240,8 @@ Void::Testing::Test archive_rejects_traversal_and_links()
Void::Testing::expect(
!std::filesystem::exists(temporary.path() / "escape"));
const auto symlink = temporary / "symlink.tar.zst";
write_archive(symlink, "link", {}, AE_IFLNK, "/tmp/escape");
write_archive(symlink, "link", {}, FixtureEntryType::Symlink,
"/tmp/escape");
Void::Testing::expect(
!Void::Archive::extract(symlink, temporary / "out2").has_value());
const auto trailingFile = temporary / "trailing-file.tar.zst";