diff --git a/include/dwarfs/fstypes.h b/include/dwarfs/fstypes.h index 7171c02d..18e513b6 100644 --- a/include/dwarfs/fstypes.h +++ b/include/dwarfs/fstypes.h @@ -21,11 +21,13 @@ #pragma once +#include #include #include #include #include #include +#include #include #include // TODO: or the other way round? @@ -54,9 +56,13 @@ enum class section_type : uint16_t { }; struct file_header { - char magic[6]; // "DWARFS" - uint8_t major; // major version - uint8_t minor; // minor version + std::array magic; // "DWARFS" + uint8_t major; // major version + uint8_t minor; // minor version + + std::string_view magic_sv() const { + return std::string_view(magic.data(), magic.size()); + } }; struct section_header { @@ -70,15 +76,16 @@ struct section_header { }; struct section_header_v2 { - char magic[6]; // [0] "DWARFS" / file_header no longer needed - uint8_t major; // [6] major version - uint8_t minor; // [7] minor version - uint8_t sha2_512_256[32]; // [8] SHA2-512/256 starting from next field - uint64_t xxh3_64; // [40] XXH3-64 starting from next field - uint32_t number; // [48] section number - uint16_t type; // [52] section type - uint16_t compression; // [54] compression - uint64_t length; // [56] length of section + std::array magic; // [0] "DWARFS" / file_header no longer needed + uint8_t major; // [6] major version + uint8_t minor; // [7] minor version + std::array // [8] SHA2-512/256 starting from next field + sha2_512_256; // + uint64_t xxh3_64; // [40] XXH3-64 starting from next field + uint32_t number; // [48] section number + uint16_t type; // [52] section type + uint16_t compression; // [54] compression + uint64_t length; // [56] length of section std::string to_string() const; void dump(std::ostream& os) const; diff --git a/src/internal/fs_section.cpp b/src/internal/fs_section.cpp index fdd5ff70..51d104c7 100644 --- a/src/internal/fs_section.cpp +++ b/src/internal/fs_section.cpp @@ -204,8 +204,8 @@ class fs_section_v2 final : public fs_section::impl { sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64); return checksum::verify(checksum::algorithm::SHA2_512_256, mm.as(start_ - hdr_sha_len), - hdr_.length + hdr_sha_len, &hdr_.sha2_512_256, - sizeof(hdr_.sha2_512_256)); + hdr_.length + hdr_sha_len, hdr_.sha2_512_256.data(), + hdr_.sha2_512_256.size()); } std::span data(mmif const& mm) const override { @@ -221,8 +221,8 @@ class fs_section_v2 final : public fs_section::impl { } std::optional> sha2_512_256_value() const override { - return std::vector(hdr_.sha2_512_256, - hdr_.sha2_512_256 + sizeof(hdr_.sha2_512_256)); + return std::vector(hdr_.sha2_512_256.begin(), + hdr_.sha2_512_256.end()); } private: diff --git a/src/reader/internal/filesystem_parser.cpp b/src/reader/internal/filesystem_parser.cpp index 2167b8d1..af106c10 100644 --- a/src/reader/internal/filesystem_parser.cpp +++ b/src/reader/internal/filesystem_parser.cpp @@ -121,7 +121,7 @@ filesystem_parser::filesystem_parser(std::shared_ptr mm, auto fh = mm_->as(image_offset_); - if (::memcmp(&fh->magic[0], "DWARFS", 6) != 0) { + if (fh->magic_sv() != "DWARFS") { DWARFS_THROW(runtime_error, "magic not found"); } diff --git a/src/writer/filesystem_writer.cpp b/src/writer/filesystem_writer.cpp index 8805d24d..bd35c5a5 100644 --- a/src/writer/filesystem_writer.cpp +++ b/src/writer/filesystem_writer.cpp @@ -500,7 +500,7 @@ void fsblock::build_section_header(section_header_v2& sh, std::optional const& sec) { auto range = fsb.data(); - ::memcpy(&sh.magic[0], "DWARFS", 6); + ::memcpy(sh.magic.data(), "DWARFS", 6); sh.major = MAJOR_VERSION; sh.minor = MINOR_VERSION; 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 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(); - std::copy(sha->begin(), sha->end(), &sh.sha2_512_256[0]); + std::copy(sha->begin(), sha->end(), sh.sha2_512_256.data()); return; } } @@ -538,7 +538,8 @@ void fsblock::build_section_header(section_header_v2& sh, sha.update(&sh.xxh3_64, sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64)); 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 diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index c3850973..a5fec1dc 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -207,7 +207,7 @@ std::string valid_v1_header() { std::string valid_v2_header(uint32_t section_number = 0) { section_header_v2 hdr; std::memset(&hdr, 0, sizeof(hdr)); - std::memcpy(hdr.magic, "DWARFS", 6); + std::memcpy(hdr.magic.data(), "DWARFS", 6); hdr.major = 2; hdr.minor = 3; hdr.number = section_number;