From 8e360332e5a37cce5fc9e9ca135c8c0ffb63c036 Mon Sep 17 00:00:00 2001 From: jakcron Date: Fri, 11 May 2018 21:20:28 +0800 Subject: [PATCH] [nstool] Changed handling of shared IFile derrivatives. Always a pointer --- programs/nstool/source/AesCtrWrappedIFile.cpp | 14 +++++++------- programs/nstool/source/AesCtrWrappedIFile.h | 4 ++-- programs/nstool/source/NcaProcess.cpp | 6 +++--- programs/nstool/source/NcaProcess.h | 3 +-- programs/nstool/source/NpdmProcess.cpp | 4 ++-- programs/nstool/source/NpdmProcess.h | 2 +- programs/nstool/source/OffsetAdjustedIFile.cpp | 8 ++++---- programs/nstool/source/OffsetAdjustedIFile.h | 4 ++-- programs/nstool/source/PfsProcess.cpp | 4 ++-- programs/nstool/source/PfsProcess.h | 2 +- programs/nstool/source/RomfsProcess.cpp | 4 ++-- programs/nstool/source/RomfsProcess.h | 2 +- programs/nstool/source/XciProcess.cpp | 8 ++++---- programs/nstool/source/XciProcess.h | 2 +- programs/nstool/source/main.cpp | 8 ++++---- 15 files changed, 37 insertions(+), 38 deletions(-) diff --git a/programs/nstool/source/AesCtrWrappedIFile.cpp b/programs/nstool/source/AesCtrWrappedIFile.cpp index 29c4344..79d966f 100644 --- a/programs/nstool/source/AesCtrWrappedIFile.cpp +++ b/programs/nstool/source/AesCtrWrappedIFile.cpp @@ -1,6 +1,6 @@ #include "AesCtrWrappedIFile.h" -AesCtrWrappedIFile::AesCtrWrappedIFile(fnd::IFile& file, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr) : +AesCtrWrappedIFile::AesCtrWrappedIFile(fnd::IFile* file, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr) : mFile(file), mKey(key), mBaseCtr(ctr) @@ -10,12 +10,12 @@ AesCtrWrappedIFile::AesCtrWrappedIFile(fnd::IFile& file, const crypto::aes::sAes size_t AesCtrWrappedIFile::size() { - return mFile.size(); + return mFile->size(); } void AesCtrWrappedIFile::seek(size_t offset) { - mFile.seek(offset); + mFile->seek(offset); crypto::aes::AesIncrementCounter(mBaseCtr.iv, offset>>4, mCurrentCtr.iv); mBlockOffset = offset & 0xf; } @@ -24,7 +24,7 @@ void AesCtrWrappedIFile::read(byte_t* out, size_t len) { for (size_t i = 0; i < (len / kAesCtrScratchSize); i++) { - mFile.read(mScratch.getBytes() + mBlockOffset, kAesCtrScratchSize); + mFile->read(mScratch.getBytes() + mBlockOffset, kAesCtrScratchSize); crypto::aes::AesCtr(mScratch.getBytes(), kAesCtrScratchAllocSize, mKey.key, mCurrentCtr.iv, mScratch.getBytes()); memcpy(out + (i * kAesCtrScratchSize), mScratch.getBytes() + mBlockOffset, kAesCtrScratchSize); } @@ -33,7 +33,7 @@ void AesCtrWrappedIFile::read(byte_t* out, size_t len) { size_t read_len = len % kAesCtrScratchSize; size_t read_pos = ((len / kAesCtrScratchSize) * kAesCtrScratchSize); - mFile.read(mScratch.getBytes() + mBlockOffset, read_len); + mFile->read(mScratch.getBytes() + mBlockOffset, read_len); crypto::aes::AesCtr(mScratch.getBytes(), kAesCtrScratchAllocSize, mKey.key, mCurrentCtr.iv, mScratch.getBytes()); memcpy(out + read_pos, mScratch.getBytes() + mBlockOffset, read_len); } @@ -51,7 +51,7 @@ void AesCtrWrappedIFile::write(const byte_t* out, size_t len) { memcpy(mScratch.getBytes() + mBlockOffset, out + (i * kAesCtrScratchSize), kAesCtrScratchSize); crypto::aes::AesCtr(mScratch.getBytes(), kAesCtrScratchAllocSize, mKey.key, mCurrentCtr.iv, mScratch.getBytes()); - mFile.write(mScratch.getBytes() + mBlockOffset, kAesCtrScratchSize); + mFile->write(mScratch.getBytes() + mBlockOffset, kAesCtrScratchSize); } if (len % kAesCtrScratchSize) @@ -60,7 +60,7 @@ void AesCtrWrappedIFile::write(const byte_t* out, size_t len) size_t write_pos = ((len / kAesCtrScratchSize) * kAesCtrScratchSize); memcpy(mScratch.getBytes() + mBlockOffset, out + write_pos, write_len); crypto::aes::AesCtr(mScratch.getBytes(), kAesCtrScratchAllocSize, mKey.key, mCurrentCtr.iv, mScratch.getBytes()); - mFile.write(mScratch.getBytes() + mBlockOffset, write_len); + mFile->write(mScratch.getBytes() + mBlockOffset, write_len); } } diff --git a/programs/nstool/source/AesCtrWrappedIFile.h b/programs/nstool/source/AesCtrWrappedIFile.h index 5f88844..f2a001c 100644 --- a/programs/nstool/source/AesCtrWrappedIFile.h +++ b/programs/nstool/source/AesCtrWrappedIFile.h @@ -5,7 +5,7 @@ class AesCtrWrappedIFile : public fnd::IFile { public: - AesCtrWrappedIFile(fnd::IFile& file, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr); + AesCtrWrappedIFile(fnd::IFile* file, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr); size_t size(); void seek(size_t offset); @@ -18,7 +18,7 @@ private: static const size_t kAesCtrScratchSize = 0x1000000; static const size_t kAesCtrScratchAllocSize = kAesCtrScratchSize + crypto::aes::kAesBlockSize; - fnd::IFile& mFile; + fnd::IFile* mFile; crypto::aes::sAes128Key mKey; crypto::aes::sAesIvCtr mBaseCtr, mCurrentCtr; size_t mBlockOffset; diff --git a/programs/nstool/source/NcaProcess.cpp b/programs/nstool/source/NcaProcess.cpp index a954617..b6f830b 100644 --- a/programs/nstool/source/NcaProcess.cpp +++ b/programs/nstool/source/NcaProcess.cpp @@ -249,7 +249,7 @@ void NcaProcess::processPartitions() // create reader fnd::IFile* partitionReader = nullptr; - AesCtrWrappedIFile aesCtrFile = AesCtrWrappedIFile(*mReader, mBodyKeys.aes_ctr.var, ctr); + AesCtrWrappedIFile aesCtrFile = AesCtrWrappedIFile(mReader, mBodyKeys.aes_ctr.var, ctr); switch(fs_header.encryption_type) { case (nx::nca::CRYPT_AESXTS): @@ -290,7 +290,7 @@ void NcaProcess::processPartitions() if (fs_header.format_type == nx::nca::FORMAT_PFS0) { PfsProcess pfs; - pfs.setInputFile(*partitionReader); + pfs.setInputFile(partitionReader); pfs.setInputFileOffset(partition.offset + data_offset); pfs.setCliOutputMode(mCliOutputType); pfs.setListFs(mListFs); @@ -301,7 +301,7 @@ void NcaProcess::processPartitions() else if (fs_header.format_type == nx::nca::FORMAT_ROMFS) { RomfsProcess romfs; - romfs.setInputFile(*partitionReader); + romfs.setInputFile(partitionReader); romfs.setInputFileOffset(partition.offset + data_offset); romfs.setCliOutputMode(mCliOutputType); romfs.setListFs(mListFs); diff --git a/programs/nstool/source/NcaProcess.h b/programs/nstool/source/NcaProcess.h index 0d9890b..e7fc0fa 100644 --- a/programs/nstool/source/NcaProcess.h +++ b/programs/nstool/source/NcaProcess.h @@ -57,8 +57,7 @@ private: sOptional aes_ctr; sOptional aes_xts; } mBodyKeys; - - + void displayHeader(); diff --git a/programs/nstool/source/NpdmProcess.cpp b/programs/nstool/source/NpdmProcess.cpp index 26cc318..aeac024 100644 --- a/programs/nstool/source/NpdmProcess.cpp +++ b/programs/nstool/source/NpdmProcess.cpp @@ -665,9 +665,9 @@ void NpdmProcess::process() } } -void NpdmProcess::setInputFile(fnd::IFile& reader) +void NpdmProcess::setInputFile(fnd::IFile* reader) { - mReader = &reader; + mReader = reader; } void NpdmProcess::setInputFileOffset(size_t offset) diff --git a/programs/nstool/source/NpdmProcess.h b/programs/nstool/source/NpdmProcess.h index 36bef32..ece7623 100644 --- a/programs/nstool/source/NpdmProcess.h +++ b/programs/nstool/source/NpdmProcess.h @@ -13,7 +13,7 @@ public: void process(); - void setInputFile(fnd::IFile& reader); + void setInputFile(fnd::IFile* reader); void setInputFileOffset(size_t offset); void setKeyset(const sKeyset* keyset); void setCliOutputMode(CliOutputType type); diff --git a/programs/nstool/source/OffsetAdjustedIFile.cpp b/programs/nstool/source/OffsetAdjustedIFile.cpp index fdf7cdb..bda80d6 100644 --- a/programs/nstool/source/OffsetAdjustedIFile.cpp +++ b/programs/nstool/source/OffsetAdjustedIFile.cpp @@ -1,6 +1,6 @@ #include "OffsetAdjustedIFile.h" -OffsetAdjustedIFile::OffsetAdjustedIFile(fnd::IFile& file, size_t offset, size_t size) : +OffsetAdjustedIFile::OffsetAdjustedIFile(fnd::IFile* file, size_t offset, size_t size) : mFile(file), mBaseOffset(offset), mCurrentOffset(0), @@ -17,13 +17,13 @@ size_t OffsetAdjustedIFile::size() void OffsetAdjustedIFile::seek(size_t offset) { mCurrentOffset = offset; - mFile.seek(offset + mBaseOffset); + mFile->seek(offset + mBaseOffset); } void OffsetAdjustedIFile::read(byte_t* out, size_t len) { seek(mCurrentOffset); - mFile.read(out, len); + mFile->read(out, len); } void OffsetAdjustedIFile::read(byte_t* out, size_t offset, size_t len) @@ -35,7 +35,7 @@ void OffsetAdjustedIFile::read(byte_t* out, size_t offset, size_t len) void OffsetAdjustedIFile::write(const byte_t* out, size_t len) { seek(mCurrentOffset); - mFile.write(out, len); + mFile->write(out, len); } void OffsetAdjustedIFile::write(const byte_t* out, size_t offset, size_t len) diff --git a/programs/nstool/source/OffsetAdjustedIFile.h b/programs/nstool/source/OffsetAdjustedIFile.h index 18b3b9c..5b60dcf 100644 --- a/programs/nstool/source/OffsetAdjustedIFile.h +++ b/programs/nstool/source/OffsetAdjustedIFile.h @@ -3,7 +3,7 @@ class OffsetAdjustedIFile : public fnd::IFile { public: - OffsetAdjustedIFile(fnd::IFile& file, size_t offset, size_t size); + OffsetAdjustedIFile(fnd::IFile* file, size_t offset, size_t size); size_t size(); void seek(size_t offset); @@ -12,7 +12,7 @@ public: void write(const byte_t* out, size_t len); void write(const byte_t* out, size_t offset, size_t len); private: - fnd::IFile& mFile; + fnd::IFile* mFile; size_t mBaseOffset, mCurrentOffset; size_t mSize; }; \ No newline at end of file diff --git a/programs/nstool/source/PfsProcess.cpp b/programs/nstool/source/PfsProcess.cpp index 8577e8f..fa04bd3 100644 --- a/programs/nstool/source/PfsProcess.cpp +++ b/programs/nstool/source/PfsProcess.cpp @@ -139,9 +139,9 @@ void PfsProcess::process() extractFs(); } -void PfsProcess::setInputFile(fnd::IFile& reader) +void PfsProcess::setInputFile(fnd::IFile* reader) { - mReader = &reader; + mReader = reader; } void PfsProcess::setInputFileOffset(size_t offset) diff --git a/programs/nstool/source/PfsProcess.h b/programs/nstool/source/PfsProcess.h index 7a07a80..bf062c0 100644 --- a/programs/nstool/source/PfsProcess.h +++ b/programs/nstool/source/PfsProcess.h @@ -14,7 +14,7 @@ public: void process(); // generic - void setInputFile(fnd::IFile& reader); + void setInputFile(fnd::IFile* reader); void setInputFileOffset(size_t offset); void setKeyset(const sKeyset* keyset); void setCliOutputMode(CliOutputType type); diff --git a/programs/nstool/source/RomfsProcess.cpp b/programs/nstool/source/RomfsProcess.cpp index 0c3d8a1..95aa7c5 100644 --- a/programs/nstool/source/RomfsProcess.cpp +++ b/programs/nstool/source/RomfsProcess.cpp @@ -256,9 +256,9 @@ void RomfsProcess::process() extractFs(); } -void RomfsProcess::setInputFile(fnd::IFile& reader) +void RomfsProcess::setInputFile(fnd::IFile* reader) { - mReader = &reader; + mReader = reader; } void RomfsProcess::setInputFileOffset(size_t offset) diff --git a/programs/nstool/source/RomfsProcess.h b/programs/nstool/source/RomfsProcess.h index 42cb54f..ee3ecc1 100644 --- a/programs/nstool/source/RomfsProcess.h +++ b/programs/nstool/source/RomfsProcess.h @@ -93,7 +93,7 @@ public: void process(); // generic - void setInputFile(fnd::IFile& reader); + void setInputFile(fnd::IFile* reader); void setInputFileOffset(size_t offset); void setKeyset(const sKeyset* keyset); void setCliOutputMode(CliOutputType type); diff --git a/programs/nstool/source/XciProcess.cpp b/programs/nstool/source/XciProcess.cpp index 3e0ae68..54ffdbf 100644 --- a/programs/nstool/source/XciProcess.cpp +++ b/programs/nstool/source/XciProcess.cpp @@ -141,7 +141,7 @@ void XciProcess::processRootPfs() printf("[WARNING] XCI Root HFS0: FAIL (bad hash)\n"); } } - mRootPfs.setInputFile(*mReader); + mRootPfs.setInputFile(mReader); mRootPfs.setInputFileOffset(mOffset + mHdr.getPartitionFsAddress()); mRootPfs.setListFs(mListFs); mRootPfs.setVerifyMode(mVerify); @@ -156,7 +156,7 @@ void XciProcess::processPartitionPfs() for (size_t i = 0; i < rootPartitions.getSize(); i++) { PfsProcess tmp; - tmp.setInputFile(*mReader); + tmp.setInputFile(mReader); tmp.setInputFileOffset(mOffset + mHdr.getPartitionFsAddress() + rootPartitions[i].offset); tmp.setListFs(mListFs); tmp.setVerifyMode(mVerify); @@ -227,9 +227,9 @@ void XciProcess::process() processPartitionPfs(); } -void XciProcess::setInputFile(fnd::IFile& reader) +void XciProcess::setInputFile(fnd::IFile* reader) { - mReader = &reader; + mReader = reader; } void XciProcess::setInputFileOffset(size_t offset) diff --git a/programs/nstool/source/XciProcess.h b/programs/nstool/source/XciProcess.h index 5141515..d0b7061 100644 --- a/programs/nstool/source/XciProcess.h +++ b/programs/nstool/source/XciProcess.h @@ -17,7 +17,7 @@ public: void process(); // generic - void setInputFile(fnd::IFile& reader); + void setInputFile(fnd::IFile* reader); void setInputFileOffset(size_t offset); void setKeyset(const sKeyset* keyset); void setCliOutputMode(CliOutputType type); diff --git a/programs/nstool/source/main.cpp b/programs/nstool/source/main.cpp index 8296f9a..8907c39 100644 --- a/programs/nstool/source/main.cpp +++ b/programs/nstool/source/main.cpp @@ -21,7 +21,7 @@ int main(int argc, char** argv) { XciProcess xci; - xci.setInputFile(inputFile); + xci.setInputFile(&inputFile); xci.setKeyset(&user_set.getKeyset()); xci.setCliOutputMode(user_set.getCliOutputType()); @@ -41,7 +41,7 @@ int main(int argc, char** argv) { PfsProcess pfs; - pfs.setInputFile(inputFile); + pfs.setInputFile(&inputFile); pfs.setKeyset(&user_set.getKeyset()); pfs.setCliOutputMode(user_set.getCliOutputType()); pfs.setVerifyMode(user_set.isVerifyFile()); @@ -56,7 +56,7 @@ int main(int argc, char** argv) { RomfsProcess romfs; - romfs.setInputFile(inputFile); + romfs.setInputFile(&inputFile); romfs.setKeyset(&user_set.getKeyset()); romfs.setCliOutputMode(user_set.getCliOutputType()); romfs.setVerifyMode(user_set.isVerifyFile()); @@ -93,7 +93,7 @@ int main(int argc, char** argv) { NpdmProcess npdm; - npdm.setInputFile(inputFile); + npdm.setInputFile(&inputFile); npdm.setKeyset(&user_set.getKeyset()); npdm.setCliOutputMode(user_set.getCliOutputType()); npdm.setVerifyMode(user_set.isVerifyFile());