From 054776b58616d48c882f10cc1864b348c2410d7e Mon Sep 17 00:00:00 2001 From: jakcron Date: Tue, 12 Oct 2021 14:26:19 +0800 Subject: [PATCH] Move IniProcess and KipProcess --- build/visualstudio/nstool/nstool.vcxproj | 2 + .../nstool/nstool.vcxproj.filters | 6 +++ src/{not_ported => }/IniProcess.cpp | 37 +++++++++++-------- src/IniProcess.h | 3 +- src/{not_ported => }/KipProcess.cpp | 0 5 files changed, 32 insertions(+), 16 deletions(-) rename src/{not_ported => }/IniProcess.cpp (76%) rename src/{not_ported => }/KipProcess.cpp (100%) diff --git a/build/visualstudio/nstool/nstool.vcxproj b/build/visualstudio/nstool/nstool.vcxproj index bd7757d..0b4e846 100644 --- a/build/visualstudio/nstool/nstool.vcxproj +++ b/build/visualstudio/nstool/nstool.vcxproj @@ -185,7 +185,9 @@ + + diff --git a/build/visualstudio/nstool/nstool.vcxproj.filters b/build/visualstudio/nstool/nstool.vcxproj.filters index a18c5e9..a6434e6 100644 --- a/build/visualstudio/nstool/nstool.vcxproj.filters +++ b/build/visualstudio/nstool/nstool.vcxproj.filters @@ -152,5 +152,11 @@ Source Files + + Source Files + + + Source Files + \ No newline at end of file diff --git a/src/not_ported/IniProcess.cpp b/src/IniProcess.cpp similarity index 76% rename from src/not_ported/IniProcess.cpp rename to src/IniProcess.cpp index d976309..86354f9 100644 --- a/src/not_ported/IniProcess.cpp +++ b/src/IniProcess.cpp @@ -10,6 +10,7 @@ nstool::IniProcess::IniProcess() : + mModuleName("nstool::IniProcess"), mFile(), mCliOutputMode(true, false, false, false), mVerify(false), @@ -27,7 +28,7 @@ void nstool::IniProcess::process() displayHeader(); displayKipList(); } - if (mDoExtractKip) + if (mKipExtractPath.isSet()) { extractKipList(); } @@ -48,29 +49,35 @@ void nstool::IniProcess::setVerifyMode(bool verify) mVerify = verify; } -void nstool::IniProcess::setKipExtractPath(const std::string& path) +void nstool::IniProcess::setKipExtractPath(const tc::io::Path& path) { - mDoExtractKip = true; mKipExtractPath = path; } void nstool::IniProcess::importHeader() { - tc::ByteData scratch; - - if (*mFile == nullptr) + if (mFile == nullptr) { - throw tc::Exception(kModuleName, "No file reader set."); + throw tc::Exception(mModuleName, "No file reader set."); + } + if (mFile->canRead() == false || mFile->canSeek() == false) + { + throw tc::NotSupportedException(mModuleName, "Input stream requires read/seek permissions."); } - if ((*mFile)->size() < sizeof(nn::hac::sIniHeader)) + // check if file_size is smaller than INI header size + size_t file_size = tc::io::IOUtil::castInt64ToSize(mFile->length()); + if (file_size < sizeof(nn::hac::sIniHeader)) { - throw tc::Exception(kModuleName, "Corrupt INI: file too small"); + throw tc::Exception(mModuleName, "Corrupt NSO: file too small."); } - scratch.alloc(sizeof(nn::hac::sIniHeader)); - (*mFile)->read(scratch.data(), 0, scratch.size()); + // read ini + tc::ByteData scratch = tc::ByteData(sizeof(nn::hac::sIniHeader)); + mFile->seek(0, tc::io::SeekOrigin::Begin); + mFile->read(scratch.data(), scratch.size()); + // parse ini header mHdr.fromBytes(scratch.data(), scratch.size()); } @@ -97,9 +104,9 @@ void nstool::IniProcess::importKipList() void nstool::IniProcess::displayHeader() { - std::cout << "[INI Header]" << std::endl; - std::cout << " Size: 0x" << std::hex << mHdr.getSize() << std::endl; - std::cout << " KIP Num: " << std::dec << (uint32_t)mHdr.getKipNum() << std::endl; + fmt::print("[INI Header]\n"); + fmt::print(" Size: 0x{:x}\n", mHdr.getSize()); + fmt::print(" KIP Num: {:d}\n", mHdr.getKipNum()); } void nstool::IniProcess::displayKipList() @@ -123,7 +130,7 @@ void nstool::IniProcess::extractKipList() // allocate cache memory - cache.alloc(kCacheSize); + cache = tc::ByteData(kCacheSize); // make extract dir fnd::io::makeDirectory(mKipExtractPath); diff --git a/src/IniProcess.h b/src/IniProcess.h index 59499ae..e5ee8a3 100644 --- a/src/IniProcess.h +++ b/src/IniProcess.h @@ -19,10 +19,11 @@ public: void setKipExtractPath(const tc::io::Path& path); private: - const std::string kModuleName = "IniProcess"; const std::string kKipExtention = ".kip"; const size_t kCacheSize = 0x10000; + std::string mModuleName; + std::shared_ptr mFile; CliOutputMode mCliOutputMode; bool mVerify; diff --git a/src/not_ported/KipProcess.cpp b/src/KipProcess.cpp similarity index 100% rename from src/not_ported/KipProcess.cpp rename to src/KipProcess.cpp