nstool/src/IniProcess.cpp

147 lines
3.6 KiB
C++
Raw Normal View History

#include "IniProcess.h"
#include "util.h"
2021-10-13 05:39:45 +00:00
#include "KipProcess.h"
nstool::IniProcess::IniProcess() :
2021-10-12 06:26:19 +00:00
mModuleName("nstool::IniProcess"),
mFile(),
mCliOutputMode(true, false, false, false),
mVerify(false),
mKipExtractPath()
{
}
void nstool::IniProcess::process()
{
importHeader();
importKipList();
if (mCliOutputMode.show_basic_info)
{
displayHeader();
displayKipList();
}
2021-10-12 06:26:19 +00:00
if (mKipExtractPath.isSet())
{
extractKipList();
}
}
void nstool::IniProcess::setInputFile(const std::shared_ptr<tc::io::IStream>& file)
{
mFile = file;
}
void nstool::IniProcess::setCliOutputMode(CliOutputMode type)
{
mCliOutputMode = type;
}
void nstool::IniProcess::setVerifyMode(bool verify)
{
mVerify = verify;
}
2021-10-12 06:26:19 +00:00
void nstool::IniProcess::setKipExtractPath(const tc::io::Path& path)
{
mKipExtractPath = path;
}
void nstool::IniProcess::importHeader()
{
2021-10-12 06:26:19 +00:00
if (mFile == nullptr)
{
throw tc::Exception(mModuleName, "No file reader set.");
}
if (mFile->canRead() == false || mFile->canSeek() == false)
{
2021-10-12 06:26:19 +00:00
throw tc::NotSupportedException(mModuleName, "Input stream requires read/seek permissions.");
}
2021-10-12 06:26:19 +00:00
// check if file_size is smaller than INI header size
2022-06-29 13:19:36 +00:00
if (tc::io::IOUtil::castInt64ToSize(mFile->length()) < sizeof(pie::hac::sIniHeader))
{
throw tc::Exception(mModuleName, "Corrupt INI: file too small.");
}
2021-10-12 06:26:19 +00:00
// read ini
2022-06-29 13:19:36 +00:00
tc::ByteData scratch = tc::ByteData(sizeof(pie::hac::sIniHeader));
2021-10-12 06:26:19 +00:00
mFile->seek(0, tc::io::SeekOrigin::Begin);
mFile->read(scratch.data(), scratch.size());
2021-10-12 06:26:19 +00:00
// parse ini header
mHdr.fromBytes(scratch.data(), scratch.size());
}
void nstool::IniProcess::importKipList()
{
// kip pos info
2022-06-29 13:19:36 +00:00
int64_t kip_pos = tc::io::IOUtil::castSizeToInt64(sizeof(pie::hac::sIniHeader));
2021-10-13 05:39:45 +00:00
int64_t kip_size = 0;
// tmp data to determine size
2022-06-29 13:19:36 +00:00
pie::hac::sKipHeader hdr_raw;
pie::hac::KernelInitialProcessHeader hdr;
for (size_t i = 0; i < mHdr.getKipNum(); i++)
{
2021-10-13 05:39:45 +00:00
mFile->seek(kip_pos, tc::io::SeekOrigin::Begin);
mFile->read((byte_t*)&hdr_raw, sizeof(hdr_raw));
hdr.fromBytes((byte_t*)&hdr_raw, sizeof(hdr_raw));
kip_size = getKipSizeFromHeader(hdr);
mKipList.push_back({hdr, std::make_shared<tc::io::SubStream>(tc::io::SubStream(mFile, kip_pos, kip_size))});
kip_pos += kip_size;
}
}
void nstool::IniProcess::displayHeader()
{
2021-10-12 06:26:19 +00:00
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()
{
for (auto itr = mKipList.begin(); itr != mKipList.end(); itr++)
{
KipProcess obj;
obj.setInputFile(itr->stream);
obj.setCliOutputMode(mCliOutputMode);
obj.setVerifyMode(mVerify);
obj.process();
}
}
void nstool::IniProcess::extractKipList()
{
// allocate cache memory
tc::ByteData cache = tc::ByteData(kCacheSize);
// make extract dir
2022-06-29 13:19:36 +00:00
tc::io::LocalFileSystem local_fs;
local_fs.createDirectory(mKipExtractPath.get());
// out path for extracted KIP
tc::io::Path out_path;
// extract KIPs
for (auto itr = mKipList.begin(); itr != mKipList.end(); itr++)
{
out_path = mKipExtractPath.get();
out_path += fmt::format("{:s}.kip", itr->hdr.getName());
if (mCliOutputMode.show_basic_info)
2022-06-29 13:19:36 +00:00
fmt::print("Saving {:s}...\n", out_path.to_string());
writeStreamToFile(itr->stream, out_path, cache);
}
}
2022-06-29 13:19:36 +00:00
int64_t nstool::IniProcess::getKipSizeFromHeader(const pie::hac::KernelInitialProcessHeader& hdr) const
{
// the order of elements in a KIP are sequential, there are no file offsets
2022-06-29 13:19:36 +00:00
return int64_t(sizeof(pie::hac::sKipHeader)) + int64_t(hdr.getTextSegmentInfo().file_layout.size + hdr.getRoSegmentInfo().file_layout.size + hdr.getDataSegmentInfo().file_layout.size);
}