Beautify code with new macro

This commit is contained in:
Pengfei
2021-07-16 21:59:32 +08:00
parent 55db5f110f
commit 4e619b3621
3 changed files with 21 additions and 33 deletions
+2
View File
@@ -87,3 +87,5 @@ constexpr u64 MakeMagic(char a, char b, char c, char d, char e, char f, char g,
return u64(a) | u64(b) << 8 | u64(c) << 16 | u64(d) << 24 | u64(e) << 32 | u64(f) << 40 |
u64(g) << 48 | u64(h) << 56;
}
#define TRY_MEMCPY(...) TRY(CheckedMemcpy(__VA_ARGS__), LOG_ERROR(Core, "File size is too small"))
+8 -15
View File
@@ -94,8 +94,7 @@ bool DataContainer::IsGood() const {
bool DataContainer::InitAsDISA() {
DISAHeader header;
TRY(CheckedMemcpy(&header, data, 0x100, sizeof(header)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&header, data, 0x100, sizeof(header));
if (header.version != 0x40000) {
LOG_ERROR(Core, "DISA Version {:x} is not correct", header.version);
@@ -123,8 +122,7 @@ bool DataContainer::InitAsDISA() {
bool DataContainer::InitAsDIFF() {
DIFFHeader header;
TRY(CheckedMemcpy(&header, data, 0x100, sizeof(header)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&header, data, 0x100, sizeof(header));
if (header.version != 0x30000) {
LOG_ERROR(Core, "DIFF Version {:x} is not correct", header.version);
@@ -148,8 +146,7 @@ bool DataContainer::GetPartitionData(std::vector<u8>& out, u8 index) const {
auto partition_descriptor_offset = partition_table_offset + partition_descriptors[index].offset;
DIFIHeader difi;
TRY(CheckedMemcpy(&difi, data, partition_descriptor_offset, sizeof(difi)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&difi, data, partition_descriptor_offset, sizeof(difi));
if (difi.magic != MakeMagic('D', 'I', 'F', 'I') || difi.version != 0x10000) {
LOG_ERROR(Core, "Invalid magic {:08x} or version {}", difi.magic, difi.version);
@@ -158,9 +155,8 @@ bool DataContainer::GetPartitionData(std::vector<u8>& out, u8 index) const {
ASSERT_MSG(difi.ivfc.size >= sizeof(IVFCDescriptor), "IVFC descriptor size is too small");
IVFCDescriptor ivfc_descriptor;
TRY(CheckedMemcpy(&ivfc_descriptor, data, partition_descriptor_offset + difi.ivfc.offset,
sizeof(ivfc_descriptor)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&ivfc_descriptor, data, partition_descriptor_offset + difi.ivfc.offset,
sizeof(ivfc_descriptor));
if (difi.enable_external_IVFC_level_4) {
if (data.size() < partitions[index].offset + difi.external_IVFC_level_4_offset +
@@ -178,14 +174,11 @@ bool DataContainer::GetPartitionData(std::vector<u8>& out, u8 index) const {
// Unwrap DPFS Tree
ASSERT_MSG(difi.dpfs.size >= sizeof(DPFSDescriptor), "DPFS descriptor size is too small");
DPFSDescriptor dpfs_descriptor;
TRY(CheckedMemcpy(&dpfs_descriptor, data, partition_descriptor_offset + difi.dpfs.offset,
sizeof(dpfs_descriptor)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&dpfs_descriptor, data, partition_descriptor_offset + difi.dpfs.offset,
sizeof(dpfs_descriptor));
std::vector<u32_le> partition_data(partitions[index].size / 4);
TRY(CheckedMemcpy(partition_data.data(), data, partitions[index].offset,
partitions[index].size),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(partition_data.data(), data, partitions[index].offset, partitions[index].size);
DPFSContainer dpfs_container(std::move(dpfs_descriptor), difi.dpfs_level1_selector,
std::move(partition_data));
+11 -18
View File
@@ -142,8 +142,7 @@ protected:
const auto& header_vector = partitions[0];
// Read header
TRY(CheckedMemcpy(&header, header_vector, 0, sizeof(header)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&header, header_vector, 0, sizeof(header));
if (!static_cast<const T*>(this)->CheckMagic()) {
LOG_ERROR(Core, "File is invalid, decryption errors may have happened.");
@@ -153,10 +152,9 @@ protected:
static constexpr std::size_t PreheaderSize = FullHeader<Preheader>::PreheaderSize;
// Read filesystem information
TRY(CheckedMemcpy(&fs_info, header_vector,
PreheaderSize + header.fat_header.filesystem_information_offset,
sizeof(fs_info)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(&fs_info, header_vector,
PreheaderSize + header.fat_header.filesystem_information_offset,
sizeof(fs_info));
// Read data region
if (duplicate_data) {
@@ -189,9 +187,8 @@ protected:
static_cast<std::size_t>(fs_info.data_region_block_size)
: PreheaderSize + fs_info.directory_entry_table.non_duplicate;
TRY(CheckedMemcpy(directory_entry_table.data(), header_vector, directory_entry_table_pos,
directory_entry_table.size() * sizeof(DirectoryEntryType)),
LOG_ERROR(Core, "File is too small"));
TRY_MEMCPY(directory_entry_table.data(), header_vector, directory_entry_table_pos,
directory_entry_table.size() * sizeof(DirectoryEntryType));
// Read file entry table
file_entry_table.resize(fs_info.maximum_file_count + 1); // including head
@@ -202,16 +199,13 @@ protected:
static_cast<std::size_t>(fs_info.data_region_block_size)
: PreheaderSize + fs_info.file_entry_table.non_duplicate;
TRY(CheckedMemcpy(file_entry_table.data(), header_vector, file_entry_table_pos,
file_entry_table.size() * sizeof(FileEntryType)),
LOG_ERROR(Core, "File is too small"));
TRY_MEMCPY(file_entry_table.data(), header_vector, file_entry_table_pos,
file_entry_table.size() * sizeof(FileEntryType));
// Read file allocation table
fat.resize(fs_info.file_allocation_table_entry_count);
TRY(CheckedMemcpy(fat.data(), header_vector,
PreheaderSize + fs_info.file_allocation_table_offset,
fat.size() * sizeof(FATNode)),
LOG_ERROR(Core, "File size is too small"));
TRY_MEMCPY(fat.data(), header_vector, PreheaderSize + fs_info.file_allocation_table_offset,
fat.size() * sizeof(FATNode));
return true;
}
@@ -251,8 +245,7 @@ protected:
static_cast<std::size_t>(fs_info.data_region_block_size) * (last_block - block + 1);
const auto to_write = std::min<std::size_t>(file_size, size);
TRY(CheckedMemcpy(out.data() + written, data_region, offset, to_write),
LOG_ERROR(Core, "File data out of bound"));
TRY_MEMCPY(out.data() + written, data_region, offset, to_write);
file_size -= to_write;
written += to_write;