Use references everywhere they can be used

This commit is contained in:
Macdu 2024-01-13 14:40:12 +01:00
parent 195367d47e
commit 4094450bca
17 changed files with 54 additions and 60 deletions

View File

@ -4,7 +4,7 @@
#include <fstream>
#include <regex>
F00DFileKeyEncryptor::F00DFileKeyEncryptor(psvpfs::path filePath)
F00DFileKeyEncryptor::F00DFileKeyEncryptor(const psvpfs::path& filePath)
: m_filePath(filePath), m_isCacheLoaded(false)
{
}

View File

@ -15,7 +15,7 @@ private:
bool m_isCacheLoaded;
public:
F00DFileKeyEncryptor(psvpfs::path filePath);
F00DFileKeyEncryptor(const psvpfs::path& filePath);
private:
int load_cache_flat_file();

View File

@ -168,13 +168,11 @@ bool FilesDbParser::verify_header_icv(std::ifstream& inputStream, const unsigned
bool FilesDbParser::get_isUnicv(bool& isUnicv)
{
psvpfs::path root(m_titleIdPath);
psvpfs::path filepath = root / "sce_pfs" / "unicv.db";
psvpfs::path filepath = m_titleIdPath / "sce_pfs" / "unicv.db";
if(!psvpfs::exists(filepath))
{
psvpfs::path filepath2 = root / "sce_pfs" / "icv.db";
psvpfs::path filepath2 = m_titleIdPath / "sce_pfs" / "icv.db";
if(!psvpfs::exists(filepath2) || !psvpfs::is_directory(filepath2))
{
m_output << "failed to find unicv.db file or icv.db folder" << std::endl;
@ -790,7 +788,7 @@ bool FilesDbParser::constructFilePaths(const std::map<std::uint32_t, std::uint32
}
//checks that directory exists
bool FilesDbParser::linkDirpaths(const std::set<psvpfs::path> real_directories)
bool FilesDbParser::linkDirpaths(const std::set<psvpfs::path>& real_directories)
{
m_output << "Linking dir paths..." << std::endl;
@ -826,7 +824,7 @@ bool FilesDbParser::linkDirpaths(const std::set<psvpfs::path> real_directories)
//checks that files exist
//checks that file size is correct
bool FilesDbParser::linkFilepaths(const std::set<psvpfs::path> real_files, std::uint32_t fileSectorSize)
bool FilesDbParser::linkFilepaths(const std::set<psvpfs::path>& real_files, std::uint32_t fileSectorSize)
{
m_output << "Linking file paths..." << std::endl;

View File

@ -245,9 +245,9 @@ private:
bool constructFilePaths(const std::map<std::uint32_t, std::uint32_t>& dirMatrix, const std::map<std::uint32_t, std::uint32_t>& fileMatrix, const std::vector<sce_ng_pfs_flat_block_t>& flatBlocks);
private:
bool linkDirpaths(const std::set<psvpfs::path> real_directories);
bool linkDirpaths(const std::set<psvpfs::path>& real_directories);
bool linkFilepaths(const std::set<psvpfs::path> real_files, std::uint32_t fileSectorSize);
bool linkFilepaths(const std::set<psvpfs::path>& real_files, std::uint32_t fileSectorSize);
int matchFileLists(const std::set<psvpfs::path>& files);

View File

@ -146,10 +146,9 @@ int check_keystone(std::shared_ptr<ICryptoOperations> cryptops, keystone_t& ks,
//public functions
int get_sealedkey(std::shared_ptr<ICryptoOperations> cryptops, psvpfs::path titleIdPath, unsigned char* dec_key)
int get_sealedkey(std::shared_ptr<ICryptoOperations> cryptops, const psvpfs::path& titleIdPath, unsigned char* dec_key)
{
psvpfs::path root(titleIdPath);
psvpfs::path filepath = root / "sce_sys" / "sealedkey";
psvpfs::path filepath = titleIdPath / "sce_sys" / "sealedkey";
if(!psvpfs::exists(filepath))
{
@ -170,10 +169,9 @@ int get_sealedkey(std::shared_ptr<ICryptoOperations> cryptops, psvpfs::path titl
return 0;
}
int get_keystone(std::shared_ptr<ICryptoOperations> cryptops, psvpfs::path titleIdPath, char* passcode)
int get_keystone(std::shared_ptr<ICryptoOperations> cryptops, const psvpfs::path& titleIdPath, char* passcode)
{
psvpfs::path root(titleIdPath);
psvpfs::path filepath = root / "sce_sys" / "keystone";
psvpfs::path filepath = titleIdPath / "sce_sys" / "keystone";
if(!psvpfs::exists(filepath))
{

View File

@ -40,6 +40,6 @@ typedef struct keystone_t
#pragma pack(pop)
int get_sealedkey(std::shared_ptr<ICryptoOperations> cryptops, psvpfs::path titleIdPath, unsigned char* dec_key);
int get_sealedkey(std::shared_ptr<ICryptoOperations> cryptops, const psvpfs::path& titleIdPath, unsigned char* dec_key);
int get_keystone(std::shared_ptr<ICryptoOperations> cryptops, psvpfs::path titleIdPath, char* passcode = 0);
int get_keystone(std::shared_ptr<ICryptoOperations> cryptops, const psvpfs::path& titleIdPath, char* passcode = 0);

View File

@ -4,7 +4,7 @@
#include "PfsKeyGenerator.h"
PfsFile::PfsFile(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output,
const unsigned char* klicensee, psvpfs::path titleIdPath,
const unsigned char* klicensee, const psvpfs::path& titleIdPath,
const sce_ng_pfs_file_t& file, const sce_junction& filepath, const sce_ng_pfs_header_t& ngpfs, std::shared_ptr<sce_iftbl_base_t> table)
: m_cryptops(cryptops), m_iF00D(iF00D), m_output(output), m_titleIdPath(titleIdPath),
m_file(file), m_filepath(filepath), m_ngpfs(ngpfs), m_table(table)
@ -128,7 +128,7 @@ int PfsFile::init_crypt_ctx(CryptEngineWorkCtx* work_ctx, sig_tbl_t& block, std:
return 0;
}
int PfsFile::decrypt_icv_file(psvpfs::path destination_root) const
int PfsFile::decrypt_icv_file(const psvpfs::path& destination_root) const
{
//create new file
@ -201,7 +201,7 @@ int PfsFile::decrypt_icv_file(psvpfs::path destination_root) const
return 0;
}
int PfsFile::decrypt_unicv_file(psvpfs::path destination_root) const
int PfsFile::decrypt_unicv_file(const psvpfs::path& destination_root) const
{
//create new file
@ -363,7 +363,7 @@ int PfsFile::decrypt_unicv_file(psvpfs::path destination_root) const
return 0;
}
int PfsFile::decrypt_file(psvpfs::path destination_root) const
int PfsFile::decrypt_file(const psvpfs::path& destination_root) const
{
if(img_spec_to_is_unicv(m_ngpfs.image_spec))
return decrypt_unicv_file(destination_root);

View File

@ -19,7 +19,7 @@ private:
std::shared_ptr<IF00DKeyEncryptor> m_iF00D;
std::ostream& m_output;
unsigned char m_klicensee[0x10];
psvpfs::path m_titleIdPath;
const psvpfs::path& m_titleIdPath;
private:
const sce_ng_pfs_file_t& m_file;
@ -34,16 +34,16 @@ private:
public:
PfsFile(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output,
const unsigned char* klicensee, psvpfs::path titleIdPath,
const unsigned char* klicensee, const psvpfs::path& titleIdPath,
const sce_ng_pfs_file_t& file, const sce_junction& filepath, const sce_ng_pfs_header_t& ngpfs, std::shared_ptr<sce_iftbl_base_t> table);
private:
int init_crypt_ctx(CryptEngineWorkCtx* work_ctx, sig_tbl_t& block, std::uint32_t sector_base, std::uint32_t tail_size, unsigned char* source) const;
int decrypt_icv_file(psvpfs::path destination_root) const;
int decrypt_icv_file(const psvpfs::path& destination_root) const;
int decrypt_unicv_file(psvpfs::path destination_root) const;
int decrypt_unicv_file(const psvpfs::path& destination_root) const;
public:
int decrypt_file(psvpfs::path destination_root) const;
int decrypt_file(const psvpfs::path& destination_root) const;
};

View File

@ -5,7 +5,7 @@
#include <cstring>
PfsFilesystem::PfsFilesystem(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output,
const unsigned char* klicensee, psvpfs::path titleIdPath)
const unsigned char* klicensee, const psvpfs::path& titleIdPath)
: m_cryptops(cryptops), m_iF00D(iF00D), m_output(output), m_titleIdPath(titleIdPath)
{
memcpy(m_klicensee, klicensee, 0x10);
@ -35,7 +35,7 @@ static void to_uppercase(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), static_cast<int (*)(int)>(std::toupper));
}
int PfsFilesystem::decrypt_files(psvpfs::path destTitleIdPath) const
int PfsFilesystem::decrypt_files(const psvpfs::path& destTitleIdPath) const
{
const sce_ng_pfs_header_t& ngpfs = m_filesDbParser->get_header();
const std::vector<sce_ng_pfs_file_t>& files = m_filesDbParser->get_files();

View File

@ -16,7 +16,7 @@ private:
std::shared_ptr<IF00DKeyEncryptor> m_iF00D;
std::ostream& m_output;
unsigned char m_klicensee[0x10];
psvpfs::path m_titleIdPath;
const psvpfs::path& m_titleIdPath;
private:
std::unique_ptr<FilesDbParser> m_filesDbParser;
@ -25,10 +25,10 @@ private:
public:
PfsFilesystem(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output,
const unsigned char* klicensee, psvpfs::path titleIdPath);
const unsigned char* klicensee, const psvpfs::path& titleIdPath);
public:
int mount();
int decrypt_files(psvpfs::path destTitleIdPath) const;
int decrypt_files(const psvpfs::path& destTitleIdPath) const;
};

View File

@ -4,7 +4,7 @@
#include "UnicvDbParser.h"
#include "FilesDbParser.h"
PfsPageMapper::PfsPageMapper(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output, const unsigned char* klicensee, psvpfs::path titleIdPath)
PfsPageMapper::PfsPageMapper(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output, const unsigned char* klicensee, const psvpfs::path& titleIdPath)
: m_cryptops(cryptops), m_iF00D(iF00D), m_output(output), m_titleIdPath(titleIdPath)
{
memcpy(m_klicensee, klicensee, 0x10);
@ -395,9 +395,9 @@ int PfsPageMapper::bruteforce_map(const std::unique_ptr<FilesDbParser>& filesDbP
}
//this is a test method that was supposed to be used for caching
int PfsPageMapper::load_page_map(psvpfs::path filepath, std::map<std::uint32_t, std::string>& pageMap) const
int PfsPageMapper::load_page_map(const psvpfs::path& filepath, std::map<std::uint32_t, std::string>& pageMap) const
{
psvpfs::path fp(filepath);
const auto& fp = filepath;
if(!psvpfs::exists(fp))
{

View File

@ -30,10 +30,10 @@ private:
std::shared_ptr<IF00DKeyEncryptor> m_iF00D;
std::ostream& m_output;
unsigned char m_klicensee[0x10];
psvpfs::path m_titleIdPath;
const psvpfs::path& m_titleIdPath;
public:
PfsPageMapper(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output, const unsigned char* klicensee, psvpfs::path titleIdPath);
PfsPageMapper(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, std::ostream& output, const unsigned char* klicensee, const psvpfs::path& titleIdPath);
private:
std::shared_ptr<sce_junction> brutforce_hashes(const std::unique_ptr<FilesDbParser>& filesDbParser, std::map<sce_junction, std::vector<std::uint8_t>>& fileDatas, const unsigned char* secret, const unsigned char* signature) const;
@ -45,7 +45,7 @@ private:
public:
int bruteforce_map(const std::unique_ptr<FilesDbParser>& filesDbParser, const std::unique_ptr<UnicvDbParser>& unicvDbParser);
int load_page_map(psvpfs::path filepath, std::map<std::uint32_t, std::string>& pageMap) const;
int load_page_map(const psvpfs::path& filepath, std::map<std::uint32_t, std::string>& pageMap) const;
public:
const std::map<std::uint32_t, sce_junction>& get_pageMap() const;

View File

@ -14,7 +14,7 @@
#include "PsvPfsParserConfig.h"
#include "LocalKeyGenerator.h"
int execute(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, const unsigned char *klicensee, psvpfs::path titleIdPath, psvpfs::path destTitleIdPath) {
int execute(std::shared_ptr<ICryptoOperations> cryptops, std::shared_ptr<IF00DKeyEncryptor> iF00D, const unsigned char *klicensee, const psvpfs::path& titleIdPath, const psvpfs::path& destTitleIdPath) {
PfsFilesystem pfs(cryptops, iF00D, std::cout, klicensee, titleIdPath);
if (pfs.mount() < 0)

View File

@ -8,7 +8,7 @@
#include "UnicvDbTypes.h"
UnicvDbParser::UnicvDbParser(psvpfs::path titleIdPath, std::ostream& output)
UnicvDbParser::UnicvDbParser(const psvpfs::path& titleIdPath, std::ostream& output)
: m_titleIdPath(titleIdPath), m_output(output)
{
}
@ -21,13 +21,11 @@ int UnicvDbParser::parse()
return -1;
}
psvpfs::path root(m_titleIdPath);
psvpfs::path filepath = root / "sce_pfs" / "unicv.db";
psvpfs::path filepath = m_titleIdPath / "sce_pfs" / "unicv.db";
if(!psvpfs::exists(filepath))
{
psvpfs::path filepath2 = root / "sce_pfs" / "icv.db";
psvpfs::path filepath2 = m_titleIdPath / "sce_pfs" / "icv.db";
if(!psvpfs::exists(filepath2) || !psvpfs::is_directory(filepath2))
{
m_output << "failed to find unicv.db file or icv.db folder" << std::endl;

View File

@ -7,13 +7,13 @@
class UnicvDbParser
{
private:
psvpfs::path m_titleIdPath;
const psvpfs::path& m_titleIdPath;
std::unique_ptr<sce_idb_base_t> m_fdb;
std::ostream& m_output;
public:
UnicvDbParser(psvpfs::path titleIdPath, std::ostream& output);
UnicvDbParser(const psvpfs::path& titleIdPath, std::ostream& output);
public:
int parse();

View File

@ -110,7 +110,7 @@ psvpfs::path source_path_to_dest_path(const psvpfs::path& source_root, const psv
//===
sce_junction::sce_junction(psvpfs::path value)
sce_junction::sce_junction(const psvpfs::path& value)
: m_value(value),
m_real(std::string())
{
@ -127,7 +127,7 @@ sce_junction::sce_junction(const sce_junction& other)
//this is because virtual path in files.db may not exactly match to physical path
//in real world - windows is case insensitive while linux is case sensitive
//it seems that pfs assumes windows as its prior filesystem for tools
bool sce_junction::is_equal(psvpfs::path p) const
bool sce_junction::is_equal(const psvpfs::path& p) const
{
#ifdef PSVPFS_BOOST
std::string left = m_value.generic_string();
@ -187,7 +187,7 @@ bool sce_junction::open(std::ifstream& in) const
}
//create empty directory in destination root using path from this junction
bool sce_junction::create_empty_directory(psvpfs::path source_root, psvpfs::path destination_root) const
bool sce_junction::create_empty_directory(const psvpfs::path& source_root, const psvpfs::path& destination_root) const
{
//construct new path
psvpfs::path new_path = source_path_to_dest_path(source_root, destination_root, m_real);
@ -201,7 +201,7 @@ bool sce_junction::create_empty_directory(psvpfs::path source_root, psvpfs::path
//create empty file in destination root using path from this junction
//leaves stream opened for other operations like write
bool sce_junction::create_empty_file(psvpfs::path source_root, psvpfs::path destination_root, std::ofstream& outputStream) const
bool sce_junction::create_empty_file(const psvpfs::path& source_root, const psvpfs::path& destination_root, std::ofstream& outputStream) const
{
//construct new path
psvpfs::path new_path = source_path_to_dest_path(source_root, destination_root, m_real);
@ -225,7 +225,7 @@ bool sce_junction::create_empty_file(psvpfs::path source_root, psvpfs::path dest
}
//create empty file in destination root using path from this junction
bool sce_junction::create_empty_file(psvpfs::path source_root, psvpfs::path destination_root) const
bool sce_junction::create_empty_file(const psvpfs::path& source_root, const psvpfs::path& destination_root) const
{
std::ofstream outputStream;
if(create_empty_file(source_root, destination_root, outputStream))
@ -240,7 +240,7 @@ bool sce_junction::create_empty_file(psvpfs::path source_root, psvpfs::path dest
}
//copy file in destination root using path from this junction
bool sce_junction::copy_existing_file(psvpfs::path source_root, psvpfs::path destination_root) const
bool sce_junction::copy_existing_file(const psvpfs::path& source_root, const psvpfs::path& destination_root) const
{
//construct new path
psvpfs::path new_path = source_path_to_dest_path(source_root, destination_root, m_real);
@ -268,7 +268,7 @@ bool sce_junction::copy_existing_file(psvpfs::path source_root, psvpfs::path des
}
bool sce_junction::copy_existing_file(psvpfs::path source_root, psvpfs::path destination_root, std::uintmax_t size) const
bool sce_junction::copy_existing_file(const psvpfs::path& source_root, const psvpfs::path& destination_root, std::uintmax_t size) const
{
if (!copy_existing_file(source_root, destination_root))
return false;

14
Utils.h
View File

@ -36,12 +36,12 @@ private:
mutable psvpfs::path m_real; //real path in file system
public:
sce_junction(psvpfs::path value);
sce_junction(const psvpfs::path& value);
sce_junction(const sce_junction& other);
public:
bool is_equal(psvpfs::path p) const;
bool is_equal(const psvpfs::path& p) const;
bool is_equal(const sce_junction& other) const;
@ -61,20 +61,20 @@ public:
bool open(std::ifstream& in) const;
//create empty directory in destination root using path from this junction
bool create_empty_directory(psvpfs::path source_root, psvpfs::path destination_root) const;
bool create_empty_directory(const psvpfs::path& source_root, const psvpfs::path& destination_root) const;
//create empty file in destination root using path from this junction
//leaves stream opened for other operations like write
bool create_empty_file(psvpfs::path source_root, psvpfs::path destination_root, std::ofstream& outputStream) const;
bool create_empty_file(const psvpfs::path& source_root, const psvpfs::path& destination_root, std::ofstream& outputStream) const;
//create empty file in destination root using path from this junction
bool create_empty_file(psvpfs::path source_root, psvpfs::path destination_root) const;
bool create_empty_file(const psvpfs::path& source_root, const psvpfs::path& destination_root) const;
//copy file in destination root using path from this junction
bool copy_existing_file(psvpfs::path source_root, psvpfs::path destination_root) const;
bool copy_existing_file(const psvpfs::path& source_root, const psvpfs::path& destination_root) const;
//copy file with specific size in destination root using path from this junction
bool copy_existing_file(psvpfs::path source_root, psvpfs::path destination_root, std::uintmax_t size) const;
bool copy_existing_file(const psvpfs::path& source_root, const psvpfs::path& destination_root, std::uintmax_t size) const;
//return corresponding virtual path
const psvpfs::path& get_value() const;