mirror of
https://github.com/mhx/dwarfs.git
synced 2025-08-03 17:56:12 -04:00
refactor(fstypes): use std::array
for magic / SHA2 members
This commit is contained in:
parent
37881a8f90
commit
7efa82854a
@ -21,11 +21,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <iosfwd>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include <dwarfs/block_compressor.h> // 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<char, 6> 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<char, 6> magic; // [0] "DWARFS" / file_header no longer needed
|
||||
uint8_t major; // [6] major version
|
||||
uint8_t minor; // [7] minor version
|
||||
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
|
||||
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;
|
||||
|
@ -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<void>(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<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 {
|
||||
return std::vector<uint8_t>(hdr_.sha2_512_256,
|
||||
hdr_.sha2_512_256 + sizeof(hdr_.sha2_512_256));
|
||||
return std::vector<uint8_t>(hdr_.sha2_512_256.begin(),
|
||||
hdr_.sha2_512_256.end());
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -121,7 +121,7 @@ filesystem_parser::filesystem_parser(std::shared_ptr<mmif> mm,
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
|
@ -500,7 +500,7 @@ void fsblock::build_section_header(section_header_v2& sh,
|
||||
std::optional<fs_section> 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
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user