From e1798b9c573873987573f0d7fa808856814ece34 Mon Sep 17 00:00:00 2001 From: Jakcron Date: Sat, 11 Jun 2022 13:18:55 +0800 Subject: [PATCH] [ctrtool] Honour -p flag when processing CIA files. --- ctrtool/src/CiaProcess.cpp | 51 +++++++++++--------------------------- ctrtool/src/CiaProcess.h | 2 +- 2 files changed, 16 insertions(+), 37 deletions(-) diff --git a/ctrtool/src/CiaProcess.cpp b/ctrtool/src/CiaProcess.cpp index 6b89d7a..237f3e3 100644 --- a/ctrtool/src/CiaProcess.cpp +++ b/ctrtool/src/CiaProcess.cpp @@ -1,4 +1,5 @@ #include "CiaProcess.h" +#include "util.h" #include #include #include @@ -15,6 +16,7 @@ ctrtool::CiaProcess::CiaProcess() : mShowHeaderInfo(false), mVerbose(false), mVerify(false), + mPlain(false), mCertExtractPath(), mTikExtractPath(), mTmdExtractPath(), @@ -103,6 +105,7 @@ void ctrtool::CiaProcess::setRawMode(bool raw) void ctrtool::CiaProcess::setPlainMode(bool plain) { + mPlain = plain; mNcchProcess.setPlainMode(plain); } @@ -515,7 +518,7 @@ void ctrtool::CiaProcess::verifyContent() content_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, itr->second.offset, itr->second.size)); - if (itr->second.is_encrypted && mDecryptedTitleKey.isSet()) + if (!mPlain && itr->second.is_encrypted && mDecryptedTitleKey.isSet()) { tc::crypto::Aes128CbcEncryptedStream::iv_t content_iv; createContentIv(content_iv, itr->second.cindex); @@ -691,23 +694,22 @@ void ctrtool::CiaProcess::printHeader() void ctrtool::CiaProcess::extractCia() { + tc::ByteData cache = tc::ByteData(0x10000); tc::io::Path out_path; std::shared_ptr in_stream; - std::shared_ptr out_stream; if (mCertExtractPath.isSet() && mCertSizeInfo.size > 0) { out_path = mCertExtractPath.get(); in_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, mCertSizeInfo.offset, mCertSizeInfo.size)); - out_stream = std::shared_ptr(new tc::io::FileStream(out_path, tc::io::FileMode::OpenOrCreate, tc::io::FileAccess::Write)); if (mVerbose) { fmt::print(stderr, "[{} LOG] Saving certs to {}...\n", mModuleLabel, out_path.to_string()); } - copyStream(in_stream, out_stream); + writeStreamToFile(in_stream, out_path, cache); } if (mTikExtractPath.isSet() && mTikSizeInfo.size > 0) @@ -715,13 +717,13 @@ void ctrtool::CiaProcess::extractCia() out_path = mTikExtractPath.get(); in_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, mTikSizeInfo.offset, mTikSizeInfo.size)); - out_stream = std::shared_ptr(new tc::io::FileStream(out_path, tc::io::FileMode::OpenOrCreate, tc::io::FileAccess::Write)); if (mVerbose) { fmt::print(stderr, "[{} LOG] Saving tik to {}...\n", mModuleLabel, out_path.to_string()); } - copyStream(in_stream, out_stream); + + writeStreamToFile(in_stream, out_path, cache); } if (mTmdExtractPath.isSet() && mTmdSizeInfo.size > 0) @@ -729,13 +731,13 @@ void ctrtool::CiaProcess::extractCia() out_path = mTmdExtractPath.get(); in_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, mTmdSizeInfo.offset, mTmdSizeInfo.size)); - out_stream = std::shared_ptr(new tc::io::FileStream(out_path, tc::io::FileMode::OpenOrCreate, tc::io::FileAccess::Write)); if (mVerbose) { fmt::print(stderr, "[{} LOG] Saving tmd to {}...\n", mModuleLabel, out_path.to_string()); } - copyStream(in_stream, out_stream); + + writeStreamToFile(in_stream, out_path, cache); } if (mFooterExtractPath.isSet() && mFooterSizeInfo.size > 0) @@ -743,13 +745,13 @@ void ctrtool::CiaProcess::extractCia() out_path = mFooterExtractPath.get(); in_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, mFooterSizeInfo.offset, mFooterSizeInfo.size)); - out_stream = std::shared_ptr(new tc::io::FileStream(out_path, tc::io::FileMode::OpenOrCreate, tc::io::FileAccess::Write)); if (mVerbose) { fmt::print(stderr, "[{} LOG] Saving meta to {}...\n", mModuleLabel, out_path.to_string()); } - copyStream(in_stream, out_stream); + + writeStreamToFile(in_stream, out_path, cache); } if (mContentExtractPath.isSet() && mContentInfo.size() > 0) @@ -761,7 +763,7 @@ void ctrtool::CiaProcess::extractCia() in_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, itr->second.offset, itr->second.size)); - if (itr->second.is_encrypted && mDecryptedTitleKey.isSet()) + if (!mPlain && itr->second.is_encrypted && mDecryptedTitleKey.isSet()) { tc::crypto::Aes128CbcEncryptedStream::iv_t content_iv; createContentIv(content_iv, itr->second.cindex); @@ -769,39 +771,16 @@ void ctrtool::CiaProcess::extractCia() in_stream = std::shared_ptr(new tc::crypto::Aes128CbcEncryptedStream(in_stream, mDecryptedTitleKey.get(), content_iv)); } - out_stream = std::shared_ptr(new tc::io::FileStream(out_path, tc::io::FileMode::OpenOrCreate, tc::io::FileAccess::Write)); - if (mVerbose) { fmt::print(stderr, "[{} LOG] Saving content {:04x} to {}...\n", mModuleLabel, itr->second.cindex, out_path.to_string()); } - copyStream(in_stream, out_stream); + writeStreamToFile(in_stream, out_path, cache); } } } -void ctrtool::CiaProcess::copyStream(const std::shared_ptr& in, const std::shared_ptr& out) -{ - tc::ByteData cache = tc::ByteData(0x10000); - size_t cache_read_len; - - in->seek(0, tc::io::SeekOrigin::Begin); - out->seek(0, tc::io::SeekOrigin::Begin); - for (int64_t remaining_data = in->length(); remaining_data > 0;) - { - cache_read_len = in->read(cache.data(), cache.size()); - if (cache_read_len == 0) - { - throw tc::io::IOException(mModuleLabel, "Failed to read from source file."); - } - - out->write(cache.data(), cache_read_len); - - remaining_data -= int64_t(cache_read_len); - } -} - void ctrtool::CiaProcess::processContent() { if (mContentIndex >= ntd::n3ds::CiaHeader::kCiaMaxContentNum) @@ -813,7 +792,7 @@ void ctrtool::CiaProcess::processContent() { std::shared_ptr content_stream = std::shared_ptr(new tc::io::SubStream(mInputStream, mContentInfo[mContentIndex].offset, mContentInfo[mContentIndex].size)); - if (mContentInfo[mContentIndex].is_encrypted && mDecryptedTitleKey.isSet()) + if (!mPlain && mContentInfo[mContentIndex].is_encrypted && mDecryptedTitleKey.isSet()) { tc::crypto::Aes128CbcEncryptedStream::iv_t content_iv; createContentIv(content_iv, mContentInfo[mContentIndex].cindex); diff --git a/ctrtool/src/CiaProcess.h b/ctrtool/src/CiaProcess.h index 245184e..c708936 100644 --- a/ctrtool/src/CiaProcess.h +++ b/ctrtool/src/CiaProcess.h @@ -46,6 +46,7 @@ private: bool mShowHeaderInfo; bool mVerbose; bool mVerify; + bool mPlain; tc::Optional mCertExtractPath; tc::Optional mTikExtractPath; tc::Optional mTmdExtractPath; @@ -108,7 +109,6 @@ private: void verifyContent(); void printHeader(); void extractCia(); - void copyStream(const std::shared_ptr& in, const std::shared_ptr& out); void processContent(); void createContentIv(std::array& content_iv, uint16_t index);