PKG: Implement SDATA decryption

This commit is contained in:
Eladash 2021-09-16 11:30:36 +03:00 committed by Megamouse
parent 77c252a4c0
commit 3e84a2acc0
1 changed files with 22 additions and 2 deletions

View File

@ -667,6 +667,8 @@ package_error package_reader::check_target_app_version() const
return package_error::app_version;
}
fs::file DecryptEDAT(const fs::file& input, const std::string& input_file_name, int mode, const std::string& rap_file_name, u8 *custom_klic, bool verbose = false);
bool package_reader::extract_data(atomic_t<double>& sync)
{
if (!m_is_valid)
@ -745,7 +747,7 @@ bool package_reader::extract_data(atomic_t<double>& sync)
const bool log_error = entry.pad || (entry.type & ~PKG_FILE_ENTRY_KNOWN_BITS);
(log_error ? pkg_log.error : pkg_log.notice)("Entry 0x%08x: %s (pad=0x%x)", entry.type, name, entry.pad);
switch (entry.type & 0xff)
switch (const u8 entry_type = entry.type & 0xff)
{
case PKG_FILE_ENTRY_NPDRM:
case PKG_FILE_ENTRY_NPDRMEDAT:
@ -771,7 +773,14 @@ bool package_reader::extract_data(atomic_t<double>& sync)
break;
}
if (fs::file out{ path, fs::rewrite })
const bool is_buffered = entry_type == PKG_FILE_ENTRY_SDAT;
if (entry_type == PKG_FILE_ENTRY_NPDRMEDAT)
{
pkg_log.todo("NPDRM EDAT!");
}
if (fs::file out = is_buffered ? fs::make_stream<std::vector<u8>>() : fs::file{ path, fs::rewrite })
{
bool extract_success = true;
for (u64 pos = 0; pos < entry.file_size; pos += BUF_SIZE)
@ -807,6 +816,17 @@ bool package_reader::extract_data(atomic_t<double>& sync)
}
}
if (is_buffered)
{
out = DecryptEDAT(out, name, 1, "", reinterpret_cast<u8*>(&m_header.klicensee), true);
if (!out || !fs::write_file(path, fs::rewrite, static_cast<fs::container_stream<std::vector<u8>>*>(out.release().get())->obj))
{
num_failures++;
pkg_log.error("Failed to create file %s", path);
break;
}
}
if (extract_success)
{
if (did_overwrite)