refactor(fstypes): use std::array for magic / SHA2 members

This commit is contained in:
Marcus Holland-Moritz 2025-03-16 11:49:22 +01:00
parent 37881a8f90
commit 7efa82854a
5 changed files with 30 additions and 22 deletions

View File

@ -21,11 +21,13 @@
#pragma once #pragma once
#include <array>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <iosfwd> #include <iosfwd>
#include <optional> #include <optional>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include <dwarfs/block_compressor.h> // TODO: or the other way round? #include <dwarfs/block_compressor.h> // TODO: or the other way round?
@ -54,9 +56,13 @@ enum class section_type : uint16_t {
}; };
struct file_header { struct file_header {
char magic[6]; // "DWARFS" std::array<char, 6> magic; // "DWARFS"
uint8_t major; // major version uint8_t major; // major version
uint8_t minor; // minor version uint8_t minor; // minor version
std::string_view magic_sv() const {
return std::string_view(magic.data(), magic.size());
}
}; };
struct section_header { struct section_header {
@ -70,10 +76,11 @@ struct section_header {
}; };
struct section_header_v2 { struct section_header_v2 {
char magic[6]; // [0] "DWARFS" / file_header no longer needed std::array<char, 6> magic; // [0] "DWARFS" / file_header no longer needed
uint8_t major; // [6] major version uint8_t major; // [6] major version
uint8_t minor; // [7] minor version uint8_t minor; // [7] minor version
uint8_t sha2_512_256[32]; // [8] SHA2-512/256 starting from next field std::array<uint8_t, 32> // [8] SHA2-512/256 starting from next field
sha2_512_256; //
uint64_t xxh3_64; // [40] XXH3-64 starting from next field uint64_t xxh3_64; // [40] XXH3-64 starting from next field
uint32_t number; // [48] section number uint32_t number; // [48] section number
uint16_t type; // [52] section type uint16_t type; // [52] section type

View File

@ -204,8 +204,8 @@ class fs_section_v2 final : public fs_section::impl {
sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64); sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64);
return checksum::verify(checksum::algorithm::SHA2_512_256, return checksum::verify(checksum::algorithm::SHA2_512_256,
mm.as<void>(start_ - hdr_sha_len), mm.as<void>(start_ - hdr_sha_len),
hdr_.length + hdr_sha_len, &hdr_.sha2_512_256, hdr_.length + hdr_sha_len, hdr_.sha2_512_256.data(),
sizeof(hdr_.sha2_512_256)); hdr_.sha2_512_256.size());
} }
std::span<uint8_t const> data(mmif const& mm) const override { std::span<uint8_t const> data(mmif const& mm) const override {
@ -221,8 +221,8 @@ class fs_section_v2 final : public fs_section::impl {
} }
std::optional<std::vector<uint8_t>> sha2_512_256_value() const override { std::optional<std::vector<uint8_t>> sha2_512_256_value() const override {
return std::vector<uint8_t>(hdr_.sha2_512_256, return std::vector<uint8_t>(hdr_.sha2_512_256.begin(),
hdr_.sha2_512_256 + sizeof(hdr_.sha2_512_256)); hdr_.sha2_512_256.end());
} }
private: private:

View File

@ -121,7 +121,7 @@ filesystem_parser::filesystem_parser(std::shared_ptr<mmif> mm,
auto fh = mm_->as<file_header>(image_offset_); auto fh = mm_->as<file_header>(image_offset_);
if (::memcmp(&fh->magic[0], "DWARFS", 6) != 0) { if (fh->magic_sv() != "DWARFS") {
DWARFS_THROW(runtime_error, "magic not found"); DWARFS_THROW(runtime_error, "magic not found");
} }

View File

@ -500,7 +500,7 @@ void fsblock::build_section_header(section_header_v2& sh,
std::optional<fs_section> const& sec) { std::optional<fs_section> const& sec) {
auto range = fsb.data(); auto range = fsb.data();
::memcpy(&sh.magic[0], "DWARFS", 6); ::memcpy(sh.magic.data(), "DWARFS", 6);
sh.major = MAJOR_VERSION; sh.major = MAJOR_VERSION;
sh.minor = MINOR_VERSION; sh.minor = MINOR_VERSION;
sh.number = fsb.block_no(); sh.number = fsb.block_no();
@ -520,9 +520,9 @@ void fsblock::build_section_header(section_header_v2& sh,
auto xxh = sec->xxh3_64_value(); auto xxh = sec->xxh3_64_value();
auto sha = sec->sha2_512_256_value(); auto sha = sec->sha2_512_256_value();
if (xxh && sha && sha->size() == sizeof(sh.sha2_512_256)) { if (xxh && sha && sha->size() == sh.sha2_512_256.size()) {
sh.xxh3_64 = xxh.value(); sh.xxh3_64 = xxh.value();
std::copy(sha->begin(), sha->end(), &sh.sha2_512_256[0]); std::copy(sha->begin(), sha->end(), sh.sha2_512_256.data());
return; return;
} }
} }
@ -538,7 +538,8 @@ void fsblock::build_section_header(section_header_v2& sh,
sha.update(&sh.xxh3_64, sha.update(&sh.xxh3_64,
sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64)); sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64));
sha.update(range.data(), range.size()); sha.update(range.data(), range.size());
DWARFS_CHECK(sha.finalize(&sh.sha2_512_256), "SHA512/256 checksum failed"); DWARFS_CHECK(sha.finalize(sh.sha2_512_256.data()),
"SHA512/256 checksum failed");
} }
} // namespace } // namespace

View File

@ -207,7 +207,7 @@ std::string valid_v1_header() {
std::string valid_v2_header(uint32_t section_number = 0) { std::string valid_v2_header(uint32_t section_number = 0) {
section_header_v2 hdr; section_header_v2 hdr;
std::memset(&hdr, 0, sizeof(hdr)); std::memset(&hdr, 0, sizeof(hdr));
std::memcpy(hdr.magic, "DWARFS", 6); std::memcpy(hdr.magic.data(), "DWARFS", 6);
hdr.major = 2; hdr.major = 2;
hdr.minor = 3; hdr.minor = 3;
hdr.number = section_number; hdr.number = section_number;