From 8b610c7228d836d81b5ba1a1b0643b5d1894c0cf Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Sun, 23 Oct 2011 17:55:11 -0700 Subject: [PATCH] Make plFile stuff compile on Linux. No guarantees made that it works though (particularly the plSecureStream stuff). --- .../Plasma/PubUtilLib/plFile/CMakeLists.txt | 2 +- .../Plasma/PubUtilLib/plFile/plFileUtils.cpp | 14 +++- .../PubUtilLib/plFile/plSecureStream.cpp | 71 +++++++++++++++++-- .../Plasma/PubUtilLib/plFile/plSecureStream.h | 12 +++- .../PubUtilLib/plFile/plStreamSource.cpp | 6 +- 5 files changed, 91 insertions(+), 14 deletions(-) diff --git a/Sources/Plasma/PubUtilLib/plFile/CMakeLists.txt b/Sources/Plasma/PubUtilLib/plFile/CMakeLists.txt index 71f93727..2e5be567 100644 --- a/Sources/Plasma/PubUtilLib/plFile/CMakeLists.txt +++ b/Sources/Plasma/PubUtilLib/plFile/CMakeLists.txt @@ -35,7 +35,7 @@ set(plFile_HEADERS ) add_library(plFile STATIC ${plFile_SOURCES} ${plFile_HEADERS}) -target_link_libraries(plFile ${ZLIB_LIBRARIES}) +target_link_libraries(plFile CoreLib ${ZLIB_LIBRARIES}) source_group("Source Files" FILES ${plFile_SOURCES}) source_group("Header Files" FILES ${plFile_HEADERS}) diff --git a/Sources/Plasma/PubUtilLib/plFile/plFileUtils.cpp b/Sources/Plasma/PubUtilLib/plFile/plFileUtils.cpp index 13ec4ff1..9c884366 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plFileUtils.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plFileUtils.cpp @@ -103,7 +103,8 @@ hsBool plFileUtils::CreateDir( const wchar *path ) #if HS_BUILD_FOR_WIN32 return ( _wmkdir( path ) == 0 ) ? true : ( errno==EEXIST ); #elif HS_BUILD_FOR_UNIX - return ( mkdir( path, 0777 ) == 0 ) ? true : ( errno==EEXIST ); + const char* cpath = hsWStringToString(path); + CreateDir(cpath); #endif } @@ -148,9 +149,14 @@ bool plFileUtils::RemoveFile(const char* filename, bool delReadOnly) bool plFileUtils::RemoveFile(const wchar* filename, bool delReadOnly) { +#ifdef HS_BUILD_FOR_WIN32 if (delReadOnly) _wchmod(filename, S_IWRITE); return (_wunlink(filename) == 0); +#elif HS_BUILD_FOR_UNIX + const char* cfilename = hsWStringToString(filename); + RemoveFile(cfilename, delReadOnly); +#endif } bool plFileUtils::FileCopy(const char* existingFile, const char* newFile) @@ -169,8 +175,10 @@ bool plFileUtils::FileCopy(const wchar* existingFile, const wchar* newFile) return (::CopyFileW(existingFile, newFile, FALSE) != 0); #elif HS_BUILD_FOR_UNIX char data[1500]; - FILE* fp = fopen(existingFile, "rb"); - FILE* fw = fopen(newFile, "w"); + const char* cexisting = hsWStringToString(existingFile); + const char* cnew = hsWStringToString(newFile); + FILE* fp = fopen(cexisting, "rb"); + FILE* fw = fopen(cnew, "w"); int num = 0; bool retVal = true; if (fp && fw){ diff --git a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp index 3be4f54c..551fa0fd 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp @@ -46,6 +46,10 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include +#if !HS_BUILD_FOR_WIN32 +#define INVALID_HANDLE_VALUE 0 +#endif + // our default encryption key const UInt32 plSecureStream::kDefaultKey[4] = { 0x6c0a5452, 0x3827d0f, 0x3a170b92, 0x16db7fc2 }; @@ -143,6 +147,7 @@ hsBool plSecureStream::Open(const wchar* name, const wchar* mode) { if (wcscmp(mode, L"rb") == 0) { +#if HS_BUILD_FOR_WIN32 if (fDeleteOnExit) { fRef = CreateFileW(name, @@ -179,6 +184,22 @@ hsBool plSecureStream::Open(const wchar* name, const wchar* mode) DWORD numBytesRead; ReadFile(fRef, &fActualFileSize, sizeof(UInt32), &numBytesRead, NULL); +#elif HS_BUILD_FOR_UNIX + const char* cname = hsWStringToString(name); + fRef = fopen(cname, "rb"); + + fPosition = 0; + + if (fRef == INVALID_HANDLE_VALUE) + return false; + + if (!ICheckMagicString(fRef)) + { + fclose(fRef); + fRef = INVALID_HANDLE_VALUE; + return false; + } +#endif // The encrypted stream is inefficient if you do reads smaller than // 8 bytes. Since we do a lot of those, any file under a size threshold @@ -221,7 +242,11 @@ hsBool plSecureStream::Close() } if (fRef != INVALID_HANDLE_VALUE) { +#if HS_BUILD_FOR_WIN32 rtn = CloseHandle(fRef); +#elif HS_BUILD_FOR_UNIX + rtn = fclose(fRef); +#endif fRef = INVALID_HANDLE_VALUE; } @@ -248,8 +273,13 @@ UInt32 plSecureStream::IRead(UInt32 bytes, void* buffer) { if (fRef == INVALID_HANDLE_VALUE) return 0; - DWORD numItems; + UInt32 numItems; +#if HS_BUILD_FOR_WIN32 bool success = (ReadFile(fRef, buffer, bytes, &numItems, NULL) != 0); +#elif HS_BUILD_FOR_UNIX + numItems = fread(buffer, bytes, 1, fRef); + bool success = numItems != 0; +#endif fBytesRead += numItems; fPosition += numItems; if ((unsigned)numItems < bytes) @@ -281,7 +311,11 @@ void plSecureStream::IBufferFile() fRAMStream->Rewind(); fBufferedStream = true; +#if HS_BUILD_FOR_WIN32 CloseHandle(fRef); +#elif HS_BUILD_FOR_UNIX + fclose(fRef); +#endif fRef = INVALID_HANDLE_VALUE; fPosition = 0; } @@ -305,7 +339,11 @@ void plSecureStream::Skip(UInt32 delta) { fBytesRead += delta; fPosition += delta; +#if HS_BUILD_FOR_WIN32 SetFilePointer(fRef, delta, 0, FILE_CURRENT); +#elif HS_BUILD_FOR_UNIX + fseek(fRef, delta, SEEK_CUR); +#endif } } @@ -320,7 +358,11 @@ void plSecureStream::Rewind() { fBytesRead = 0; fPosition = 0; +#if HS_BUILD_FOR_WIN32 SetFilePointer(fRef, kFileStartOffset, 0, FILE_BEGIN); +#elif HS_BUILD_FOR_UNIX + fseek(fRef, kFileStartOffset, SEEK_SET); +#endif } } @@ -333,7 +375,11 @@ void plSecureStream::FastFwd() } else if (fRef != INVALID_HANDLE_VALUE) { +#if HS_BUILD_FOR_WIN32 fBytesRead = fPosition = SetFilePointer(fRef, kFileStartOffset + fActualFileSize, 0, FILE_BEGIN); +#elif HS_BUILD_FOR_UNIX + fBytesRead = fPosition = fseek(fRef, 0, SEEK_END); +#endif } } @@ -551,11 +597,14 @@ bool plSecureStream::FileDecrypt(const wchar* fileName, UInt32* key /* = nil */) return true; } -bool plSecureStream::ICheckMagicString(HANDLE fp) +bool plSecureStream::ICheckMagicString(hsFD fp) { char magicString[kMagicStringLen+1]; - DWORD numBytesRead; - ReadFile(fp, &magicString, kMagicStringLen, &numBytesRead, NULL); +#ifdef HS_BUILD_FOR_WIN32 + ReadFile(fp, &magicString, kMagicStringLen, NULL, NULL); +#elif HS_BUILD_FOR_UNIX + fread(&magicString, kMagicStringLen, 1, fp); +#endif magicString[kMagicStringLen] = '\0'; return (hsStrEQ(magicString, kMagicString) != 0); } @@ -570,7 +619,9 @@ bool plSecureStream::IsSecureFile(const char* fileName) bool plSecureStream::IsSecureFile(const wchar* fileName) { - HANDLE fp = INVALID_HANDLE_VALUE; + hsFD fp = INVALID_HANDLE_VALUE; + +#if HS_BUILD_FOR_WIN32 fp = CreateFileW(fileName, GENERIC_READ, // open for reading 0, // no one can open the file until we're done @@ -578,13 +629,21 @@ bool plSecureStream::IsSecureFile(const wchar* fileName) OPEN_EXISTING, // only open existing files (no creation) FILE_ATTRIBUTE_NORMAL, // normal file attributes NULL); // no template +#elif HS_BUILD_FOR_UNIX + const char* cfile = hsWStringToString(fileName); + fp = fopen(cfile, "rb"); +#endif if (fp == INVALID_HANDLE_VALUE) return false; bool isEncrypted = ICheckMagicString(fp); +#if HS_BUILD_FOR_WIN32 CloseHandle(fp); +#elif HS_BUILD_FOR_UNIX + fclose(fp); +#endif return isEncrypted; } @@ -637,4 +696,4 @@ hsStream* plSecureStream::OpenSecureFileWrite(const wchar* fileName, UInt32* key s->Open(fileName, L"wb"); return s; -} \ No newline at end of file +} diff --git a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h index 1f4c5346..1354618a 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h +++ b/Sources/Plasma/PubUtilLib/plFile/plSecureStream.h @@ -43,7 +43,13 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #define plSecureStream_h_inc #include "hsStream.h" -#include + +#if HS_BUILD_FOR_WIN32 +# include +# define hsFD HANDLE +#else +# define hsFD FILE* +#endif // A slightly more secure stream then plEncryptedStream in that it uses windows file functions // to prevent other processes from accessing the file it is working with. It also can be set @@ -53,7 +59,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com class plSecureStream: public hsStream { protected: - HANDLE fRef; + hsFD fRef; UInt32 fKey[4]; UInt32 fActualFileSize; @@ -78,7 +84,7 @@ protected: bool IWriteEncrypted(hsStream* sourceStream, const wchar* outputFile); - static bool ICheckMagicString(HANDLE fp); + static bool ICheckMagicString(hsFD fp); public: plSecureStream(hsBool deleteOnExit = false, UInt32* key = nil); // uses default key if you don't pass one in diff --git a/Sources/Plasma/PubUtilLib/plFile/plStreamSource.cpp b/Sources/Plasma/PubUtilLib/plFile/plStreamSource.cpp index d33ec41e..4f58db81 100644 --- a/Sources/Plasma/PubUtilLib/plFile/plStreamSource.cpp +++ b/Sources/Plasma/PubUtilLib/plFile/plStreamSource.cpp @@ -46,6 +46,10 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plEncryptedStream.h" #include "plFileUtils.h" +#if HS_BUILD_FOR_UNIX +# include +#endif + void ToLower(std::wstring& str) { for (unsigned i = 0; i < str.length(); i++) @@ -209,4 +213,4 @@ plStreamSource* plStreamSource::GetInstance() { static plStreamSource source; return &source; -} \ No newline at end of file +}