| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <limits> |
| 8 |
#include <memory> |
| 9 |
#include <span> |
| 10 |
#include <string> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include "Common/CommonTypes.h" |
| 14 |
#include "Common/Crypto/AES.h" |
| 15 |
#include "Common/DirectIOFile.h" |
| 16 |
#include "DiscIO/Blob.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace DiscIO |
| 21 |
{ |
| 22 |
static constexpr u32 NFS_MAGIC = 0x53474745; |
| 23 |
|
| 24 |
struct NFSLBARange |
| 25 |
{ |
| 26 |
u32 start_block; |
| 27 |
u32 num_blocks; |
| 28 |
}; |
| 29 |
|
| 30 |
struct NFSHeader |
| 31 |
{ |
| 32 |
u32 magic; |
| 33 |
u32 version; |
| 34 |
u32 unknown_1; |
| 35 |
u32 unknown_2; |
| 36 |
u32 lba_range_count; |
| 37 |
std::array<NFSLBARange, 61> lba_ranges; |
| 38 |
u32 end_magic; |
| 39 |
}; |
| 40 |
static_assert(sizeof(NFSHeader) == 0x200); |
| 41 |
|
| 42 |
class NFSFileReader final : public BlobReader |
| 43 |
{ |
| 44 |
public: |
| 45 |
static std::unique_ptr<NFSFileReader> Create(File::DirectIOFile first_file, |
| 46 |
const std::string& directory_path); |
| 47 |
|
| 48 |
BlobType GetBlobType() const override { return BlobType::NFS; } |
| 49 |
std::unique_ptr<BlobReader> CopyReader() const override; |
| 50 |
|
| 51 |
u64 GetRawSize() const override; |
| 52 |
u64 GetDataSize() const override; |
| 53 |
DataSizeType GetDataSizeType() const override { return DataSizeType::LowerBound; } |
| 54 |
|
| 55 |
u64 GetBlockSize() const override { return BLOCK_SIZE; } |
| 56 |
bool HasFastRandomAccessInBlock() const override { return false; } |
| 57 |
std::string GetCompressionMethod() const override { return {}; } |
| 58 |
std::optional<int> GetCompressionLevel() const override { return std::nullopt; } |
| 59 |
|
| 60 |
bool Read(u64 offset, u64 nbytes, u8* out_ptr) override; |
| 61 |
|
| 62 |
private: |
| 63 |
using Key = std::array<u8, Common::AES::Context::KEY_SIZE>; |
| 64 |
static constexpr u32 BLOCK_SIZE = 0x8000; |
| 65 |
static constexpr u32 MAX_FILE_SIZE = 0xFA00000; |
| 66 |
|
| 67 |
static bool ReadKey(const std::string& path, const std::string& directory, Key* key_out); |
| 68 |
static std::vector<NFSLBARange> GetLBARanges(const NFSHeader& header); |
| 69 |
static std::vector<File::DirectIOFile> OpenFiles(const std::string& directory, |
| 70 |
File::DirectIOFile first_file, |
| 71 |
u64 expected_raw_size, u64* raw_size_out); |
| 72 |
static u64 CalculateExpectedRawSize(std::span<const NFSLBARange> lba_ranges); |
| 73 |
static u64 CalculateExpectedDataSize(std::span<const NFSLBARange> lba_ranges); |
| 74 |
|
| 75 |
NFSFileReader(std::vector<NFSLBARange> lba_ranges, std::vector<File::DirectIOFile> files, Key key, |
| 76 |
u64 raw_size); |
| 77 |
|
| 78 |
u64 ToPhysicalBlockIndex(u64 logical_block_index) const; |
| 79 |
bool ReadEncryptedBlock(u64 physical_block_index); |
| 80 |
void DecryptBlock(u64 logical_block_index); |
| 81 |
bool ReadAndDecryptBlock(u64 logical_block_index); |
| 82 |
|
| 83 |
std::array<u8, BLOCK_SIZE> m_current_block_encrypted; |
| 84 |
std::array<u8, BLOCK_SIZE> m_current_block_decrypted; |
| 85 |
u64 m_current_logical_block_index = std::numeric_limits<u64>::max(); |
| 86 |
|
| 87 |
std::vector<NFSLBARange> m_lba_ranges; |
| 88 |
std::vector<File::DirectIOFile> m_files; |
| 89 |
std::unique_ptr<Common::AES::Context> m_aes_context; |
| 90 |
u64 m_raw_size; |
| 91 |
u64 m_data_size; |
| 92 |
Key m_key; |
| 93 |
}; |
| 94 |
|
| 95 |
} |