Browse Source

Allow plSecureStream to open any arbitrary hsStream in read-only mode

Adam Johnson 13 years ago
parent
commit
83ff761632
  1. 57
      Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp
  2. 3
      Sources/Plasma/PubUtilLib/plFile/plSecureStream.h

57
Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp

@ -77,6 +77,22 @@ fDeleteOnExit(deleteOnExit)
memcpy(&fKey, &kDefaultKey, sizeof(kDefaultKey)); memcpy(&fKey, &kDefaultKey, sizeof(kDefaultKey));
} }
plSecureStream::plSecureStream(hsStream* base, UInt32* key) :
fRef(INVALID_HANDLE_VALUE),
fActualFileSize(0),
fBufferedStream(false),
fRAMStream(nil),
fWriteFileName(nil),
fOpenMode(kOpenFail),
fDeleteOnExit(false)
{
if (key)
memcpy(&fKey, key, sizeof(kDefaultKey));
else
memcpy(&fKey, &kDefaultKey, sizeof(kDefaultKey));
Open(base);
}
plSecureStream::~plSecureStream() plSecureStream::~plSecureStream()
{ {
} }
@ -232,6 +248,39 @@ hsBool plSecureStream::Open(const wchar* name, const wchar* mode)
} }
} }
hsBool plSecureStream::Open(hsStream* stream)
{
UInt32 pos = stream->GetPosition();
stream->Rewind();
if (!ICheckMagicString(stream))
return false;
fActualFileSize = stream->ReadLE32();
UInt32 trimSize = kMagicStringLen + sizeof(UInt32) + fActualFileSize;
fRAMStream = new hsRAMStream;
while (!stream->AtEnd())
{
// Don't write out any garbage
UInt32 size;
if ((trimSize - stream->GetPosition()) < kEncryptChunkSize)
size = (trimSize - stream->GetPosition());
else
size = kEncryptChunkSize;
UInt8 buf[kEncryptChunkSize];
stream->Read(kEncryptChunkSize, &buf);
IDecipher((UInt32*)&buf, kEncryptChunkSize / sizeof(UInt32));
fRAMStream->Write(size, &buf);
}
stream->SetPosition(pos);
fRAMStream->Rewind();
fPosition = 0;
fBufferedStream = true;
fOpenMode = kOpenRead;
return true;
}
hsBool plSecureStream::Close() hsBool plSecureStream::Close()
{ {
int rtn = false; int rtn = false;
@ -598,6 +647,14 @@ bool plSecureStream::FileDecrypt(const wchar* fileName, UInt32* key /* = nil */)
return true; return true;
} }
bool plSecureStream::ICheckMagicString(hsStream* s)
{
char magicString[kMagicStringLen+1];
s->Read(kMagicStringLen, &magicString);
magicString[kMagicStringLen] = '\0';
return (hsStrEQ(magicString, kMagicString) != 0);
}
bool plSecureStream::ICheckMagicString(hsFD fp) bool plSecureStream::ICheckMagicString(hsFD fp)
{ {
char magicString[kMagicStringLen+1]; char magicString[kMagicStringLen+1];

3
Sources/Plasma/PubUtilLib/plFile/plSecureStream.h

@ -85,13 +85,16 @@ protected:
bool IWriteEncrypted(hsStream* sourceStream, const wchar* outputFile); bool IWriteEncrypted(hsStream* sourceStream, const wchar* outputFile);
static bool ICheckMagicString(hsFD fp); static bool ICheckMagicString(hsFD fp);
static bool ICheckMagicString(hsStream* s);
public: public:
plSecureStream(hsBool deleteOnExit = false, UInt32* key = nil); // uses default key if you don't pass one in plSecureStream(hsBool deleteOnExit = false, UInt32* key = nil); // uses default key if you don't pass one in
plSecureStream(hsStream* base, UInt32* key = nil);
~plSecureStream(); ~plSecureStream();
virtual hsBool Open(const char* name, const char* mode = "rb"); virtual hsBool Open(const char* name, const char* mode = "rb");
virtual hsBool Open(const wchar* name, const wchar* mode = L"rb"); virtual hsBool Open(const wchar* name, const wchar* mode = L"rb");
hsBool Open(hsStream* stream);
virtual hsBool Close(); virtual hsBool Close();
virtual UInt32 Read(UInt32 byteCount, void* buffer); virtual UInt32 Read(UInt32 byteCount, void* buffer);

Loading…
Cancel
Save