[nstool] Wrapped IFile classes now can optionally specify when constructed if they own their IFile pointer. By default they do not.

This commit is contained in:
jakcron 2018-05-21 20:48:59 +08:00
parent 446927b53e
commit 1e75262f3f
4 changed files with 43 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#include "AesCtrWrappedIFile.h" #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) :
mOwnIFile(false),
mFile(file), mFile(file),
mKey(key), mKey(key),
mBaseCtr(ctr) mBaseCtr(ctr)
@ -8,6 +9,23 @@ AesCtrWrappedIFile::AesCtrWrappedIFile(fnd::IFile* file, const crypto::aes::sAes
mScratch.alloc(kAesCtrScratchAllocSize); mScratch.alloc(kAesCtrScratchAllocSize);
} }
AesCtrWrappedIFile::AesCtrWrappedIFile(fnd::IFile* file, bool ownIfile, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr) :
mOwnIFile(ownIfile),
mFile(file),
mKey(key),
mBaseCtr(ctr)
{
mScratch.alloc(kAesCtrScratchAllocSize);
}
AesCtrWrappedIFile::~AesCtrWrappedIFile()
{
if (mOwnIFile)
{
delete mFile;
}
}
size_t AesCtrWrappedIFile::size() size_t AesCtrWrappedIFile::size()
{ {
return mFile->size(); return mFile->size();

View file

@ -6,6 +6,8 @@ class AesCtrWrappedIFile : public fnd::IFile
{ {
public: 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);
AesCtrWrappedIFile(fnd::IFile* file, bool ownIfile, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr);
~AesCtrWrappedIFile();
size_t size(); size_t size();
void seek(size_t offset); void seek(size_t offset);
@ -18,6 +20,7 @@ private:
static const size_t kAesCtrScratchSize = 0x1000000; static const size_t kAesCtrScratchSize = 0x1000000;
static const size_t kAesCtrScratchAllocSize = kAesCtrScratchSize + crypto::aes::kAesBlockSize; static const size_t kAesCtrScratchAllocSize = kAesCtrScratchSize + crypto::aes::kAesBlockSize;
bool mOwnIFile;
fnd::IFile* mFile; fnd::IFile* mFile;
crypto::aes::sAes128Key mKey; crypto::aes::sAes128Key mKey;
crypto::aes::sAesIvCtr mBaseCtr, mCurrentCtr; crypto::aes::sAesIvCtr mBaseCtr, mCurrentCtr;

View file

@ -1,6 +1,7 @@
#include "OffsetAdjustedIFile.h" #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) :
mOwnIFile(false),
mFile(file), mFile(file),
mBaseOffset(offset), mBaseOffset(offset),
mCurrentOffset(0), mCurrentOffset(0),
@ -9,6 +10,24 @@ OffsetAdjustedIFile::OffsetAdjustedIFile(fnd::IFile* file, size_t offset, size_t
} }
OffsetAdjustedIFile::OffsetAdjustedIFile(fnd::IFile* file, bool ownIFile, size_t offset, size_t size) :
mOwnIFile(ownIFile),
mFile(file),
mBaseOffset(offset),
mCurrentOffset(0),
mSize(size)
{
}
OffsetAdjustedIFile::~OffsetAdjustedIFile()
{
if (mOwnIFile)
{
delete mFile;
}
}
size_t OffsetAdjustedIFile::size() size_t OffsetAdjustedIFile::size()
{ {
return mSize; return mSize;

View file

@ -4,6 +4,8 @@ class OffsetAdjustedIFile : public fnd::IFile
{ {
public: public:
OffsetAdjustedIFile(fnd::IFile* file, size_t offset, size_t size); OffsetAdjustedIFile(fnd::IFile* file, size_t offset, size_t size);
OffsetAdjustedIFile(fnd::IFile* file, bool ownIFile, size_t offset, size_t size);
~OffsetAdjustedIFile();
size_t size(); size_t size();
void seek(size_t offset); void seek(size_t offset);
@ -12,6 +14,7 @@ public:
void write(const byte_t* out, size_t len); void write(const byte_t* out, size_t len);
void write(const byte_t* out, size_t offset, size_t len); void write(const byte_t* out, size_t offset, size_t len);
private: private:
bool mOwnIFile;
fnd::IFile* mFile; fnd::IFile* mFile;
size_t mBaseOffset, mCurrentOffset; size_t mBaseOffset, mCurrentOffset;
size_t mSize; size_t mSize;