diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/file_sys/partition_filesystem.cpp | 29 | ||||
| -rw-r--r-- | src/core/file_sys/partition_filesystem.h | 10 |
2 files changed, 15 insertions, 24 deletions
diff --git a/src/core/file_sys/partition_filesystem.cpp b/src/core/file_sys/partition_filesystem.cpp index 15b1fb946..d4097a510 100644 --- a/src/core/file_sys/partition_filesystem.cpp +++ b/src/core/file_sys/partition_filesystem.cpp | |||
| @@ -11,6 +11,11 @@ | |||
| 11 | 11 | ||
| 12 | namespace FileSys { | 12 | namespace FileSys { |
| 13 | 13 | ||
| 14 | bool PartitionFilesystem::Header::HasValidMagicValue() const { | ||
| 15 | return magic == Common::MakeMagic('H', 'F', 'S', '0') || | ||
| 16 | magic == Common::MakeMagic('P', 'F', 'S', '0'); | ||
| 17 | } | ||
| 18 | |||
| 14 | PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { | 19 | PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { |
| 15 | // At least be as large as the header | 20 | // At least be as large as the header |
| 16 | if (file->GetSize() < sizeof(Header)) { | 21 | if (file->GetSize() < sizeof(Header)) { |
| @@ -20,19 +25,17 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { | |||
| 20 | 25 | ||
| 21 | // For cartridges, HFSs can get very large, so we need to calculate the size up to | 26 | // For cartridges, HFSs can get very large, so we need to calculate the size up to |
| 22 | // the actual content itself instead of just blindly reading in the entire file. | 27 | // the actual content itself instead of just blindly reading in the entire file. |
| 23 | Header pfs_header; | ||
| 24 | if (sizeof(Header) != file->ReadObject(&pfs_header)) { | 28 | if (sizeof(Header) != file->ReadObject(&pfs_header)) { |
| 25 | status = Loader::ResultStatus::Error; | 29 | status = Loader::ResultStatus::Error; |
| 26 | return; | 30 | return; |
| 27 | } | 31 | } |
| 28 | 32 | ||
| 29 | if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && | 33 | if (!pfs_header.HasValidMagicValue()) { |
| 30 | pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { | ||
| 31 | status = Loader::ResultStatus::ErrorInvalidFormat; | 34 | status = Loader::ResultStatus::ErrorInvalidFormat; |
| 32 | return; | 35 | return; |
| 33 | } | 36 | } |
| 34 | 37 | ||
| 35 | bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); | 38 | is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); |
| 36 | 39 | ||
| 37 | size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); | 40 | size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); |
| 38 | size_t metadata_size = | 41 | size_t metadata_size = |
| @@ -40,27 +43,13 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { | |||
| 40 | 43 | ||
| 41 | // Actually read in now... | 44 | // Actually read in now... |
| 42 | std::vector<u8> file_data = file->ReadBytes(metadata_size); | 45 | std::vector<u8> file_data = file->ReadBytes(metadata_size); |
| 46 | const size_t total_size = file_data.size(); | ||
| 43 | 47 | ||
| 44 | if (file_data.size() != metadata_size) { | 48 | if (total_size != metadata_size) { |
| 45 | status = Loader::ResultStatus::Error; | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | size_t total_size = file_data.size(); | ||
| 50 | if (total_size < sizeof(Header)) { | ||
| 51 | status = Loader::ResultStatus::Error; | 49 | status = Loader::ResultStatus::Error; |
| 52 | return; | 50 | return; |
| 53 | } | 51 | } |
| 54 | 52 | ||
| 55 | memcpy(&pfs_header, file_data.data(), sizeof(Header)); | ||
| 56 | if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && | ||
| 57 | pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) { | ||
| 58 | status = Loader::ResultStatus::ErrorInvalidFormat; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); | ||
| 63 | |||
| 64 | size_t entries_offset = sizeof(Header); | 53 | size_t entries_offset = sizeof(Header); |
| 65 | size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size); | 54 | size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size); |
| 66 | content_offset = strtab_offset + pfs_header.strtab_size; | 55 | content_offset = strtab_offset + pfs_header.strtab_size; |
diff --git a/src/core/file_sys/partition_filesystem.h b/src/core/file_sys/partition_filesystem.h index 9656b40bf..7c7a75816 100644 --- a/src/core/file_sys/partition_filesystem.h +++ b/src/core/file_sys/partition_filesystem.h | |||
| @@ -42,6 +42,8 @@ private: | |||
| 42 | u32_le num_entries; | 42 | u32_le num_entries; |
| 43 | u32_le strtab_size; | 43 | u32_le strtab_size; |
| 44 | INSERT_PADDING_BYTES(0x4); | 44 | INSERT_PADDING_BYTES(0x4); |
| 45 | |||
| 46 | bool HasValidMagicValue() const; | ||
| 45 | }; | 47 | }; |
| 46 | 48 | ||
| 47 | static_assert(sizeof(Header) == 0x10, "PFS/HFS header structure size is wrong"); | 49 | static_assert(sizeof(Header) == 0x10, "PFS/HFS header structure size is wrong"); |
| @@ -73,11 +75,11 @@ private: | |||
| 73 | 75 | ||
| 74 | #pragma pack(pop) | 76 | #pragma pack(pop) |
| 75 | 77 | ||
| 76 | Loader::ResultStatus status; | 78 | Loader::ResultStatus status{}; |
| 77 | 79 | ||
| 78 | Header pfs_header; | 80 | Header pfs_header{}; |
| 79 | bool is_hfs; | 81 | bool is_hfs = false; |
| 80 | size_t content_offset; | 82 | size_t content_offset = 0; |
| 81 | 83 | ||
| 82 | std::vector<VirtualFile> pfs_files; | 84 | std::vector<VirtualFile> pfs_files; |
| 83 | std::vector<VirtualDir> pfs_dirs; | 85 | std::vector<VirtualDir> pfs_dirs; |