aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'dep/CascLib/src/common')
-rw-r--r--dep/CascLib/src/common/Common.cpp129
-rw-r--r--dep/CascLib/src/common/Common.h82
-rw-r--r--dep/CascLib/src/common/Csv.cpp2
-rw-r--r--dep/CascLib/src/common/FileStream.cpp94
-rw-r--r--dep/CascLib/src/common/ListFile.cpp6
-rw-r--r--dep/CascLib/src/common/Map.h23
6 files changed, 179 insertions, 157 deletions
diff --git a/dep/CascLib/src/common/Common.cpp b/dep/CascLib/src/common/Common.cpp
index c16323e0f5e..7bec2b14a6a 100644
--- a/dep/CascLib/src/common/Common.cpp
+++ b/dep/CascLib/src/common/Common.cpp
@@ -59,24 +59,42 @@ unsigned char AsciiToUpperTable_BkSlash[256] =
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
};
+// Converts ASCII characters to hexa digit
+unsigned char AsciiToHexTable[128] =
+{
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+};
+
unsigned char IntToHexChar[] = "0123456789abcdef";
//-----------------------------------------------------------------------------
-// GetLastError/SetLastError support for non-Windows platform
+// GetCascError/SetCascError support for non-Windows platform
-#ifndef PLATFORM_WINDOWS
static DWORD dwLastError = ERROR_SUCCESS;
-DWORD GetLastError()
+DWORD GetCascError()
{
+#ifdef PLATFORM_WINDOWS
+ return GetLastError();
+#else
return dwLastError;
+#endif
}
-void SetLastError(DWORD dwErrCode)
+void SetCascError(DWORD dwErrCode)
{
+#ifdef PLATFORM_WINDOWS
+ SetLastError(dwErrCode);
+#endif
dwLastError = dwErrCode;
}
-#endif
//-----------------------------------------------------------------------------
// Linear data stream manipulation
@@ -520,103 +538,6 @@ ULONGLONG CalcFileNameHash(const char * szFileName)
return CalcNormNameHash(szNormName, nLength);
}
-DWORD ConvertDigitToInt32(LPCTSTR szString, PDWORD PtrValue)
-{
- BYTE Digit;
-
- Digit = (BYTE)(AsciiToUpperTable_BkSlash[szString[0]] - _T('0'));
- if(Digit > 9)
- Digit -= 'A' - '9' - 1;
-
- PtrValue[0] = Digit;
- return (Digit > 0x0F) ? ERROR_BAD_FORMAT : ERROR_SUCCESS;
-}
-
-DWORD ConvertStringToInt08(LPCSTR szString, PDWORD PtrValue)
-{
- BYTE DigitOne = AsciiToUpperTable_BkSlash[szString[0]] - '0';
- BYTE DigitTwo = AsciiToUpperTable_BkSlash[szString[1]] - '0';
-
- // Fix the digits
- if(DigitOne > 9)
- DigitOne -= 'A' - '9' - 1;
- if(DigitTwo > 9)
- DigitTwo -= 'A' - '9' - 1;
-
- // Combine them into a value
- PtrValue[0] = (DigitOne << 0x04) | DigitTwo;
- return (DigitOne <= 0x0F && DigitTwo <= 0x0F) ? ERROR_SUCCESS : ERROR_BAD_FORMAT;
-}
-
-DWORD ConvertStringToInt32(LPCTSTR szString, size_t nMaxDigits, PDWORD PtrValue)
-{
- // The number of digits must be even
- assert((nMaxDigits & 0x01) == 0);
- assert(nMaxDigits <= 8);
-
- // Prepare the variables
- PtrValue[0] = 0;
- nMaxDigits >>= 1;
-
- // Convert the string up to the number of digits
- for(size_t i = 0; i < nMaxDigits; i++)
- {
- BYTE DigitOne;
- BYTE DigitTwo;
-
- DigitOne = (BYTE)(AsciiToUpperTable_BkSlash[szString[0]] - _T('0'));
- if(DigitOne > 9)
- DigitOne -= 'A' - '9' - 1;
-
- DigitTwo = (BYTE)(AsciiToUpperTable_BkSlash[szString[1]] - _T('0'));
- if(DigitTwo > 9)
- DigitTwo -= 'A' - '9' - 1;
-
- if(DigitOne > 0x0F || DigitTwo > 0x0F)
- return ERROR_BAD_FORMAT;
-
- PtrValue[0] = (PtrValue[0] << 0x08) | (DigitOne << 0x04) | DigitTwo;
- szString += 2;
- }
-
- return ERROR_SUCCESS;
-}
-
-// Converts string blob to binary blob.
-DWORD ConvertStringToBinary(
- LPCSTR szString,
- size_t nMaxDigits,
- LPBYTE pbBinary)
-{
- const char * szStringEnd = szString + nMaxDigits;
- DWORD dwCounter = 0;
- BYTE DigitValue;
- BYTE ByteValue = 0;
-
- // Convert the string
- while(szString < szStringEnd)
- {
- // Retrieve the digit converted to hexa
- DigitValue = (BYTE)(AsciiToUpperTable_BkSlash[szString[0]] - '0');
- if(DigitValue > 9)
- DigitValue -= 'A' - '9' - 1;
- if(DigitValue > 0x0F)
- return ERROR_BAD_FORMAT;
-
- // Insert the digit to the binary buffer
- ByteValue = (ByteValue << 0x04) | DigitValue;
- dwCounter++;
-
- // If we reached the second digit, it means that we need
- // to flush the byte value and move on
- if((dwCounter & 0x01) == 0)
- *pbBinary++ = ByteValue;
- szString++;
- }
-
- return ERROR_SUCCESS;
-}
-
//-----------------------------------------------------------------------------
// File name utilities
@@ -648,7 +569,7 @@ bool IsFileDataIdName(const char * szFileName, DWORD & FileDataId)
if(!strncmp(szFileName, "FILE", 4) && strlen(szFileName) >= 0x0C)
{
// Convert the hexadecimal number to integer
- if(ConvertStringToBinary(szFileName+4, 8, BinaryValue) == ERROR_SUCCESS)
+ if(BinaryFromString(szFileName+4, 8, BinaryValue) == ERROR_SUCCESS)
{
// Must be followed by an extension or end-of-string
if(szFileName[0x0C] == 0 || szFileName[0x0C] == '.')
@@ -668,7 +589,7 @@ bool IsFileCKeyEKeyName(const char * szFileName, LPBYTE PtrKeyBuffer)
if(nLength == MD5_STRING_SIZE)
{
- if(ConvertStringToBinary(szFileName, MD5_STRING_SIZE, PtrKeyBuffer) == ERROR_SUCCESS)
+ if(BinaryFromString(szFileName, MD5_STRING_SIZE, PtrKeyBuffer) == ERROR_SUCCESS)
{
return true;
}
diff --git a/dep/CascLib/src/common/Common.h b/dep/CascLib/src/common/Common.h
index 0037293f53d..46b23adb6ae 100644
--- a/dep/CascLib/src/common/Common.h
+++ b/dep/CascLib/src/common/Common.h
@@ -124,6 +124,7 @@ typedef CASC_CKEY_ENTRY *PCASC_CKEY_ENTRY;
extern unsigned char AsciiToLowerTable_Slash[256];
extern unsigned char AsciiToUpperTable_BkSlash[256];
+extern unsigned char AsciiToHexTable[0x80];
extern unsigned char IntToHexChar[];
//-----------------------------------------------------------------------------
@@ -339,15 +340,82 @@ size_t NormalizeFileName_LowerSlash(char * szNormName, const char * szFileName,
ULONGLONG CalcNormNameHash(const char * szNormName, size_t nLength);
ULONGLONG CalcFileNameHash(const char * szFileName);
-DWORD ConvertDigitToInt32(LPCTSTR szString, PDWORD PtrValue);
-DWORD ConvertStringToInt08(LPCSTR szString, PDWORD PtrValue);
-DWORD ConvertStringToInt32(LPCTSTR szString, size_t nMaxDigits, PDWORD PtrValue);
-DWORD ConvertStringToBinary(LPCSTR szString, size_t nMaxDigits, LPBYTE pbBinary);
-
//-----------------------------------------------------------------------------
-// Conversion from binary array to string. The caller must ensure that
-// the buffer has at least ((cbBinary * 2) + 1) characters
+// String conversion functions
+
+template <typename xchar, typename INTXX>
+DWORD ConvertStringToInt(const xchar * szString, size_t nMaxDigits, INTXX & RefValue, const xchar ** PtrStringEnd = NULL)
+{
+ INTXX MaxValueMask = (INTXX)0x0F << ((sizeof(INTXX) * 8) - 4);
+ INTXX Accumulator = 0;
+ BYTE DigitOne;
+
+ // Set default value
+ if(nMaxDigits == 0)
+ nMaxDigits = sizeof(INTXX) * 2;
+
+ // Convert the string up to the number of digits
+ for(size_t i = 0; i < nMaxDigits; i++, szString++)
+ {
+ // Check for the end of the string
+ if(szString[0] > sizeof(AsciiToHexTable))
+ return ERROR_BAD_FORMAT;
+ if(szString[0] <= 0x20)
+ break;
+
+ // Extract the next digit
+ DigitOne = AsciiToHexTable[szString[0]];
+ if(DigitOne == 0xFF)
+ return ERROR_BAD_FORMAT;
+
+ // Check overflow. If OK, shift the value by 4 to the left
+ if(Accumulator & MaxValueMask)
+ return ERROR_ARITHMETIC_OVERFLOW;
+ Accumulator = (Accumulator << 4) | DigitOne;
+ }
+
+ // Give the results
+ if(PtrStringEnd != NULL)
+ PtrStringEnd[0] = szString;
+ RefValue = Accumulator;
+ return ERROR_SUCCESS;
+}
+
+// Converts string blob to binary blob
+template <typename xchar>
+DWORD BinaryFromString(const xchar * szString, size_t nMaxDigits, LPBYTE pbBinary)
+{
+ const xchar * szStringEnd = szString + nMaxDigits;
+ DWORD dwCounter = 0;
+ BYTE DigitValue;
+ BYTE ByteValue = 0;
+
+ // Convert the string
+ while(szString < szStringEnd)
+ {
+ // Retrieve the digit converted to hexa
+ DigitValue = (BYTE)(AsciiToUpperTable_BkSlash[szString[0]] - '0');
+ if(DigitValue > 9)
+ DigitValue -= 'A' - '9' - 1;
+ if(DigitValue > 0x0F)
+ return ERROR_BAD_FORMAT;
+
+ // Insert the digit to the binary buffer
+ ByteValue = (ByteValue << 0x04) | DigitValue;
+ dwCounter++;
+
+ // If we reached the second digit, it means that we need
+ // to flush the byte value and move on
+ if((dwCounter & 0x01) == 0)
+ *pbBinary++ = ByteValue;
+ szString++;
+ }
+
+ return ERROR_SUCCESS;
+}
+// Converts binary array to string.
+// The caller must ensure that the buffer has at least ((cbBinary * 2) + 1) characters
template <typename xchar>
xchar * StringFromBinary(LPBYTE pbBinary, size_t cbBinary, xchar * szBuffer)
{
diff --git a/dep/CascLib/src/common/Csv.cpp b/dep/CascLib/src/common/Csv.cpp
index 818973e9603..40a50ec723b 100644
--- a/dep/CascLib/src/common/Csv.cpp
+++ b/dep/CascLib/src/common/Csv.cpp
@@ -203,7 +203,7 @@ DWORD CASC_CSV::Load(LPCTSTR szFileName)
}
else
{
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
}
return dwErrCode;
diff --git a/dep/CascLib/src/common/FileStream.cpp b/dep/CascLib/src/common/FileStream.cpp
index 350b0540cf4..cb0090f8dfb 100644
--- a/dep/CascLib/src/common/FileStream.cpp
+++ b/dep/CascLib/src/common/FileStream.cpp
@@ -75,7 +75,7 @@ static bool BaseFile_Create(TFileStream * pStream)
if(handle == -1)
{
pStream->Base.File.hFile = INVALID_HANDLE_VALUE;
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
@@ -128,14 +128,14 @@ static bool BaseFile_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
handle = open(szFileName, oflag | O_LARGEFILE);
if(handle == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
// Get the file size
if(fstat64(handle, &fileinfo) == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
close(handle);
return false;
}
@@ -185,8 +185,11 @@ static bool BaseFile_Read(
Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32);
Overlapped.Offset = (DWORD)ByteOffset;
Overlapped.hEvent = NULL;
- if (!ReadFile(pStream->Base.File.hFile, pvBuffer, dwBytesToRead, &dwBytesRead, &Overlapped))
+ if(!ReadFile(pStream->Base.File.hFile, pvBuffer, dwBytesToRead, &dwBytesRead, &Overlapped))
+ {
+ CascUnlock(pStream->Lock);
return false;
+ }
}
}
#endif
@@ -201,7 +204,8 @@ static bool BaseFile_Read(
{
if (lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET) == (off64_t)-1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
pStream->Base.File.FilePos = ByteOffset;
@@ -213,7 +217,8 @@ static bool BaseFile_Read(
bytes_read = read((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToRead);
if (bytes_read == -1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
@@ -239,7 +244,7 @@ static bool BaseFile_Read(
}
else
{
- SetLastError(ERROR_HANDLE_EOF);
+ SetCascError(ERROR_HANDLE_EOF);
}
}
@@ -280,8 +285,11 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32);
Overlapped.Offset = (DWORD)ByteOffset;
Overlapped.hEvent = NULL;
- if (!WriteFile(pStream->Base.File.hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, &Overlapped))
+ if(!WriteFile(pStream->Base.File.hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, &Overlapped))
+ {
+ CascUnlock(pStream->Lock);
return false;
+ }
}
}
#endif
@@ -296,7 +304,8 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
{
if (lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET) == (off64_t)-1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
pStream->Base.File.FilePos = ByteOffset;
@@ -306,7 +315,8 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
bytes_written = write((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToWrite);
if (bytes_written == -1)
{
- SetLastError(errno);
+ CascUnlock(pStream->Lock);
+ SetCascError(errno);
return false;
}
@@ -324,7 +334,7 @@ static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const
CascUnlock(pStream->Lock);
if(dwBytesWritten != dwBytesToWrite)
- SetLastError(ERROR_DISK_FULL);
+ SetCascError(ERROR_DISK_FULL);
return (dwBytesWritten == dwBytesToWrite);
}
@@ -343,7 +353,7 @@ static bool BaseFile_Resize(TFileStream * pStream, ULONGLONG NewFileSize)
// Set the position at the new file size
dwNewPos = SetFilePointer(pStream->Base.File.hFile, (LONG)NewFileSize, &FileSizeHi, FILE_BEGIN);
- if(dwNewPos == INVALID_SET_FILE_POINTER && GetLastError() != ERROR_SUCCESS)
+ if(dwNewPos == INVALID_SET_FILE_POINTER && GetCascError() != ERROR_SUCCESS)
return false;
// Set the current file pointer as the end of the file
@@ -363,7 +373,7 @@ static bool BaseFile_Resize(TFileStream * pStream, ULONGLONG NewFileSize)
{
if(ftruncate64((intptr_t)pStream->Base.File.hFile, (off64_t)NewFileSize) == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
@@ -403,7 +413,7 @@ static bool BaseFile_Replace(TFileStream * pStream, TFileStream * pNewStream)
// "rename" on Linux also works if the target file exists
if(rename(pNewStream->szFileName, pStream->szFileName) == -1)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
@@ -533,7 +543,7 @@ static bool BaseMap_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStre
// Did the mapping fail?
if(bResult == false)
{
- SetLastError(errno);
+ SetCascError(errno);
return false;
}
#endif
@@ -638,7 +648,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
// Don't download if we are not connected to the internet
if(!InternetGetConnectedState(&dwTemp, 0))
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
// Initiate the connection to the internet
if(dwErrCode == ERROR_SUCCESS)
@@ -649,7 +659,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
NULL,
0);
if(pStream->Base.Http.hInternet == NULL)
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
}
// Connect to the server
@@ -669,7 +679,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
dwFlags,
0);
if(pStream->Base.Http.hConnect == NULL)
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
}
// Now try to query the file size
@@ -727,7 +737,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
if(bFileAvailable == false)
{
pStream->BaseClose(pStream);
- SetLastError(dwErrCode);
+ SetCascError(dwErrCode);
return false;
}
@@ -736,7 +746,7 @@ static bool BaseHttp_Open(TFileStream * pStream, LPCTSTR szFileName, DWORD dwStr
#else
// Not supported
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
pStream = pStream;
return false;
@@ -804,7 +814,7 @@ static bool BaseHttp_Read(
// If the number of bytes read doesn't match the required amount, return false
if(dwTotalBytesRead != dwBytesToRead)
- SetLastError(ERROR_HANDLE_EOF);
+ SetCascError(ERROR_HANDLE_EOF);
return (dwTotalBytesRead == dwBytesToRead);
#else
@@ -814,7 +824,7 @@ static bool BaseHttp_Read(
pByteOffset = pByteOffset;
pvBuffer = pvBuffer;
dwBytesToRead = dwBytesToRead;
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return false;
#endif
@@ -888,7 +898,7 @@ static bool BlockStream_Read(
EndOffset = ByteOffset + dwBytesToRead;
if(EndOffset > pStream->StreamSize)
{
- SetLastError(ERROR_HANDLE_EOF);
+ SetCascError(ERROR_HANDLE_EOF);
return false;
}
@@ -905,7 +915,7 @@ static bool BlockStream_Read(
TransferBuffer = BlockBuffer = CASC_ALLOC<BYTE>(BlockCount * BlockSize);
if(TransferBuffer == NULL)
{
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ SetCascError(ERROR_NOT_ENOUGH_MEMORY);
return false;
}
@@ -983,7 +993,7 @@ static bool BlockStream_Read(
else
{
// If the block read failed, set the last error
- SetLastError(ERROR_FILE_INCOMPLETE);
+ SetCascError(ERROR_FILE_INCOMPLETE);
}
// Call the callback to indicate we are done
@@ -1058,7 +1068,7 @@ static TFileStream * AllocateFileStream(
// Don't allow another master file in the string
if(_tcschr(szNextFile + 1, _T('*')) != NULL)
{
- SetLastError(ERROR_INVALID_PARAMETER);
+ SetCascError(ERROR_INVALID_PARAMETER);
return NULL;
}
@@ -1392,7 +1402,7 @@ static TFileStream * FlatStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
if(pStream == NULL)
{
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ SetCascError(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
@@ -1402,7 +1412,7 @@ static TFileStream * FlatStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(!FlatStream_CreateMirror(pStream))
{
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
+ SetCascError(ERROR_FILE_NOT_FOUND);
return NULL;
}
}
@@ -1819,7 +1829,7 @@ static TFileStream * PartStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(!PartStream_CreateMirror(pStream))
{
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
+ SetCascError(ERROR_FILE_NOT_FOUND);
return NULL;
}
}
@@ -1836,7 +1846,7 @@ static TFileStream * PartStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(!PartStream_LoadBitmap(pStream))
{
FileStream_Close(pStream);
- SetLastError(ERROR_BAD_FORMAT);
+ SetCascError(ERROR_BAD_FORMAT);
return NULL;
}
}
@@ -2149,7 +2159,7 @@ static TFileStream * EncrStream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
// Cleanup the stream and return
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_ENCRYPTED);
+ SetCascError(ERROR_FILE_ENCRYPTED);
return NULL;
}
@@ -2293,7 +2303,7 @@ static TFileStream * Block4Stream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
NewBaseArray = CASC_ALLOC<TBaseProviderData>(dwBaseFiles + 1);
if(NewBaseArray == NULL)
{
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ SetCascError(ERROR_NOT_ENOUGH_MEMORY);
return NULL;
}
@@ -2348,7 +2358,7 @@ static TFileStream * Block4Stream_Open(LPCTSTR szFileName, DWORD dwStreamFlags)
if(dwBaseFiles == 0)
{
FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
+ SetCascError(ERROR_FILE_NOT_FOUND);
pStream = NULL;
}
@@ -2384,7 +2394,7 @@ TFileStream * FileStream_CreateFile(
// We only support creation of flat, local file
if((dwStreamFlags & (STREAM_PROVIDERS_MASK)) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE))
{
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return NULL;
}
@@ -2457,7 +2467,7 @@ TFileStream * FileStream_OpenFile(
return Block4Stream_Open(szFileName, dwStreamFlags);
default:
- SetLastError(ERROR_INVALID_PARAMETER);
+ SetCascError(ERROR_INVALID_PARAMETER);
return NULL;
}
}
@@ -2569,7 +2579,7 @@ bool FileStream_SetCallback(TFileStream * pStream, STREAM_DOWNLOAD_CALLBACK pfnC
if(pStream->BlockRead == NULL)
{
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return false;
}
@@ -2594,8 +2604,8 @@ bool FileStream_SetCallback(TFileStream * pStream, STREAM_DOWNLOAD_CALLBACK pfnC
*
* \returns
* - If the function reads the required amount of bytes, it returns true.
- * - If the function reads less than required bytes, it returns false and GetLastError() returns ERROR_HANDLE_EOF
- * - If the function fails, it reads false and GetLastError() returns an error code different from ERROR_HANDLE_EOF
+ * - If the function reads less than required bytes, it returns false and GetCascError() returns ERROR_HANDLE_EOF
+ * - If the function fails, it reads false and GetCascError() returns an error code different from ERROR_HANDLE_EOF
*/
bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead)
{
@@ -2619,7 +2629,7 @@ bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void
{
if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
{
- SetLastError(ERROR_ACCESS_DENIED);
+ SetCascError(ERROR_ACCESS_DENIED);
return false;
}
@@ -2649,7 +2659,7 @@ bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize)
{
if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
{
- SetLastError(ERROR_ACCESS_DENIED);
+ SetCascError(ERROR_ACCESS_DENIED);
return false;
}
@@ -2709,14 +2719,14 @@ bool FileStream_Replace(TFileStream * pStream, TFileStream * pNewStream)
// Only supported on flat files
if((pStream->dwFlags & STREAM_PROVIDERS_MASK) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE))
{
- SetLastError(ERROR_NOT_SUPPORTED);
+ SetCascError(ERROR_NOT_SUPPORTED);
return false;
}
// Not supported on read-only streams
if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
{
- SetLastError(ERROR_ACCESS_DENIED);
+ SetCascError(ERROR_ACCESS_DENIED);
return false;
}
diff --git a/dep/CascLib/src/common/ListFile.cpp b/dep/CascLib/src/common/ListFile.cpp
index a284ef2db10..a91922cd9b1 100644
--- a/dep/CascLib/src/common/ListFile.cpp
+++ b/dep/CascLib/src/common/ListFile.cpp
@@ -245,7 +245,7 @@ size_t ListFile_GetNextLine(void * pvListFile, char * szBuffer, size_t nMaxChars
// If we didn't read anything, set the error code
if(nLength == 0)
- SetLastError(dwErrCode);
+ SetCascError(dwErrCode);
return nLength;
}
@@ -282,7 +282,7 @@ size_t ListFile_GetNext(void * pvListFile, char * szBuffer, size_t nMaxChars, PD
nLength = ListFile_GetNextLine(pvListFile, szBuffer, nMaxChars);
if(nLength == 0)
{
- dwErrCode = GetLastError();
+ dwErrCode = GetCascError();
break;
}
@@ -292,7 +292,7 @@ size_t ListFile_GetNext(void * pvListFile, char * szBuffer, size_t nMaxChars, PD
}
if(dwErrCode != ERROR_SUCCESS)
- SetLastError(dwErrCode);
+ SetCascError(dwErrCode);
return nLength;
}
diff --git a/dep/CascLib/src/common/Map.h b/dep/CascLib/src/common/Map.h
index 4b277cb73a8..54dda5baed8 100644
--- a/dep/CascLib/src/common/Map.h
+++ b/dep/CascLib/src/common/Map.h
@@ -354,4 +354,27 @@ class CASC_MAP
// Will improve performance, as we will not hash a hash :-)
};
+//-----------------------------------------------------------------------------
+// Key map interface
+
+// Maximum length of encryption key
+#define CASC_KEY_LENGTH 0x10
+#define CASC_KEY_TABLE_SIZE 0x100
+#define CASC_KEY_TABLE_MASK (CASC_KEY_TABLE_SIZE - 1)
+
+class CASC_KEY_MAP
+{
+ public:
+
+ CASC_KEY_MAP();
+ ~CASC_KEY_MAP();
+
+ LPBYTE FindKey(ULONGLONG KeyName);
+ bool AddKey(ULONGLONG KeyName, LPBYTE Key);
+
+ protected:
+
+ void * HashTable[CASC_KEY_TABLE_SIZE];
+};
+
#endif // __CASC_MAP_H__