From 4e619b3621a4192472c12b810c0690d136b9f0cd Mon Sep 17 00:00:00 2001 From: Pengfei Date: Fri, 16 Jul 2021 21:59:32 +0800 Subject: [PATCH] Beautify code with new macro --- src/common/common_funcs.h | 2 ++ src/core/data_container.cpp | 23 ++++++++--------------- src/core/inner_fat.hpp | 29 +++++++++++------------------ 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index f3ae251..63f72f1 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -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")) diff --git a/src/core/data_container.cpp b/src/core/data_container.cpp index 7d18819..fbe2028 100644 --- a/src/core/data_container.cpp +++ b/src/core/data_container.cpp @@ -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& 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& 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& 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 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)); diff --git a/src/core/inner_fat.hpp b/src/core/inner_fat.hpp index ac068be..6ae1b70 100644 --- a/src/core/inner_fat.hpp +++ b/src/core/inner_fat.hpp @@ -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(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::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(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(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(fs_info.data_region_block_size) * (last_block - block + 1); const auto to_write = std::min(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;