mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-17 02:51:27 +00:00
Replace hsStream::Open duplicated methods everywhere with a single plFileName interface
This commit is contained in:
@ -117,21 +117,11 @@ void plEncryptedStream::IDecipher(uint32_t* const v)
|
||||
v[0]=y; v[1]=z;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::Open(const char* name, const char* mode)
|
||||
bool plEncryptedStream::Open(const plFileName& name, const char* mode)
|
||||
{
|
||||
wchar_t* wName = hsStringToWString(name);
|
||||
wchar_t* wMode = hsStringToWString(mode);
|
||||
bool ret = Open(wName, wMode);
|
||||
delete [] wName;
|
||||
delete [] wMode;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::Open(const wchar_t* name, const wchar_t* mode)
|
||||
{
|
||||
if (wcscmp(mode, L"rb") == 0)
|
||||
if (strcmp(mode, "rb") == 0)
|
||||
{
|
||||
fRef = hsWFopen(name, mode);
|
||||
fRef = plFileSystem::Open(name, mode);
|
||||
fPosition = 0;
|
||||
|
||||
if (!fRef)
|
||||
@ -156,11 +146,10 @@ bool plEncryptedStream::Open(const wchar_t* name, const wchar_t* mode)
|
||||
|
||||
return true;
|
||||
}
|
||||
else if (wcscmp(mode, L"wb") == 0)
|
||||
else if (strcmp(mode, "wb") == 0)
|
||||
{
|
||||
fRAMStream = new hsVectorStream;
|
||||
fWriteFileName = new wchar_t[wcslen(name) + 1];
|
||||
wcscpy(fWriteFileName, name);
|
||||
fWriteFileName = name;
|
||||
fPosition = 0;
|
||||
|
||||
fOpenMode = kOpenWrite;
|
||||
@ -197,12 +186,7 @@ bool plEncryptedStream::Close()
|
||||
fRAMStream = nil;
|
||||
}
|
||||
|
||||
if (fWriteFileName)
|
||||
{
|
||||
delete [] fWriteFileName;
|
||||
fWriteFileName = nil;
|
||||
}
|
||||
|
||||
fWriteFileName = plString::Null;
|
||||
fActualFileSize = 0;
|
||||
fBufferedStream = false;
|
||||
fOpenMode = kOpenFail;
|
||||
@ -389,11 +373,11 @@ uint32_t plEncryptedStream::Write(uint32_t bytes, const void* buffer)
|
||||
return fRAMStream->Write(bytes, buffer);
|
||||
}
|
||||
|
||||
bool plEncryptedStream::IWriteEncypted(hsStream* sourceStream, const wchar_t* outputFile)
|
||||
bool plEncryptedStream::IWriteEncypted(hsStream* sourceStream, const plFileName& outputFile)
|
||||
{
|
||||
hsUNIXStream outputStream;
|
||||
|
||||
if (!outputStream.Open(outputFile, L"wb"))
|
||||
if (!outputStream.Open(outputFile, "wb"))
|
||||
return false;
|
||||
|
||||
outputStream.Write(kMagicStringLen, kMagicString);
|
||||
@ -439,15 +423,7 @@ bool plEncryptedStream::IWriteEncypted(hsStream* sourceStream, const wchar_t* ou
|
||||
return true;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::FileEncrypt(const char* fileName)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
bool ret = FileEncrypt(wFilename);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::FileEncrypt(const wchar_t* fileName)
|
||||
bool plEncryptedStream::FileEncrypt(const plFileName& fileName)
|
||||
{
|
||||
hsUNIXStream sIn;
|
||||
if (!sIn.Open(fileName))
|
||||
@ -462,36 +438,28 @@ bool plEncryptedStream::FileEncrypt(const wchar_t* fileName)
|
||||
sIn.Rewind();
|
||||
|
||||
plEncryptedStream sOut;
|
||||
bool wroteEncrypted = sOut.IWriteEncypted(&sIn, L"crypt.dat");
|
||||
bool wroteEncrypted = sOut.IWriteEncypted(&sIn, "crypt.dat");
|
||||
|
||||
sIn.Close();
|
||||
sOut.Close();
|
||||
|
||||
if (wroteEncrypted)
|
||||
{
|
||||
plFileUtils::RemoveFile(fileName);
|
||||
plFileUtils::FileMove(L"crypt.dat", fileName);
|
||||
plFileSystem::Unlink(fileName);
|
||||
plFileSystem::Move("crypt.dat", fileName);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::FileDecrypt(const char* fileName)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
bool ret = FileDecrypt(wFilename);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::FileDecrypt(const wchar_t* fileName)
|
||||
bool plEncryptedStream::FileDecrypt(const plFileName& fileName)
|
||||
{
|
||||
plEncryptedStream sIn;
|
||||
if (!sIn.Open(fileName))
|
||||
return false;
|
||||
|
||||
hsUNIXStream sOut;
|
||||
if (!sOut.Open(L"crypt.dat", L"wb"))
|
||||
if (!sOut.Open("crypt.dat", "wb"))
|
||||
{
|
||||
sIn.Close();
|
||||
return false;
|
||||
@ -508,32 +476,23 @@ bool plEncryptedStream::FileDecrypt(const wchar_t* fileName)
|
||||
sIn.Close();
|
||||
sOut.Close();
|
||||
|
||||
plFileUtils::RemoveFile(fileName);
|
||||
plFileUtils::FileMove(L"crypt.dat", fileName);
|
||||
plFileSystem::Unlink(fileName);
|
||||
plFileSystem::Move("crypt.dat", fileName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::ICheckMagicString(FILE* fp)
|
||||
{
|
||||
char magicString[kMagicStringLen+1];
|
||||
char magicString[kMagicStringLen];
|
||||
fread(&magicString, kMagicStringLen, 1, fp);
|
||||
magicString[kMagicStringLen] = '\0';
|
||||
return strcmp(magicString, kMagicString) == 0 ||
|
||||
strcmp(magicString, kOldMagicString) == 0;
|
||||
return memcmp(magicString, kMagicString, kMagicStringLen) == 0 ||
|
||||
memcmp(magicString, kOldMagicString, kMagicStringLen) == 0;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::IsEncryptedFile(const char* fileName)
|
||||
bool plEncryptedStream::IsEncryptedFile(const plFileName& fileName)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
bool ret = IsEncryptedFile(wFilename);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plEncryptedStream::IsEncryptedFile(const wchar_t* fileName)
|
||||
{
|
||||
FILE* fp = hsWFopen(fileName, L"rb");
|
||||
FILE* fp = plFileSystem::Open(fileName, "rb");
|
||||
if (!fp)
|
||||
return false;
|
||||
|
||||
@ -544,15 +503,7 @@ bool plEncryptedStream::IsEncryptedFile(const wchar_t* fileName)
|
||||
return isEncrypted;
|
||||
}
|
||||
|
||||
hsStream* plEncryptedStream::OpenEncryptedFile(const char* fileName, uint32_t* cryptKey)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
hsStream* ret = OpenEncryptedFile(wFilename, cryptKey);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
hsStream* plEncryptedStream::OpenEncryptedFile(const wchar_t* fileName, uint32_t* cryptKey)
|
||||
hsStream* plEncryptedStream::OpenEncryptedFile(const plFileName& fileName, uint32_t* cryptKey)
|
||||
{
|
||||
|
||||
bool isEncrypted = IsEncryptedFile(fileName);
|
||||
@ -563,19 +514,11 @@ hsStream* plEncryptedStream::OpenEncryptedFile(const wchar_t* fileName, uint32_t
|
||||
else
|
||||
s = new hsUNIXStream;
|
||||
|
||||
s->Open(fileName, L"rb");
|
||||
s->Open(fileName, "rb");
|
||||
return s;
|
||||
}
|
||||
|
||||
hsStream* plEncryptedStream::OpenEncryptedFileWrite(const char* fileName, uint32_t* cryptKey)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
hsStream* ret = OpenEncryptedFileWrite(wFilename, cryptKey);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
hsStream* plEncryptedStream::OpenEncryptedFileWrite(const wchar_t* fileName, uint32_t* cryptKey)
|
||||
hsStream* plEncryptedStream::OpenEncryptedFileWrite(const plFileName& fileName, uint32_t* cryptKey)
|
||||
{
|
||||
hsStream* s = nil;
|
||||
if (IsEncryptedFile(fileName))
|
||||
@ -583,6 +526,6 @@ hsStream* plEncryptedStream::OpenEncryptedFileWrite(const wchar_t* fileName, uin
|
||||
else
|
||||
s = new hsUNIXStream;
|
||||
|
||||
s->Open(fileName, L"wb");
|
||||
s->Open(fileName, "wb");
|
||||
return s;
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ protected:
|
||||
|
||||
hsStream* fRAMStream;
|
||||
|
||||
wchar_t* fWriteFileName;
|
||||
plFileName fWriteFileName;
|
||||
|
||||
enum OpenMode { kOpenRead, kOpenWrite, kOpenFail };
|
||||
OpenMode fOpenMode;
|
||||
@ -73,7 +73,7 @@ protected:
|
||||
void IEncipher(uint32_t* const v);
|
||||
void IDecipher(uint32_t* const v);
|
||||
|
||||
bool IWriteEncypted(hsStream* sourceStream, const wchar_t* outputFile);
|
||||
bool IWriteEncypted(hsStream* sourceStream, const plFileName& outputFile);
|
||||
|
||||
static bool ICheckMagicString(FILE* fp);
|
||||
|
||||
@ -82,8 +82,7 @@ public:
|
||||
plEncryptedStream(uint32_t* key=nil);
|
||||
~plEncryptedStream();
|
||||
|
||||
virtual bool Open(const char* name, const char* mode = "rb");
|
||||
virtual bool Open(const wchar_t* name, const wchar_t* mode = L"rb");
|
||||
virtual bool Open(const plFileName& name, const char* mode = "rb");
|
||||
virtual bool Close();
|
||||
|
||||
virtual uint32_t Read(uint32_t byteCount, void* buffer);
|
||||
@ -96,21 +95,16 @@ public:
|
||||
|
||||
uint32_t GetActualFileSize() const { return fActualFileSize;}
|
||||
|
||||
static bool FileEncrypt(const char* fileName);
|
||||
static bool FileEncrypt(const wchar_t* fileName);
|
||||
static bool FileDecrypt(const char* fileName);
|
||||
static bool FileDecrypt(const wchar_t* fileName);
|
||||
static bool FileEncrypt(const plFileName& fileName);
|
||||
static bool FileDecrypt(const plFileName& fileName);
|
||||
|
||||
static bool IsEncryptedFile(const char* fileName);
|
||||
static bool IsEncryptedFile(const wchar_t* fileName);
|
||||
static bool IsEncryptedFile(const plFileName& fileName);
|
||||
|
||||
// Attempts to create a read-binary stream for the requested file. If it's
|
||||
// encrypted, you'll get a plEncryptedStream, otherwise just a standard
|
||||
// hsUNIXStream. Remember to delete the stream when you're done with it.
|
||||
static hsStream* OpenEncryptedFile(const char* fileName, uint32_t* cryptKey = nil);
|
||||
static hsStream* OpenEncryptedFile(const wchar_t* fileName, uint32_t* cryptKey = nil);
|
||||
static hsStream* OpenEncryptedFileWrite(const char* fileName, uint32_t* cryptKey = nil);
|
||||
static hsStream* OpenEncryptedFileWrite(const wchar_t* fileName, uint32_t* cryptKey = nil);
|
||||
static hsStream* OpenEncryptedFile(const plFileName& fileName, uint32_t* cryptKey = nil);
|
||||
static hsStream* OpenEncryptedFileWrite(const plFileName& fileName, uint32_t* cryptKey = nil);
|
||||
};
|
||||
|
||||
#endif // plEncryptedStream_h_inc
|
||||
|
@ -493,7 +493,7 @@ uint32_t plFileUtils::GetFileSize( const wchar_t *path )
|
||||
uint32_t len = 0;
|
||||
|
||||
hsUNIXStream str;
|
||||
if (str.Open(path, L"rb"))
|
||||
if (str.Open(plString::FromWchar(path), "rb"))
|
||||
{
|
||||
len = str.GetEOF();
|
||||
str.Close();
|
||||
|
@ -66,7 +66,6 @@ static const int kFileStartOffset = kMagicStringLen + sizeof(uint32_t);
|
||||
static const int kMaxBufferedFileSize = 10*1024;
|
||||
|
||||
const char plSecureStream::kKeyFilename[] = "encryption.key";
|
||||
const wchar_t plSecureStream::kWKeyFilename[] = L"encryption.key";
|
||||
|
||||
plSecureStream::plSecureStream(bool deleteOnExit, uint32_t* key) :
|
||||
fRef(INVALID_HANDLE_VALUE),
|
||||
@ -155,24 +154,14 @@ void plSecureStream::IDecipher(uint32_t* const v, uint32_t n)
|
||||
}
|
||||
}
|
||||
|
||||
bool plSecureStream::Open(const char* name, const char* mode)
|
||||
bool plSecureStream::Open(const plFileName& name, const char* mode)
|
||||
{
|
||||
wchar_t* wName = hsStringToWString(name);
|
||||
wchar_t* wMode = hsStringToWString(mode);
|
||||
bool ret = Open(wName, wMode);
|
||||
delete [] wName;
|
||||
delete [] wMode;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plSecureStream::Open(const wchar_t* name, const wchar_t* mode)
|
||||
{
|
||||
if (wcscmp(mode, L"rb") == 0)
|
||||
if (strcmp(mode, "rb") == 0)
|
||||
{
|
||||
#if HS_BUILD_FOR_WIN32
|
||||
if (fDeleteOnExit)
|
||||
{
|
||||
fRef = CreateFileW(name,
|
||||
fRef = CreateFileW(name.AsString().ToWchar(),
|
||||
GENERIC_READ, // open for reading
|
||||
0, // no one can open the file until we're done
|
||||
NULL, // default security
|
||||
@ -182,7 +171,7 @@ bool plSecureStream::Open(const wchar_t* name, const wchar_t* mode)
|
||||
}
|
||||
else
|
||||
{
|
||||
fRef = CreateFileW(name,
|
||||
fRef = CreateFileW(name.AsString().ToWchar(),
|
||||
GENERIC_READ, // open for reading
|
||||
0, // no one can open the file until we're done
|
||||
NULL, // default security
|
||||
@ -207,10 +196,7 @@ bool plSecureStream::Open(const wchar_t* name, const wchar_t* mode)
|
||||
DWORD numBytesRead;
|
||||
ReadFile(fRef, &fActualFileSize, sizeof(uint32_t), &numBytesRead, NULL);
|
||||
#elif HS_BUILD_FOR_UNIX
|
||||
const char* cname = hsWStringToString(name);
|
||||
fRef = fopen(cname, "rb");
|
||||
delete[] cname;
|
||||
|
||||
fRef = plFileSystem::Open(name, "rb");
|
||||
fPosition = 0;
|
||||
|
||||
if (fRef == INVALID_HANDLE_VALUE)
|
||||
@ -234,11 +220,10 @@ bool plSecureStream::Open(const wchar_t* name, const wchar_t* mode)
|
||||
|
||||
return true;
|
||||
}
|
||||
else if (wcscmp(mode, L"wb") == 0)
|
||||
else if (strcmp(mode, "wb") == 0)
|
||||
{
|
||||
fRAMStream = new hsVectorStream;
|
||||
fWriteFileName = new wchar_t[wcslen(name) + 1];
|
||||
wcscpy(fWriteFileName, name);
|
||||
fWriteFileName = name;
|
||||
fPosition = 0;
|
||||
|
||||
fOpenMode = kOpenWrite;
|
||||
@ -312,12 +297,7 @@ bool plSecureStream::Close()
|
||||
fRAMStream = nil;
|
||||
}
|
||||
|
||||
if (fWriteFileName)
|
||||
{
|
||||
delete [] fWriteFileName;
|
||||
fWriteFileName = nil;
|
||||
}
|
||||
|
||||
fWriteFileName = plString::Null;
|
||||
fActualFileSize = 0;
|
||||
fBufferedStream = false;
|
||||
fOpenMode = kOpenFail;
|
||||
@ -528,11 +508,11 @@ uint32_t plSecureStream::Write(uint32_t bytes, const void* buffer)
|
||||
return fRAMStream->Write(bytes, buffer);
|
||||
}
|
||||
|
||||
bool plSecureStream::IWriteEncrypted(hsStream* sourceStream, const wchar_t* outputFile)
|
||||
bool plSecureStream::IWriteEncrypted(hsStream* sourceStream, const plFileName& outputFile)
|
||||
{
|
||||
hsUNIXStream outputStream;
|
||||
|
||||
if (!outputStream.Open(outputFile, L"wb"))
|
||||
if (!outputStream.Open(outputFile, "wb"))
|
||||
return false;
|
||||
|
||||
outputStream.Write(kMagicStringLen, kMagicString);
|
||||
@ -578,15 +558,7 @@ bool plSecureStream::IWriteEncrypted(hsStream* sourceStream, const wchar_t* outp
|
||||
return true;
|
||||
}
|
||||
|
||||
bool plSecureStream::FileEncrypt(const char* fileName, uint32_t* key /* = nil */)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
bool ret = FileEncrypt(wFilename, key);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plSecureStream::FileEncrypt(const wchar_t* fileName, uint32_t* key /* = nil */)
|
||||
bool plSecureStream::FileEncrypt(const plFileName& fileName, uint32_t* key /* = nil */)
|
||||
{
|
||||
hsUNIXStream sIn;
|
||||
if (!sIn.Open(fileName))
|
||||
@ -601,36 +573,28 @@ bool plSecureStream::FileEncrypt(const wchar_t* fileName, uint32_t* key /* = nil
|
||||
sIn.Rewind();
|
||||
|
||||
plSecureStream sOut(false, key);
|
||||
bool wroteEncrypted = sOut.IWriteEncrypted(&sIn, L"crypt.dat");
|
||||
bool wroteEncrypted = sOut.IWriteEncrypted(&sIn, "crypt.dat");
|
||||
|
||||
sIn.Close();
|
||||
sOut.Close();
|
||||
|
||||
if (wroteEncrypted)
|
||||
{
|
||||
plFileUtils::RemoveFile(fileName);
|
||||
plFileUtils::FileMove(L"crypt.dat", fileName);
|
||||
plFileSystem::Unlink(fileName);
|
||||
plFileSystem::Move("crypt.dat", fileName);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool plSecureStream::FileDecrypt(const char* fileName, uint32_t* key /* = nil */)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
bool ret = FileDecrypt(wFilename, key);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plSecureStream::FileDecrypt(const wchar_t* fileName, uint32_t* key /* = nil */)
|
||||
bool plSecureStream::FileDecrypt(const plFileName& fileName, uint32_t* key /* = nil */)
|
||||
{
|
||||
plSecureStream sIn(false, key);
|
||||
if (!sIn.Open(fileName))
|
||||
return false;
|
||||
|
||||
hsUNIXStream sOut;
|
||||
if (!sOut.Open(L"crypt.dat", L"wb"))
|
||||
if (!sOut.Open("crypt.dat", "wb"))
|
||||
{
|
||||
sIn.Close();
|
||||
return false;
|
||||
@ -647,8 +611,8 @@ bool plSecureStream::FileDecrypt(const wchar_t* fileName, uint32_t* key /* = nil
|
||||
sIn.Close();
|
||||
sOut.Close();
|
||||
|
||||
plFileUtils::RemoveFile(fileName);
|
||||
plFileUtils::FileMove(L"crypt.dat", fileName);
|
||||
plFileSystem::Unlink(fileName);
|
||||
plFileSystem::Move("crypt.dat", fileName);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -674,20 +638,12 @@ bool plSecureStream::ICheckMagicString(hsFD fp)
|
||||
return (strcmp(magicString, kMagicString) == 0);
|
||||
}
|
||||
|
||||
bool plSecureStream::IsSecureFile(const char* fileName)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
bool ret = IsSecureFile(wFilename);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plSecureStream::IsSecureFile(const wchar_t* fileName)
|
||||
bool plSecureStream::IsSecureFile(const plFileName& fileName)
|
||||
{
|
||||
hsFD fp = INVALID_HANDLE_VALUE;
|
||||
|
||||
#if HS_BUILD_FOR_WIN32
|
||||
fp = CreateFileW(fileName,
|
||||
fp = CreateFileW(fileName.AsString().ToWchar(),
|
||||
GENERIC_READ, // open for reading
|
||||
0, // no one can open the file until we're done
|
||||
NULL, // default security
|
||||
@ -695,9 +651,7 @@ bool plSecureStream::IsSecureFile(const wchar_t* fileName)
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file attributes
|
||||
NULL); // no template
|
||||
#elif HS_BUILD_FOR_UNIX
|
||||
const char* cfile = hsWStringToString(fileName);
|
||||
fp = fopen(cfile, "rb");
|
||||
delete[] cfile;
|
||||
fp = plFileSystem::Open(fileName);
|
||||
#endif
|
||||
|
||||
if (fp == INVALID_HANDLE_VALUE)
|
||||
@ -714,15 +668,7 @@ bool plSecureStream::IsSecureFile(const wchar_t* fileName)
|
||||
return isEncrypted;
|
||||
}
|
||||
|
||||
hsStream* plSecureStream::OpenSecureFile(const char* fileName, const uint32_t flags /* = kRequireEncryption */, uint32_t* key /* = nil */)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
hsStream* ret = OpenSecureFile(wFilename, flags, key);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
hsStream* plSecureStream::OpenSecureFile(const wchar_t* fileName, const uint32_t flags /* = kRequireEncryption */, uint32_t* key /* = nil */)
|
||||
hsStream* plSecureStream::OpenSecureFile(const plFileName& fileName, const uint32_t flags /* = kRequireEncryption */, uint32_t* key /* = nil */)
|
||||
{
|
||||
bool requireEncryption = flags & kRequireEncryption;
|
||||
#ifndef PLASMA_EXTERNAL_RELEASE
|
||||
@ -739,19 +685,11 @@ hsStream* plSecureStream::OpenSecureFile(const wchar_t* fileName, const uint32_t
|
||||
s = new hsUNIXStream;
|
||||
|
||||
if (s)
|
||||
s->Open(fileName, L"rb");
|
||||
s->Open(fileName, "rb");
|
||||
return s;
|
||||
}
|
||||
|
||||
hsStream* plSecureStream::OpenSecureFileWrite(const char* fileName, uint32_t* key /* = nil */)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(fileName);
|
||||
hsStream* ret = OpenSecureFileWrite(wFilename, key);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
hsStream* plSecureStream::OpenSecureFileWrite(const wchar_t* fileName, uint32_t* key /* = nil */)
|
||||
hsStream* plSecureStream::OpenSecureFileWrite(const plFileName& fileName, uint32_t* key /* = nil */)
|
||||
{
|
||||
hsStream* s = nil;
|
||||
#ifdef PLASMA_EXTERNAL_RELEASE
|
||||
@ -760,46 +698,22 @@ hsStream* plSecureStream::OpenSecureFileWrite(const wchar_t* fileName, uint32_t*
|
||||
s = new hsUNIXStream;
|
||||
#endif
|
||||
|
||||
s->Open(fileName, L"wb");
|
||||
s->Open(fileName, "wb");
|
||||
return s;
|
||||
}
|
||||
|
||||
//// GetSecureEncryptionKey //////////////////////////////////////////////////
|
||||
|
||||
bool plSecureStream::GetSecureEncryptionKey(const char* filename, uint32_t* key, unsigned length)
|
||||
{
|
||||
wchar_t* wFilename = hsStringToWString(filename);
|
||||
bool ret = GetSecureEncryptionKey(wFilename, key, length);
|
||||
delete [] wFilename;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool plSecureStream::GetSecureEncryptionKey(const wchar_t* filename, uint32_t* key, unsigned length)
|
||||
bool plSecureStream::GetSecureEncryptionKey(const plFileName& filename, uint32_t* key, unsigned length)
|
||||
{
|
||||
// looks for an encryption key file in the same directory, and reads it
|
||||
std::wstring sFilename = filename;
|
||||
plFileName keyFile = plFileName::Join(filename.StripFileName(), kKeyFilename);
|
||||
|
||||
// grab parent directory
|
||||
size_t loc = sFilename.rfind(L"\\");
|
||||
if (loc == std::wstring::npos)
|
||||
loc = sFilename.rfind(L"/");
|
||||
|
||||
std::wstring sDir;
|
||||
if (loc != std::wstring::npos)
|
||||
sDir = sFilename.substr(0, loc);
|
||||
else // no directory
|
||||
sDir = L"./";
|
||||
if ((sDir[sDir.length()-1] != L'/') && (sDir[sDir.length()-1] != L'\\'))
|
||||
sDir += L'/'; // add the slash, if it doesn't has one
|
||||
|
||||
// now add the key filename
|
||||
std::wstring keyFile = sDir + kWKeyFilename;
|
||||
|
||||
if (plFileUtils::FileExists(keyFile.c_str()))
|
||||
if (plFileInfo(keyFile).Exists())
|
||||
{
|
||||
// file exists, read from it
|
||||
hsUNIXStream file;
|
||||
file.Open(keyFile.c_str(), L"rb");
|
||||
file.Open(keyFile, "rb");
|
||||
|
||||
unsigned bytesToRead = length * sizeof(uint32_t);
|
||||
uint8_t* buffer = (uint8_t*)malloc(bytesToRead);
|
||||
|
@ -68,8 +68,8 @@ protected:
|
||||
bool fBufferedStream;
|
||||
|
||||
hsStream* fRAMStream;
|
||||
|
||||
wchar_t* fWriteFileName;
|
||||
|
||||
plFileName fWriteFileName;
|
||||
|
||||
enum OpenMode {kOpenRead, kOpenWrite, kOpenFail};
|
||||
OpenMode fOpenMode;
|
||||
@ -83,7 +83,7 @@ protected:
|
||||
void IEncipher(uint32_t* const v, uint32_t n);
|
||||
void IDecipher(uint32_t* const v, uint32_t n);
|
||||
|
||||
bool IWriteEncrypted(hsStream* sourceStream, const wchar_t* outputFile);
|
||||
bool IWriteEncrypted(hsStream* sourceStream, const plFileName& outputFile);
|
||||
|
||||
static bool ICheckMagicString(hsFD fp);
|
||||
static bool ICheckMagicString(hsStream* s);
|
||||
@ -93,9 +93,8 @@ public:
|
||||
plSecureStream(hsStream* base, uint32_t* key = nil);
|
||||
~plSecureStream();
|
||||
|
||||
virtual bool Open(const char* name, const char* mode = "rb");
|
||||
virtual bool Open(const wchar_t* name, const wchar_t* mode = L"rb");
|
||||
bool Open(hsStream* stream);
|
||||
virtual bool Open(const plFileName& name, const char* mode = "rb");
|
||||
bool Open(hsStream* stream);
|
||||
virtual bool Close();
|
||||
|
||||
virtual uint32_t Read(uint32_t byteCount, void* buffer);
|
||||
@ -108,10 +107,8 @@ public:
|
||||
|
||||
uint32_t GetActualFileSize() const {return fActualFileSize;}
|
||||
|
||||
static bool FileEncrypt(const char* fileName, uint32_t* key = nil);
|
||||
static bool FileEncrypt(const wchar_t* fileName, uint32_t* key = nil);
|
||||
static bool FileDecrypt(const char* fileName, uint32_t* key = nil);
|
||||
static bool FileDecrypt(const wchar_t* fileName, uint32_t* key = nil);
|
||||
static bool FileEncrypt(const plFileName& fileName, uint32_t* key = nil);
|
||||
static bool FileDecrypt(const plFileName& fileName, uint32_t* key = nil);
|
||||
|
||||
enum OpenSecureFileFlags
|
||||
{
|
||||
@ -119,28 +116,23 @@ public:
|
||||
kDeleteOnExit = 0x02,
|
||||
};
|
||||
|
||||
static bool IsSecureFile(const char* fileName);
|
||||
static bool IsSecureFile(const wchar_t* fileName);
|
||||
static bool IsSecureFile(const plFileName& fileName);
|
||||
|
||||
// Attempts to create a read-binary stream for the requested file (delete the stream
|
||||
// when you are done with it!)
|
||||
static hsStream* OpenSecureFile(const char* fileName, const uint32_t flags = kRequireEncryption, uint32_t* key = nil);
|
||||
static hsStream* OpenSecureFile(const wchar_t* fileName, const uint32_t flags = kRequireEncryption, uint32_t* key = nil);
|
||||
static hsStream* OpenSecureFile(const plFileName& fileName, const uint32_t flags = kRequireEncryption, uint32_t* key = nil);
|
||||
// Attempts to create a write-binary stream for the requested file (delete the stream
|
||||
// when you are done with it!)
|
||||
static hsStream* OpenSecureFileWrite(const char* fileName, uint32_t* key = nil);
|
||||
static hsStream* OpenSecureFileWrite(const wchar_t* fileName, uint32_t* key = nil);
|
||||
static hsStream* OpenSecureFileWrite(const plFileName& fileName, uint32_t* key = nil);
|
||||
|
||||
static const uint32_t kDefaultKey[4]; // our default encryption key
|
||||
|
||||
// searches the parent directory of filename for the encryption key file, and reads it
|
||||
// into the key passed in. Returns false if the key file didn't exist (and sets key to
|
||||
// the default key)
|
||||
static bool GetSecureEncryptionKey(const char* filename, uint32_t* key, unsigned length);
|
||||
static bool GetSecureEncryptionKey(const wchar_t* filename, uint32_t* key, unsigned length);
|
||||
static bool GetSecureEncryptionKey(const plFileName& filename, uint32_t* key, unsigned length);
|
||||
|
||||
static const char kKeyFilename[];
|
||||
static const wchar_t kWKeyFilename[];
|
||||
};
|
||||
|
||||
#endif // plSecureStream_h_inc
|
||||
|
@ -51,25 +51,10 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
# include <wctype.h>
|
||||
#endif
|
||||
|
||||
void ToLower(std::wstring& str)
|
||||
{
|
||||
for (unsigned i = 0; i < str.length(); i++)
|
||||
str[i] = towlower(str[i]);
|
||||
}
|
||||
|
||||
void ReplaceSlashes(std::wstring& path, wchar_t replaceWith)
|
||||
{
|
||||
for (unsigned i = 0; i < path.length(); i++)
|
||||
{
|
||||
if ((path[i] == L'\\') || (path[i] == L'/'))
|
||||
path[i] = replaceWith;
|
||||
}
|
||||
}
|
||||
|
||||
void plStreamSource::ICleanup()
|
||||
{
|
||||
// loop through all the file data records, and delete the streams
|
||||
std::map<std::wstring, fileData>::iterator curData;
|
||||
decltype(fFileData.begin()) curData;
|
||||
for (curData = fFileData.begin(); curData != fFileData.end(); curData++)
|
||||
{
|
||||
curData->second.fStream->Close();
|
||||
@ -80,137 +65,85 @@ void plStreamSource::ICleanup()
|
||||
fFileData.clear();
|
||||
}
|
||||
|
||||
void plStreamSource::IBreakupFilename(std::wstring filename, std::wstring& dir, std::wstring& ext)
|
||||
hsStream* plStreamSource::GetFile(const plFileName& filename)
|
||||
{
|
||||
// break the filename up into its parts
|
||||
char* temp = hsWStringToString(filename.c_str());
|
||||
std::string sFilename = temp;
|
||||
std::string sExt = plFileUtils::GetFileExt(temp);
|
||||
plFileUtils::StripFile(temp);
|
||||
std::string sDir = temp;
|
||||
delete [] temp;
|
||||
|
||||
if (sDir == sFilename) // no directory
|
||||
sDir = "";
|
||||
if (sDir != "")
|
||||
if ((sDir[sDir.length()-1] == '/') || (sDir[sDir.length()-1] == '\\'))
|
||||
sDir = sDir.substr(0, sDir.length() - 1); // trim the slash, if it has one
|
||||
|
||||
wchar_t* wTemp;
|
||||
wTemp = hsStringToWString(sDir.c_str());
|
||||
dir = wTemp;
|
||||
delete [] wTemp;
|
||||
wTemp = hsStringToWString(sExt.c_str());
|
||||
ext = wTemp;
|
||||
delete [] wTemp;
|
||||
}
|
||||
|
||||
hsStream* plStreamSource::GetFile(std::wstring filename)
|
||||
{
|
||||
ToLower(filename);
|
||||
ReplaceSlashes(filename, L'/');
|
||||
|
||||
if (fFileData.find(filename) == fFileData.end())
|
||||
plFileName sFilename = filename.Normalize('/');
|
||||
if (fFileData.find(sFilename) == fFileData.end())
|
||||
{
|
||||
#ifndef PLASMA_EXTERNAL_RELEASE
|
||||
// internal releases can pull from disk
|
||||
char* temp = hsWStringToString(filename.c_str());
|
||||
std::string sFilename = temp;
|
||||
delete [] temp;
|
||||
|
||||
if (plFileUtils::FileExists(sFilename.c_str()))
|
||||
if (plFileInfo(filename).Exists())
|
||||
{
|
||||
// file exists on disk, cache it
|
||||
std::wstring dir, ext;
|
||||
IBreakupFilename(filename, dir, ext);
|
||||
fFileData[filename].fFilename = filename;
|
||||
fFileData[filename].fDir = dir;
|
||||
fFileData[filename].fExt = ext;
|
||||
if (plSecureStream::IsSecureFile(sFilename.c_str()))
|
||||
fFileData[sFilename].fFilename = sFilename;
|
||||
fFileData[sFilename].fDir = sFilename.StripFileName();
|
||||
fFileData[sFilename].fExt = sFilename.GetFileExt();
|
||||
if (plSecureStream::IsSecureFile(filename))
|
||||
{
|
||||
uint32_t encryptionKey[4];
|
||||
if (!plSecureStream::GetSecureEncryptionKey(sFilename.c_str(), encryptionKey, 4))
|
||||
if (!plSecureStream::GetSecureEncryptionKey(filename, encryptionKey, 4))
|
||||
{
|
||||
FATAL("Hey camper... You need an NTD key file!");
|
||||
return nil;
|
||||
}
|
||||
|
||||
fFileData[filename].fStream = plSecureStream::OpenSecureFile(sFilename.c_str(), 0, encryptionKey);
|
||||
fFileData[sFilename].fStream = plSecureStream::OpenSecureFile(filename, 0, encryptionKey);
|
||||
}
|
||||
else // otherwise it is an encrypted or plain stream, this call handles both
|
||||
fFileData[filename].fStream = plEncryptedStream::OpenEncryptedFile(sFilename.c_str());
|
||||
fFileData[sFilename].fStream = plEncryptedStream::OpenEncryptedFile(filename);
|
||||
|
||||
return fFileData[filename].fStream;
|
||||
return fFileData[sFilename].fStream;
|
||||
}
|
||||
#endif // PLASMA_EXTERNAL_RELEASE
|
||||
return nil;
|
||||
}
|
||||
return fFileData[filename].fStream;
|
||||
return fFileData[sFilename].fStream;
|
||||
}
|
||||
|
||||
std::vector<std::wstring> plStreamSource::GetListOfNames(std::wstring dir, std::wstring ext)
|
||||
std::vector<plFileName> plStreamSource::GetListOfNames(const plFileName& dir, const plString& ext)
|
||||
{
|
||||
ToLower(ext);
|
||||
ToLower(dir);
|
||||
ReplaceSlashes(dir, L'/');
|
||||
|
||||
if (ext[0] == L'.')
|
||||
ext = ext.substr(1, ext.length()); // trim the dot, if it has one
|
||||
if (dir != L"")
|
||||
if ((dir[dir.length()-1] == L'/') || (dir[dir.length()-1] == L'\\'))
|
||||
dir = dir.substr(0, dir.length() - 1); // trim the slash, if it has one
|
||||
plFileName sDir = dir.Normalize('/');
|
||||
hsAssert(ext.CharAt(0) != '.', "Don't add a dot");
|
||||
|
||||
// loop through all the file data records, and create the list
|
||||
std::vector<std::wstring> retVal;
|
||||
std::map<std::wstring, fileData>::iterator curData;
|
||||
std::vector<plFileName> retVal;
|
||||
decltype(fFileData.begin()) curData;
|
||||
for (curData = fFileData.begin(); curData != fFileData.end(); curData++)
|
||||
{
|
||||
if ((curData->second.fDir == dir.c_str()) && (curData->second.fExt == ext))
|
||||
if ((curData->second.fDir == sDir) && (curData->second.fExt == ext))
|
||||
retVal.push_back(curData->second.fFilename);
|
||||
}
|
||||
|
||||
#ifndef PLASMA_EXTERNAL_RELEASE
|
||||
// in internal releases, we can use on-disk files if they exist
|
||||
// Build the search string as "dir/*.ext"
|
||||
std::wstring wSearchStr = dir + L"/*." + ext;
|
||||
char* temp = hsWStringToString(wSearchStr.c_str());
|
||||
std::string searchStr = temp;
|
||||
delete [] temp;
|
||||
plString searchStr = plFileName::Join(sDir, "*." + ext).AsString();
|
||||
|
||||
hsFolderIterator folderIter(searchStr.c_str(), true);
|
||||
while (folderIter.NextFile())
|
||||
{
|
||||
const char* filename = folderIter.GetFileName();
|
||||
wchar_t* wTemp = hsStringToWString(filename);
|
||||
std::wstring wFilename = dir + L"/" + wTemp;
|
||||
delete [] wTemp;
|
||||
ToLower(wFilename);
|
||||
|
||||
if (fFileData.find(wFilename) == fFileData.end()) // we haven't added it yet
|
||||
retVal.push_back(wFilename);
|
||||
plFileName filename = plFileName::Join(sDir, folderIter.GetFileName());
|
||||
|
||||
if (fFileData.find(filename) == fFileData.end()) // we haven't added it yet
|
||||
retVal.push_back(filename);
|
||||
}
|
||||
#endif // PLASMA_EXTERNAL_RELEASE
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
bool plStreamSource::InsertFile(std::wstring filename, hsStream* stream)
|
||||
bool plStreamSource::InsertFile(const plFileName& filename, hsStream* stream)
|
||||
{
|
||||
ToLower(filename);
|
||||
ReplaceSlashes(filename, L'/');
|
||||
plFileName sFilename = filename.Normalize('/');
|
||||
|
||||
if (fFileData.find(filename) != fFileData.end())
|
||||
if (fFileData.find(sFilename) != fFileData.end())
|
||||
return false; // duplicate entry, return failure
|
||||
|
||||
// break the filename up into its parts
|
||||
std::wstring dir, ext;
|
||||
IBreakupFilename(filename, dir, ext);
|
||||
|
||||
// copy the data over (takes ownership of the stream!)
|
||||
fFileData[filename].fFilename = filename;
|
||||
fFileData[filename].fDir = dir;
|
||||
fFileData[filename].fExt = ext;
|
||||
fFileData[filename].fStream = stream;
|
||||
fFileData[sFilename].fFilename = sFilename;
|
||||
fFileData[sFilename].fDir = sFilename.StripFileName();
|
||||
fFileData[sFilename].fExt = sFilename.GetFileExt();
|
||||
fFileData[sFilename].fStream = stream;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -54,15 +54,14 @@ class plStreamSource
|
||||
private:
|
||||
struct fileData
|
||||
{
|
||||
std::wstring fFilename; // includes path
|
||||
std::wstring fDir; // parent directory
|
||||
std::wstring fExt;
|
||||
plFileName fFilename; // includes path
|
||||
plFileName fDir; // parent directory
|
||||
plString fExt;
|
||||
hsStream* fStream; // we own this pointer, so clean it up
|
||||
};
|
||||
std::map<std::wstring, fileData> fFileData; // key is filename
|
||||
std::map<plFileName, fileData, plFileName::less_i> fFileData; // key is filename
|
||||
|
||||
void ICleanup(); // closes all file pointers and cleans up after itself
|
||||
void IBreakupFilename(std::wstring filename, std::wstring& dir, std::wstring& ext);
|
||||
|
||||
plStreamSource() {}
|
||||
public:
|
||||
@ -72,11 +71,11 @@ public:
|
||||
void Cleanup() {ICleanup();}
|
||||
|
||||
// File access functions
|
||||
hsStream* GetFile(std::wstring filename); // internal builds will read from disk if it doesn't exist
|
||||
std::vector<std::wstring> GetListOfNames(std::wstring dir, std::wstring ext); // internal builds merge from disk
|
||||
hsStream* GetFile(const plFileName& filename); // internal builds will read from disk if it doesn't exist
|
||||
std::vector<plFileName> GetListOfNames(const plFileName& dir, const plString& ext); // internal builds merge from disk
|
||||
|
||||
// For other classes to insert files (takes ownership of the stream if successful)
|
||||
bool InsertFile(std::wstring filename, hsStream* stream);
|
||||
bool InsertFile(const plFileName& filename, hsStream* stream);
|
||||
|
||||
// Instance handling
|
||||
static plStreamSource* GetInstance();
|
||||
|
Reference in New Issue
Block a user