From 322e4d2b483e92946ee954f43a66a7d73cc52f54 Mon Sep 17 00:00:00 2001 From: jakcron Date: Wed, 25 Apr 2018 20:27:47 +0800 Subject: [PATCH] [nstool] Added RomFs extraction support. --- programs/nstool/source/RomfsProcess.cpp | 56 ++++++++++++++++++++++++- programs/nstool/source/RomfsProcess.h | 3 ++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/programs/nstool/source/RomfsProcess.cpp b/programs/nstool/source/RomfsProcess.cpp index bcde3b3..0c3d8a1 100644 --- a/programs/nstool/source/RomfsProcess.cpp +++ b/programs/nstool/source/RomfsProcess.cpp @@ -54,9 +54,63 @@ void RomfsProcess::displayFs() displayDir(mRootDir, 1); } +void RomfsProcess::extractDir(const std::string& path, const sDirectory& dir) +{ + std::string dir_path; + std::string file_path; + + // make dir path + fnd::io::appendToPath(dir_path, path); + if (dir.name.empty() == false) + fnd::io::appendToPath(dir_path, dir.name); + + //printf("dirpath=[%s]\n", dir_path.c_str()); + + // make directory + fnd::io::makeDirectory(dir_path); + + + // allocate memory for file extraction + fnd::MemoryBlob scratch; + scratch.alloc(kFileExportBlockSize); + + // extract files + fnd::SimpleFile outFile; + for (size_t i = 0; i < dir.file_list.getSize(); i++) + { + file_path.clear(); + fnd::io::appendToPath(file_path, dir_path); + fnd::io::appendToPath(file_path, dir.file_list[i].name); + + if (mCliOutputType >= OUTPUT_VERBOSE) + printf("extract=[%s]\n", file_path.c_str()); + + + outFile.open(file_path, outFile.Create); + mReader->seek(mOffset + dir.file_list[i].offset); + for (size_t j = 0; j < (dir.file_list[i].size / kFileExportBlockSize); j++) + { + mReader->read(scratch.getBytes(), kFileExportBlockSize); + outFile.write(scratch.getBytes(), kFileExportBlockSize); + } + if (dir.file_list[i].size % kFileExportBlockSize) + { + mReader->read(scratch.getBytes(), dir.file_list[i].size % kFileExportBlockSize); + outFile.write(scratch.getBytes(), dir.file_list[i].size % kFileExportBlockSize); + } + outFile.close(); + } + + for (size_t i = 0; i < dir.dir_list.getSize(); i++) + { + extractDir(dir_path, dir.dir_list[i]); + } +} + + void RomfsProcess::extractFs() { - + extractDir(mExtractPath, mRootDir); } bool RomfsProcess::validateHeaderLayout(const nx::sRomfsHeader* hdr) const diff --git a/programs/nstool/source/RomfsProcess.h b/programs/nstool/source/RomfsProcess.h index ace7cdd..42cb54f 100644 --- a/programs/nstool/source/RomfsProcess.h +++ b/programs/nstool/source/RomfsProcess.h @@ -137,6 +137,9 @@ private: void displayHeader(); void displayFs(); + + void extractFile(const std::string& path, const sFile& file); + void extractDir(const std::string& path, const sDirectory& dir); void extractFs(); bool validateHeaderLayout(const nx::sRomfsHeader* hdr) const;