From 3e84a2acc0274432a5124bf5ee93009003bd015f Mon Sep 17 00:00:00 2001 From: Eladash Date: Thu, 16 Sep 2021 11:30:36 +0300 Subject: [PATCH] PKG: Implement SDATA decryption --- rpcs3/Crypto/unpkg.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/rpcs3/Crypto/unpkg.cpp b/rpcs3/Crypto/unpkg.cpp index 97a468305e..090a316fa0 100644 --- a/rpcs3/Crypto/unpkg.cpp +++ b/rpcs3/Crypto/unpkg.cpp @@ -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& sync) { if (!m_is_valid) @@ -745,7 +747,7 @@ bool package_reader::extract_data(atomic_t& 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& 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>() : 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& sync) } } + if (is_buffered) + { + out = DecryptEDAT(out, name, 1, "", reinterpret_cast(&m_header.klicensee), true); + if (!out || !fs::write_file(path, fs::rewrite, static_cast>*>(out.release().get())->obj)) + { + num_failures++; + pkg_log.error("Failed to create file %s", path); + break; + } + } + if (extract_success) { if (did_overwrite)