Add IOFile::GetData

This commit is contained in:
zhupengfei
2020-11-28 20:12:24 +08:00
parent 9cbec118cd
commit 5c595a8046
6 changed files with 39 additions and 55 deletions
+19
View File
@@ -873,6 +873,25 @@ std::size_t IOFile::Write(const char* data, std::size_t length) {
return items_written;
}
std::vector<u8> IOFile::GetData() {
if (!IsOpen()) {
m_good = false;
LOG_ERROR(Common, "File is not open");
return {};
}
if (!Seek(0, SEEK_SET)) {
LOG_ERROR(Common, "Failed to seek file");
return {};
}
std::vector<u8> data(GetSize());
if (Read(reinterpret_cast<char*>(data.data()), data.size()) != data.size()) {
LOG_ERROR(Common, "Failed to read from file");
return {};
}
return data;
}
u64 IOFile::GetSize() const {
if (IsOpen())
return FileUtil::GetSize(m_file);
+2
View File
@@ -220,6 +220,8 @@ public:
virtual std::size_t Read(char* data, std::size_t length);
virtual std::size_t Write(const char* data, std::size_t length);
std::vector<u8> GetData();
bool IsOpen() const {
return nullptr != m_file;
}
+4 -11
View File
@@ -80,20 +80,13 @@ std::vector<u8> SDMCDecryptor::DecryptFile(const std::string& source) const {
aes.SetKeyWithIV(key.data(), key.size(), ctr.data());
FileUtil::IOFile file(root_folder + source, "rb");
if (!file) {
LOG_ERROR(Core, "Could not open {}", root_folder + source);
std::vector<u8> encrypted_data = file.GetData();
if (encrypted_data.empty()) {
LOG_ERROR(Core, "Failed to read from {}", root_folder + source);
return {};
}
auto size = file.GetSize();
std::vector<u8> encrypted_data(size);
if (file.ReadBytes(encrypted_data.data(), size) != size) {
LOG_ERROR(Core, "Could not read file {}", root_folder + source);
return {};
}
std::vector<u8> data(size);
std::vector<u8> data(file.GetSize());
aes.ProcessData(data.data(), encrypted_data.data(), encrypted_data.size());
return data;
}
+9 -27
View File
@@ -194,13 +194,8 @@ bool SDMCImporter::ImportNandSavegame(u64 id, [[maybe_unused]] const ProgressCal
const auto path = fmt::format("sysdata/{:08x}/00000000", (id & 0xFFFFFFFF));
FileUtil::IOFile file(config.nand_data_path + path, "rb");
if (!file) {
LOG_ERROR(Core, "Could not open file {}", path);
return false;
}
std::vector<u8> data(file.GetSize());
if (file.ReadBytes(data.data(), data.size()) != data.size()) {
std::vector<u8> data = file.GetData();
if (data.empty()) {
LOG_ERROR(Core, "Failed to read from {}", path);
return false;
}
@@ -248,13 +243,8 @@ bool SDMCImporter::ImportSystemArchive(u64 id, [[maybe_unused]] const ProgressCa
const auto path = fmt::format("{}{:08x}/{:08x}.app", config.system_archives_path, (id >> 32),
(id & 0xFFFFFFFF));
FileUtil::IOFile file(path, "rb");
if (!file) {
LOG_ERROR(Core, "Could not open {}", path);
return false;
}
std::vector<u8> data(file.GetSize());
if (file.ReadBytes(data.data(), data.size()) != data.size()) {
std::vector<u8> data = file.GetData();
if (data.empty()) {
LOG_ERROR(Core, "Failed to read from {}", path);
return false;
}
@@ -373,12 +363,8 @@ bool SDMCImporter::ImportSysdata(u64 id, [[maybe_unused]] const ProgressCallback
}
case 5: { // Config savegame
FileUtil::IOFile file(config.config_savegame_path, "rb");
if (!file) {
return false;
}
std::vector<u8> data(file.GetSize());
if (file.ReadBytes(data.data(), data.size()) != data.size()) {
std::vector<u8> data = file.GetData();
if (data.empty()) {
return false;
}
@@ -808,16 +794,12 @@ void SDMCImporter::ListNandSavegame(std::vector<ContentSpecifier>& out) const {
// Read the file to test.
FileUtil::IOFile file(path, "rb");
if (!file) {
LOG_ERROR(Core, "Could not open {}", path);
return false;
}
std::vector<u8> data(file.GetSize());
if (file.ReadBytes(data.data(), data.size()) != data.size()) {
std::vector<u8> data = file.GetData();
if (data.empty()) {
LOG_ERROR(Core, "Could not read from {}", path);
return false;
}
DataContainer container(std::move(data));
if (!container.IsGood()) {
return true;
+1 -11
View File
@@ -279,17 +279,7 @@ std::vector<u8> SDExtdata::ReadFile(const std::string& path) const {
return decryptor->DecryptFile(path);
} else {
FileUtil::IOFile file(path, "rb");
if (!file) {
LOG_ERROR(Core, "Failed to open {}", path);
return {};
}
std::vector<u8> data(file.GetSize());
if (file.ReadBytes(data.data(), data.size()) != data.size()) {
LOG_ERROR(Core, "Failed to read from {}", path);
return {};
}
return data;
return file.GetData();
}
}
+4 -6
View File
@@ -217,10 +217,9 @@ void UtilitiesDialog::SaveDataExtractionTool() {
} else {
// TODO: Add Progress reporting
ShowProgressDialog([source = source, destination = destination] {
const auto size = FileUtil::GetSize(source.toStdString());
std::vector<u8> data(size);
FileUtil::IOFile file(source.toStdString(), "rb");
if (file.ReadBytes(data.data(), size) != size) {
std::vector<u8> data = file.GetData();
if (data.empty()) {
return false;
}
@@ -277,10 +276,9 @@ void UtilitiesDialog::RomFSExtractionTool() {
}
ShowProgressDialog([source = source, destination = destination] {
const auto size = FileUtil::GetSize(source.toStdString());
std::vector<u8> data(size);
FileUtil::IOFile src_file(source.toStdString(), "rb");
if (src_file.ReadBytes(data.data(), size) != size) {
std::vector<u8> data = src_file.GetData();
if (data.empty()) {
return false;
}