aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/common/Common.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dep/CascLib/src/common/Common.cpp')
-rw-r--r--dep/CascLib/src/common/Common.cpp226
1 files changed, 139 insertions, 87 deletions
diff --git a/dep/CascLib/src/common/Common.cpp b/dep/CascLib/src/common/Common.cpp
index 2dbdd470478..9c0f56dc9ba 100644
--- a/dep/CascLib/src/common/Common.cpp
+++ b/dep/CascLib/src/common/Common.cpp
@@ -99,7 +99,7 @@ void CopyString(char * szTarget, const wchar_t * szSource, size_t cchLength)
szTarget[cchLength] = 0;
}
-char * NewStr(const char * szString, size_t nCharsToReserve)
+char * CascNewStr(const char * szString, size_t nCharsToReserve)
{
char * szNewString = NULL;
size_t nLength;
@@ -118,7 +118,7 @@ char * NewStr(const char * szString, size_t nCharsToReserve)
return szNewString;
}
-wchar_t * NewStr(const wchar_t * szString, size_t nCharsToReserve)
+wchar_t * CascNewStr(const wchar_t * szString, size_t nCharsToReserve)
{
wchar_t * szNewString = NULL;
size_t nLength;
@@ -137,22 +137,20 @@ wchar_t * NewStr(const wchar_t * szString, size_t nCharsToReserve)
return szNewString;
}
-TCHAR * NewStrFromAnsi(LPBYTE pbStringBegin, LPBYTE pbStringEnd)
+TCHAR * CascNewStrFromAnsi(const char * szBegin, const char * szEnd)
{
TCHAR * szNewString = NULL;
- TCHAR * szStringPtr = NULL;
- size_t nLength = (size_t)(pbStringEnd - pbStringBegin);
- if(pbStringEnd > pbStringBegin)
+ // Only if the entry is valid
+ if(szBegin != NULL && szEnd > szBegin)
{
- szNewString = szStringPtr = CASC_ALLOC(TCHAR, nLength + 1);
+ // Allocate and copy the string
+ szNewString = CASC_ALLOC(TCHAR, (szEnd - szBegin + 1));
if(szNewString != NULL)
- {
- CopyString(szStringPtr, (const char *)pbStringBegin, nLength);
- szStringPtr[nLength] = 0;
- }
+ CopyString(szNewString, szBegin, (szEnd - szBegin));
}
+ // Return the string
return szNewString;
}
@@ -218,30 +216,60 @@ TCHAR * CombinePath(const TCHAR * szDirectory, const TCHAR * szSubDir)
return szFullPath;
}
-void NormalizeFileName_UpperBkSlash(char * szTrgFileName, const char * szSrcFileName, size_t cchMaxChars)
+TCHAR * CombinePathAndString(const TCHAR * szPath, const char * szString, size_t nLength)
{
- char * szTrgFileEnd = szTrgFileName + cchMaxChars;
- size_t i;
+ TCHAR * szFullPath = NULL;
+ TCHAR * szSubDir;
- // Normalize the file name: ToLower + BackSlashToSlash
- for(i = 0; szSrcFileName[i] != 0 && szTrgFileName < szTrgFileEnd; i++)
- szTrgFileName[i] = AsciiToUpperTable_BkSlash[szSrcFileName[i]];
+ // Create the subdir string
+ szSubDir = CASC_ALLOC(TCHAR, nLength + 1);
+ if(szSubDir != NULL)
+ {
+ CopyString(szSubDir, szString, nLength);
+ szFullPath = CombinePath(szPath, szSubDir);
+ CASC_FREE(szSubDir);
+ }
- assert(szSrcFileName[i] == 0);
- szTrgFileName[i] = 0;
+ return szFullPath;
}
-void NormalizeFileName_LowerSlash(char * szTrgFileName, const char * szSrcFileName, size_t cchMaxChars)
+size_t NormalizeFileName(const unsigned char * NormTable, char * szNormName, const char * szFileName, size_t cchMaxChars)
{
- char * szTrgFileEnd = szTrgFileName + cchMaxChars;
+ char * szNormNameEnd = szNormName + cchMaxChars;
size_t i;
// Normalize the file name: ToLower + BackSlashToSlash
- for(i = 0; szSrcFileName[i] != 0 && szTrgFileName < szTrgFileEnd; i++)
- szTrgFileName[i] = AsciiToLowerTable_Slash[szSrcFileName[i]];
+ for(i = 0; szFileName[0] != 0 && szNormName < szNormNameEnd; i++)
+ *szNormName++ = NormTable[*szFileName++];
- assert(szSrcFileName[i] == 0);
- szTrgFileName[i] = 0;
+ // Terminate the string
+ szNormName[0] = 0;
+ return i;
+}
+
+size_t NormalizeFileName_UpperBkSlash(char * szNormName, const char * szFileName, size_t cchMaxChars)
+{
+ return NormalizeFileName(AsciiToUpperTable_BkSlash, szNormName, szFileName, cchMaxChars);
+}
+
+size_t NormalizeFileName_LowerSlash(char * szNormName, const char * szFileName, size_t cchMaxChars)
+{
+ return NormalizeFileName(AsciiToLowerTable_Slash, szNormName, szFileName, cchMaxChars);
+}
+
+ULONGLONG CalcFileNameHash(const char * szFileName)
+{
+ char szNormName[MAX_PATH+1];
+ uint32_t dwHashHigh = 0;
+ uint32_t dwHashLow = 0;
+ size_t nLength;
+
+ // Normalize the file name - convert to uppercase, slashes to backslashes
+ nLength = NormalizeFileName_UpperBkSlash(szNormName, szFileName, MAX_PATH);
+
+ // Calculate the HASH value of the normalized file name
+ hashlittle2(szNormName, nLength, &dwHashHigh, &dwHashLow);
+ return ((ULONGLONG)dwHashHigh << 0x20) | dwHashLow;
}
int ConvertDigitToInt32(const TCHAR * szString, PDWORD PtrValue)
@@ -256,6 +284,22 @@ int ConvertDigitToInt32(const TCHAR * szString, PDWORD PtrValue)
return (Digit > 0x0F) ? ERROR_BAD_FORMAT : ERROR_SUCCESS;
}
+int ConvertStringToInt08(const char * 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;
+}
+
int ConvertStringToInt32(const TCHAR * szString, size_t nMaxDigits, PDWORD PtrValue)
{
// The number of digits must be even
@@ -290,6 +334,41 @@ int ConvertStringToInt32(const TCHAR * szString, size_t nMaxDigits, PDWORD PtrVa
return ERROR_SUCCESS;
}
+// Converts string blob to binary blob.
+int ConvertStringToBinary(
+ const char * 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;
+}
+
char * StringFromBinary(LPBYTE pbBinary, size_t cbBinary, char * szBuffer)
{
char * szSaveBuffer = szBuffer;
@@ -308,6 +387,11 @@ char * StringFromBinary(LPBYTE pbBinary, size_t cbBinary, char * szBuffer)
return szSaveBuffer;
}
+char * StringFromMD5(LPBYTE md5, char * szBuffer)
+{
+ return StringFromBinary(md5, MD5_HASH_SIZE, szBuffer);
+}
+
//-----------------------------------------------------------------------------
// File name utilities
@@ -341,87 +425,55 @@ const char * GetPlainFileName(const char * szFileName)
bool CheckWildCard(const char * szString, const char * szWildCard)
{
- const char * szSubString;
- int nSubStringLength;
- int nMatchCount = 0;
+ const char * szWildCardPtr;
- // When the mask is empty, it never matches
- if(szWildCard == NULL || *szWildCard == 0)
- return false;
-
- // If the wildcard contains just "*", then it always matches
- if(szWildCard[0] == '*' && szWildCard[1] == 0)
- return true;
-
- // Do normal test
for(;;)
{
// If there is '?' in the wildcard, we skip one char
- while(*szWildCard == '?')
+ while(szWildCard[0] == '?')
{
+ if(szString[0] == 0)
+ return false;
+
szWildCard++;
szString++;
}
- // If there is '*', means zero or more chars. We have to
- // find the sequence after '*'
- if(*szWildCard == '*')
+ // Handle '*'
+ szWildCardPtr = szWildCard;
+ if(szWildCardPtr[0] != 0)
{
- // More stars is equal to one star
- while(*szWildCard == '*' || *szWildCard == '?')
- szWildCard++;
-
- // If we found end of the wildcard, it's a match
- if(*szWildCard == 0)
- return true;
-
- // Determine the length of the substring in szWildCard
- szSubString = szWildCard;
- while(*szSubString != 0 && *szSubString != '?' && *szSubString != '*')
- szSubString++;
- nSubStringLength = (int)(szSubString - szWildCard);
- nMatchCount = 0;
-
- // Now we have to find a substring in szString,
- // that matches the substring in szWildCard
- while(*szString != 0)
+ if(szWildCardPtr[0] == '*')
{
- // Calculate match count
- while(nMatchCount < nSubStringLength)
- {
- if(AsciiToUpperTable_BkSlash[(BYTE)szString[nMatchCount]] != AsciiToUpperTable_BkSlash[(BYTE)szWildCard[nMatchCount]])
- break;
- if(szString[nMatchCount] == 0)
- break;
- nMatchCount++;
- }
+ szWildCardPtr++;
+
+ if(szWildCardPtr[0] == '*')
+ continue;
+
+ if(szWildCardPtr[0] == 0)
+ return true;
- // If the match count has reached substring length, we found a match
- if(nMatchCount == nSubStringLength)
+ if(AsciiToUpperTable_BkSlash[szWildCardPtr[0]] == AsciiToUpperTable_BkSlash[szString[0]])
{
- szWildCard += nMatchCount;
- szString += nMatchCount;
- break;
+ if(CheckWildCard(szString, szWildCardPtr))
+ return true;
}
+ }
+ else
+ {
+ if(AsciiToUpperTable_BkSlash[szWildCardPtr[0]] != AsciiToUpperTable_BkSlash[szString[0]])
+ return false;
- // No match, move to the next char in szString
- nMatchCount = 0;
- szString++;
+ szWildCard = szWildCardPtr + 1;
}
+
+ if(szString[0] == 0)
+ return false;
+ szString++;
}
else
{
- // If we came to the end of the string, compare it to the wildcard
- if(AsciiToUpperTable_BkSlash[(BYTE)*szString] != AsciiToUpperTable_BkSlash[(BYTE)*szWildCard])
- return false;
-
- // If we arrived to the end of the string, it's a match
- if(*szString == 0)
- return true;
-
- // Otherwise, continue in comparing
- szWildCard++;
- szString++;
+ return (szString[0] == 0) ? true : false;
}
}
}