mirror of
https://github.com/jakcron/nstool.git
synced 2024-12-22 10:45:28 +00:00
[nstool] Where not guarenteed to control file position, IFile wrappers assert their position.
This commit is contained in:
parent
e1ccf81e54
commit
33313f208d
|
@ -1,19 +1,16 @@
|
|||
#include "AesCtrWrappedIFile.h"
|
||||
|
||||
AesCtrWrappedIFile::AesCtrWrappedIFile(fnd::IFile* file, const crypto::aes::sAes128Key& key, const crypto::aes::sAesIvCtr& ctr) :
|
||||
mOwnIFile(false),
|
||||
mFile(file),
|
||||
mKey(key),
|
||||
mBaseCtr(ctr)
|
||||
AesCtrWrappedIFile(file, false, key, ctr)
|
||||
{
|
||||
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)
|
||||
mBaseCtr(ctr),
|
||||
mFileOffset(0)
|
||||
{
|
||||
mScratch.alloc(kAesCtrScratchAllocSize);
|
||||
}
|
||||
|
@ -33,13 +30,13 @@ size_t AesCtrWrappedIFile::size()
|
|||
|
||||
void AesCtrWrappedIFile::seek(size_t offset)
|
||||
{
|
||||
mFile->seek(offset);
|
||||
crypto::aes::AesIncrementCounter(mBaseCtr.iv, offset>>4, mCurrentCtr.iv);
|
||||
mBlockOffset = offset & 0xf;
|
||||
mFileOffset = offset;
|
||||
}
|
||||
|
||||
void AesCtrWrappedIFile::read(byte_t* out, size_t len)
|
||||
{
|
||||
internalSeek();
|
||||
|
||||
for (size_t i = 0; i < (len / kAesCtrScratchSize); i++)
|
||||
{
|
||||
mFile->read(mScratch.getBytes() + mBlockOffset, kAesCtrScratchSize);
|
||||
|
@ -55,6 +52,8 @@ void AesCtrWrappedIFile::read(byte_t* out, size_t len)
|
|||
crypto::aes::AesCtr(mScratch.getBytes(), kAesCtrScratchAllocSize, mKey.key, mCurrentCtr.iv, mScratch.getBytes());
|
||||
memcpy(out + read_pos, mScratch.getBytes() + mBlockOffset, read_len);
|
||||
}
|
||||
|
||||
seek(mFileOffset + len);
|
||||
}
|
||||
|
||||
void AesCtrWrappedIFile::read(byte_t* out, size_t offset, size_t len)
|
||||
|
@ -65,6 +64,8 @@ void AesCtrWrappedIFile::read(byte_t* out, size_t offset, size_t len)
|
|||
|
||||
void AesCtrWrappedIFile::write(const byte_t* out, size_t len)
|
||||
{
|
||||
internalSeek();
|
||||
|
||||
for (size_t i = 0; i < (len / kAesCtrScratchSize); i++)
|
||||
{
|
||||
memcpy(mScratch.getBytes() + mBlockOffset, out + (i * kAesCtrScratchSize), kAesCtrScratchSize);
|
||||
|
@ -80,10 +81,19 @@ void AesCtrWrappedIFile::write(const byte_t* out, size_t len)
|
|||
crypto::aes::AesCtr(mScratch.getBytes(), kAesCtrScratchAllocSize, mKey.key, mCurrentCtr.iv, mScratch.getBytes());
|
||||
mFile->write(mScratch.getBytes() + mBlockOffset, write_len);
|
||||
}
|
||||
|
||||
seek(mFileOffset + len);
|
||||
}
|
||||
|
||||
void AesCtrWrappedIFile::write(const byte_t* out, size_t offset, size_t len)
|
||||
{
|
||||
seek(offset);
|
||||
write(out, len);
|
||||
}
|
||||
|
||||
void AesCtrWrappedIFile::internalSeek()
|
||||
{
|
||||
mFile->seek(mFileOffset);
|
||||
crypto::aes::AesIncrementCounter(mBaseCtr.iv, mFileOffset>>4, mCurrentCtr.iv);
|
||||
mBlockOffset = mFileOffset & 0xf;
|
||||
}
|
|
@ -25,6 +25,9 @@ private:
|
|||
crypto::aes::sAes128Key mKey;
|
||||
crypto::aes::sAesIvCtr mBaseCtr, mCurrentCtr;
|
||||
size_t mBlockOffset;
|
||||
size_t mFileOffset;
|
||||
|
||||
fnd::MemoryBlob mScratch;
|
||||
|
||||
void internalSeek();
|
||||
};
|
|
@ -43,7 +43,7 @@ void OffsetAdjustedIFile::read(byte_t* out, size_t len)
|
|||
// assert proper position in file
|
||||
mFile->seek(mCurrentOffset + mBaseOffset);
|
||||
mFile->read(out, len);
|
||||
mCurrentOffset += len;
|
||||
seek(mCurrentOffset + len);
|
||||
}
|
||||
|
||||
void OffsetAdjustedIFile::read(byte_t* out, size_t offset, size_t len)
|
||||
|
@ -57,7 +57,7 @@ void OffsetAdjustedIFile::write(const byte_t* out, size_t len)
|
|||
// assert proper position in file
|
||||
mFile->seek(mCurrentOffset + mBaseOffset);
|
||||
mFile->write(out, len);
|
||||
mCurrentOffset += len;
|
||||
seek(mCurrentOffset + len);
|
||||
}
|
||||
|
||||
void OffsetAdjustedIFile::write(const byte_t* out, size_t offset, size_t len)
|
||||
|
|
Loading…
Reference in a new issue