Browse Source

Make plFile stuff compile on Linux.

No guarantees made that it works though (particularly the plSecureStream
stuff).
Darryl Pogue 13 years ago
parent
commit
8b610c7228
  1. 2
      Sources/Plasma/PubUtilLib/plFile/CMakeLists.txt
  2. 14
      Sources/Plasma/PubUtilLib/plFile/plFileUtils.cpp
  3. 69
      Sources/Plasma/PubUtilLib/plFile/plSecureStream.cpp
  4. 10
      Sources/Plasma/PubUtilLib/plFile/plSecureStream.h
  5. 4
      Sources/Plasma/PubUtilLib/plFile/plStreamSource.cpp

2
Sources/Plasma/PubUtilLib/plFile/CMakeLists.txt

@ -35,7 +35,7 @@ set(plFile_HEADERS
) )
add_library(plFile STATIC ${plFile_SOURCES} ${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("Source Files" FILES ${plFile_SOURCES})
source_group("Header Files" FILES ${plFile_HEADERS}) source_group("Header Files" FILES ${plFile_HEADERS})

14
Sources/Plasma/PubUtilLib/plFile/plFileUtils.cpp

@ -103,7 +103,8 @@ hsBool plFileUtils::CreateDir( const wchar *path )
#if HS_BUILD_FOR_WIN32 #if HS_BUILD_FOR_WIN32
return ( _wmkdir( path ) == 0 ) ? true : ( errno==EEXIST ); return ( _wmkdir( path ) == 0 ) ? true : ( errno==EEXIST );
#elif HS_BUILD_FOR_UNIX #elif HS_BUILD_FOR_UNIX
return ( mkdir( path, 0777 ) == 0 ) ? true : ( errno==EEXIST ); const char* cpath = hsWStringToString(path);
CreateDir(cpath);
#endif #endif
} }
@ -148,9 +149,14 @@ bool plFileUtils::RemoveFile(const char* filename, bool delReadOnly)
bool plFileUtils::RemoveFile(const wchar* filename, bool delReadOnly) bool plFileUtils::RemoveFile(const wchar* filename, bool delReadOnly)
{ {
#ifdef HS_BUILD_FOR_WIN32
if (delReadOnly) if (delReadOnly)
_wchmod(filename, S_IWRITE); _wchmod(filename, S_IWRITE);
return (_wunlink(filename) == 0); 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) 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); return (::CopyFileW(existingFile, newFile, FALSE) != 0);
#elif HS_BUILD_FOR_UNIX #elif HS_BUILD_FOR_UNIX
char data[1500]; char data[1500];
FILE* fp = fopen(existingFile, "rb"); const char* cexisting = hsWStringToString(existingFile);
FILE* fw = fopen(newFile, "w"); const char* cnew = hsWStringToString(newFile);
FILE* fp = fopen(cexisting, "rb");
FILE* fw = fopen(cnew, "w");
int num = 0; int num = 0;
bool retVal = true; bool retVal = true;
if (fp && fw){ if (fp && fw){

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

@ -46,6 +46,10 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include <time.h> #include <time.h>
#if !HS_BUILD_FOR_WIN32
#define INVALID_HANDLE_VALUE 0
#endif
// our default encryption key // our default encryption key
const UInt32 plSecureStream::kDefaultKey[4] = { 0x6c0a5452, 0x3827d0f, 0x3a170b92, 0x16db7fc2 }; 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 (wcscmp(mode, L"rb") == 0)
{ {
#if HS_BUILD_FOR_WIN32
if (fDeleteOnExit) if (fDeleteOnExit)
{ {
fRef = CreateFileW(name, fRef = CreateFileW(name,
@ -179,6 +184,22 @@ hsBool plSecureStream::Open(const wchar* name, const wchar* mode)
DWORD numBytesRead; DWORD numBytesRead;
ReadFile(fRef, &fActualFileSize, sizeof(UInt32), &numBytesRead, NULL); 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 // 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 // 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 (fRef != INVALID_HANDLE_VALUE)
{ {
#if HS_BUILD_FOR_WIN32
rtn = CloseHandle(fRef); rtn = CloseHandle(fRef);
#elif HS_BUILD_FOR_UNIX
rtn = fclose(fRef);
#endif
fRef = INVALID_HANDLE_VALUE; fRef = INVALID_HANDLE_VALUE;
} }
@ -248,8 +273,13 @@ UInt32 plSecureStream::IRead(UInt32 bytes, void* buffer)
{ {
if (fRef == INVALID_HANDLE_VALUE) if (fRef == INVALID_HANDLE_VALUE)
return 0; return 0;
DWORD numItems; UInt32 numItems;
#if HS_BUILD_FOR_WIN32
bool success = (ReadFile(fRef, buffer, bytes, &numItems, NULL) != 0); 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; fBytesRead += numItems;
fPosition += numItems; fPosition += numItems;
if ((unsigned)numItems < bytes) if ((unsigned)numItems < bytes)
@ -281,7 +311,11 @@ void plSecureStream::IBufferFile()
fRAMStream->Rewind(); fRAMStream->Rewind();
fBufferedStream = true; fBufferedStream = true;
#if HS_BUILD_FOR_WIN32
CloseHandle(fRef); CloseHandle(fRef);
#elif HS_BUILD_FOR_UNIX
fclose(fRef);
#endif
fRef = INVALID_HANDLE_VALUE; fRef = INVALID_HANDLE_VALUE;
fPosition = 0; fPosition = 0;
} }
@ -305,7 +339,11 @@ void plSecureStream::Skip(UInt32 delta)
{ {
fBytesRead += delta; fBytesRead += delta;
fPosition += delta; fPosition += delta;
#if HS_BUILD_FOR_WIN32
SetFilePointer(fRef, delta, 0, FILE_CURRENT); 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; fBytesRead = 0;
fPosition = 0; fPosition = 0;
#if HS_BUILD_FOR_WIN32
SetFilePointer(fRef, kFileStartOffset, 0, FILE_BEGIN); 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) else if (fRef != INVALID_HANDLE_VALUE)
{ {
#if HS_BUILD_FOR_WIN32
fBytesRead = fPosition = SetFilePointer(fRef, kFileStartOffset + fActualFileSize, 0, FILE_BEGIN); 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; return true;
} }
bool plSecureStream::ICheckMagicString(HANDLE fp) bool plSecureStream::ICheckMagicString(hsFD fp)
{ {
char magicString[kMagicStringLen+1]; char magicString[kMagicStringLen+1];
DWORD numBytesRead; #ifdef HS_BUILD_FOR_WIN32
ReadFile(fp, &magicString, kMagicStringLen, &numBytesRead, NULL); ReadFile(fp, &magicString, kMagicStringLen, NULL, NULL);
#elif HS_BUILD_FOR_UNIX
fread(&magicString, kMagicStringLen, 1, fp);
#endif
magicString[kMagicStringLen] = '\0'; magicString[kMagicStringLen] = '\0';
return (hsStrEQ(magicString, kMagicString) != 0); return (hsStrEQ(magicString, kMagicString) != 0);
} }
@ -570,7 +619,9 @@ bool plSecureStream::IsSecureFile(const char* fileName)
bool plSecureStream::IsSecureFile(const wchar* 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, fp = CreateFileW(fileName,
GENERIC_READ, // open for reading GENERIC_READ, // open for reading
0, // no one can open the file until we're done 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) OPEN_EXISTING, // only open existing files (no creation)
FILE_ATTRIBUTE_NORMAL, // normal file attributes FILE_ATTRIBUTE_NORMAL, // normal file attributes
NULL); // no template NULL); // no template
#elif HS_BUILD_FOR_UNIX
const char* cfile = hsWStringToString(fileName);
fp = fopen(cfile, "rb");
#endif
if (fp == INVALID_HANDLE_VALUE) if (fp == INVALID_HANDLE_VALUE)
return false; return false;
bool isEncrypted = ICheckMagicString(fp); bool isEncrypted = ICheckMagicString(fp);
#if HS_BUILD_FOR_WIN32
CloseHandle(fp); CloseHandle(fp);
#elif HS_BUILD_FOR_UNIX
fclose(fp);
#endif
return isEncrypted; return isEncrypted;
} }

10
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 #define plSecureStream_h_inc
#include "hsStream.h" #include "hsStream.h"
#if HS_BUILD_FOR_WIN32
# include <windows.h> # include <windows.h>
# define hsFD HANDLE
#else
# define hsFD FILE*
#endif
// A slightly more secure stream then plEncryptedStream in that it uses windows file functions // 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 // 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 class plSecureStream: public hsStream
{ {
protected: protected:
HANDLE fRef; hsFD fRef;
UInt32 fKey[4]; UInt32 fKey[4];
UInt32 fActualFileSize; UInt32 fActualFileSize;
@ -78,7 +84,7 @@ protected:
bool IWriteEncrypted(hsStream* sourceStream, const wchar* outputFile); bool IWriteEncrypted(hsStream* sourceStream, const wchar* outputFile);
static bool ICheckMagicString(HANDLE fp); static bool ICheckMagicString(hsFD fp);
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

4
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 "plEncryptedStream.h"
#include "plFileUtils.h" #include "plFileUtils.h"
#if HS_BUILD_FOR_UNIX
# include <wctype.h>
#endif
void ToLower(std::wstring& str) void ToLower(std::wstring& str)
{ {
for (unsigned i = 0; i < str.length(); i++) for (unsigned i = 0; i < str.length(); i++)

Loading…
Cancel
Save