mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-17 10:52:46 +00:00
Fix paths and move GetSecureEncryptionKey so things compile again
This commit is contained in:
@ -62,6 +62,9 @@ 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),
|
||||
fActualFileSize(0),
|
||||
@ -757,3 +760,61 @@ hsStream* plSecureStream::OpenSecureFileWrite(const wchar_t* fileName, uint32_t*
|
||||
s->Open(fileName, L"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)
|
||||
{
|
||||
// looks for an encryption key file in the same directory, and reads it
|
||||
std::wstring sFilename = filename;
|
||||
|
||||
// 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()))
|
||||
{
|
||||
// file exists, read from it
|
||||
hsUNIXStream file;
|
||||
file.Open(keyFile.c_str(), L"rb");
|
||||
|
||||
unsigned bytesToRead = length * sizeof(uint32_t);
|
||||
uint8_t* buffer = (uint8_t*)malloc(bytesToRead);
|
||||
unsigned bytesRead = file.Read(bytesToRead, buffer);
|
||||
|
||||
file.Close();
|
||||
|
||||
unsigned memSize = min(bytesToRead, bytesRead);
|
||||
memcpy(key, buffer, memSize);
|
||||
free(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// file doesn't exist, use default key
|
||||
unsigned memSize = min(length, arrsize(plSecureStream::kDefaultKey));
|
||||
memSize *= sizeof(uint32_t);
|
||||
memcpy(key, plSecureStream::kDefaultKey, memSize);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -131,6 +131,15 @@ public:
|
||||
static hsStream* OpenSecureFileWrite(const wchar_t* 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 const char kKeyFilename[];
|
||||
static const wchar_t kWKeyFilename[];
|
||||
};
|
||||
|
||||
#endif // plSecureStream_h_inc
|
||||
|
@ -128,7 +128,7 @@ hsStream* plStreamSource::GetFile(std::wstring filename)
|
||||
if (plSecureStream::IsSecureFile(sFilename.c_str()))
|
||||
{
|
||||
uint32_t encryptionKey[4];
|
||||
if (!plFileUtils::GetSecureEncryptionKey(sFilename.c_str(), encryptionKey, 4))
|
||||
if (!plSecureStream::GetSecureEncryptionKey(sFilename.c_str(), encryptionKey, 4))
|
||||
{
|
||||
FATAL("Hey camper... You need an NTD key file!");
|
||||
return nil;
|
||||
|
Reference in New Issue
Block a user