aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/common
diff options
context:
space:
mode:
authorNayd <dnpd.dd@gmail.com>2015-01-11 22:12:49 +0000
committerNayd <dnpd.dd@gmail.com>2015-01-11 23:02:19 +0000
commit31e3dc2e75459f681480ae09641f1087096dfd69 (patch)
tree70685fc84efd01d450f8bc06a4cb266e6478b694 /dep/CascLib/src/common
parent8bbd9133d57fc34b77544cbdd59526ed9ccaa607 (diff)
Dep/CascLib: Update to https://github.com/ladislav-zezula/CascLib/commit/5d3789af3435534c288c2145e158d422651c7fe1
Closes #13866
Diffstat (limited to 'dep/CascLib/src/common')
-rw-r--r--dep/CascLib/src/common/Common.cpp23
-rw-r--r--dep/CascLib/src/common/Common.h9
-rw-r--r--dep/CascLib/src/common/FileStream.cpp8
-rw-r--r--dep/CascLib/src/common/ListFile.cpp163
-rw-r--r--dep/CascLib/src/common/ListFile.h32
-rw-r--r--dep/CascLib/src/common/Map.h2
6 files changed, 208 insertions, 29 deletions
diff --git a/dep/CascLib/src/common/Common.cpp b/dep/CascLib/src/common/Common.cpp
index d81c1b6fd89..74f86d44853 100644
--- a/dep/CascLib/src/common/Common.cpp
+++ b/dep/CascLib/src/common/Common.cpp
@@ -62,20 +62,6 @@ unsigned char AsciiToUpperTable[256] =
unsigned char IntToHexChar[] = "0123456789abcdef";
//-----------------------------------------------------------------------------
-// Support for memory reallocation
-
-#if defined(_MSC_VER) && defined(_DEBUG)
-void * DbgRealloc(void * ptr, size_t nSize)
-{
- // HeapReAlloc does not support NULL as previous block
- if(ptr == NULL)
- return HeapAlloc(GetProcessHeap, 0, nSize);
-
- return HeapReAlloc(GetProcessHeap(), 0, ptr, nSize);
-}
-#endif
-
-//-----------------------------------------------------------------------------
// GetLastError/SetLastError support for non-Windows platform
#ifndef PLATFORM_WINDOWS
@@ -232,11 +218,14 @@ TCHAR * CombinePath(const TCHAR * szDirectory, const TCHAR * szSubDir)
return szFullPath;
}
-void NormalizeFileName_UpperBkSlash(char * szFileName)
+void NormalizeFileName_UpperBkSlash(const char * szSrcFileName, char * szTrgFileName)
{
+ size_t i;
+
// Normalize the file name: ToLower + BackSlashToSlash
- for(size_t i = 0; szFileName[i] != 0; i++)
- szFileName[i] = AsciiToUpperTable[szFileName[i]];
+ for(i = 0; szSrcFileName[i] != 0; i++)
+ szTrgFileName[i] = AsciiToUpperTable[szSrcFileName[i]];
+ szTrgFileName[i] = 0;
}
void NormalizeFileName_LowerSlash(char * szFileName)
diff --git a/dep/CascLib/src/common/Common.h b/dep/CascLib/src/common/Common.h
index eb192fd50ca..40712b99eed 100644
--- a/dep/CascLib/src/common/Common.h
+++ b/dep/CascLib/src/common/Common.h
@@ -29,13 +29,6 @@ extern unsigned char AsciiToUpperTable[256];
extern unsigned char IntToHexChar[];
//-----------------------------------------------------------------------------
-// Memory management helper
-
-#if defined(_MSC_VER) && defined(_DEBUG)
-void * DbgRealloc(void * ptr, size_t nSize);
-#endif
-
-//-----------------------------------------------------------------------------
// GetLastError/SetLastError support for non-Windows platform
#ifndef PLATFORM_WINDOWS
@@ -57,7 +50,7 @@ TCHAR * NewStrFromAnsi(LPBYTE pbStringBegin, LPBYTE pbStringEnd);
TCHAR * CombinePath(const TCHAR * szPath, const TCHAR * szSubDir);
-void NormalizeFileName_UpperBkSlash(char * szFileName);
+void NormalizeFileName_UpperBkSlash(const char * szSrcFileName, char * szTrgFileName);
void NormalizeFileName_LowerSlash(char * szFileName);
int ConvertDigitToInt32(const TCHAR * szString, PDWORD PtrValue);
diff --git a/dep/CascLib/src/common/FileStream.cpp b/dep/CascLib/src/common/FileStream.cpp
index 09ac47bd095..8e541b44fca 100644
--- a/dep/CascLib/src/common/FileStream.cpp
+++ b/dep/CascLib/src/common/FileStream.cpp
@@ -2551,6 +2551,14 @@ bool FileStream_SetCallback(TFileStream * pStream, STREAM_DOWNLOAD_CALLBACK pfnC
*/
bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead)
{
+ //FILE * fp = fopen("E:\\Loading.txt", "at");
+ //if(fp != NULL)
+ //{
+ // ULONGLONG ByteOffset = (pByteOffset != NULL) ? pByteOffset[0] : 0;
+ // fprintf(fp, "%-32ws\t%08X\t%08X\n", GetPlainFileName(pStream->szFileName), (ULONG)ByteOffset, dwBytesToRead);
+ // fclose(fp);
+ //}
+
assert(pStream->StreamRead != NULL);
return pStream->StreamRead(pStream, pByteOffset, pvBuffer, dwBytesToRead);
}
diff --git a/dep/CascLib/src/common/ListFile.cpp b/dep/CascLib/src/common/ListFile.cpp
index 4b742b1d07c..8e0b1374241 100644
--- a/dep/CascLib/src/common/ListFile.cpp
+++ b/dep/CascLib/src/common/ListFile.cpp
@@ -125,12 +125,12 @@ static size_t ReadListFileLine(TListFileCache * pCache, char * szLine, size_t nM
}
// If we have found a newline, stop loading
- if(*pCache->pPos == 0x0D || *pCache->pPos == 0x0A)
+ if(pCache->pPos[0] == 0x0D || pCache->pPos[0] == 0x0A)
break;
// Blizzard listfiles can also contain information about patch:
// Pass1\Files\MacOS\unconditional\user\Background Downloader.app\Contents\Info.plist~Patch(Data#frFR#base-frFR,1326)
- if(*pCache->pPos == '~')
+ if(pCache->pPos[0] == '~')
szExtraString = szLine;
// Copy the character
@@ -190,7 +190,7 @@ static TListFileCache * CreateListFileCache(RELOAD_CACHE pfnReloadCache, CLOSE_S
}
//-----------------------------------------------------------------------------
-// Listfile functions
+// Functions for parsing an external listfile
void * ListFile_OpenExternal(const TCHAR * szListFile)
{
@@ -264,3 +264,160 @@ void ListFile_Free(void * pvListFile)
CASC_FREE(pCache);
}
}
+
+//-----------------------------------------------------------------------------
+// Functions for creating a listfile map
+
+#define LISTMAP_INITIAL 0x100000
+
+static PLISTFILE_MAP ListMap_Create()
+{
+ PLISTFILE_MAP pListMap;
+ size_t cbToAllocate;
+
+ // Create buffer for the listfile
+ // Note that because the listfile is quite big and CASC_REALLOC
+ // is a costly operation, we want to have as few reallocs as possible.
+ cbToAllocate = sizeof(LISTFILE_MAP) + LISTMAP_INITIAL;
+ pListMap = (PLISTFILE_MAP)CASC_ALLOC(BYTE, cbToAllocate);
+ if(pListMap != NULL)
+ {
+ // Fill the listfile buffer
+ memset(pListMap, 0, sizeof(LISTFILE_MAP));
+ pListMap->cbBufferMax = LISTMAP_INITIAL;
+ }
+
+ return pListMap;
+}
+
+static PLISTFILE_MAP ListMap_InsertName(PLISTFILE_MAP pListMap, const char * szFileName, size_t nLength)
+{
+ PLISTFILE_ENTRY pListEntry;
+ char szFileName2[MAX_PATH+1];
+ size_t cbToAllocate;
+ size_t cbEntrySize;
+ uint32_t dwHashHigh = 0;
+ uint32_t dwHashLow = 0;
+
+ // Make sure there is enough space in the list map
+ cbEntrySize = sizeof(LISTFILE_ENTRY) + nLength;
+ cbEntrySize = ALIGN_TO_SIZE(cbEntrySize, 8);
+ if((pListMap->cbBuffer + cbEntrySize) > pListMap->cbBufferMax)
+ {
+ cbToAllocate = sizeof(LISTFILE_MAP) + (pListMap->cbBufferMax * 3) / 2;
+ pListMap = (PLISTFILE_MAP)CASC_REALLOC(BYTE, pListMap, cbToAllocate);
+ if(pListMap == NULL)
+ return NULL;
+
+ pListMap->cbBufferMax = (pListMap->cbBufferMax * 3) / 2;
+ }
+
+ // Get the pointer to the first entry
+ pListEntry = (PLISTFILE_ENTRY)((LPBYTE)(pListMap + 1) + pListMap->cbBuffer);
+
+ // Get the name hash
+ NormalizeFileName_UpperBkSlash(szFileName, szFileName2);
+ hashlittle2(szFileName2, nLength, &dwHashHigh, &dwHashLow);
+
+ // Calculate the HASH value of the normalized file name
+ pListEntry->FileNameHash = ((ULONGLONG)dwHashHigh << 0x20) | dwHashLow;
+ pListEntry->cbEntrySize = (DWORD)cbEntrySize;
+ memcpy(pListEntry->szFileName, szFileName, nLength);
+ pListEntry->szFileName[nLength] = 0;
+
+ // Move the next entry
+ pListMap->cbBuffer += cbEntrySize;
+ pListMap->nEntries++;
+ return pListMap;
+}
+
+static PLISTFILE_MAP ListMap_Finish(PLISTFILE_MAP pListMap)
+{
+ PLISTFILE_ENTRY pListEntry;
+ PCASC_MAP pMap;
+ LPBYTE pbEntry;
+
+ // Sanity check
+ assert(pListMap->pNameMap == NULL);
+
+ // Create the map
+ pListMap->pNameMap = pMap = Map_Create((DWORD)pListMap->nEntries, sizeof(ULONGLONG), 0);
+ if(pListMap->pNameMap == NULL)
+ {
+ ListFile_FreeMap(pListMap);
+ return NULL;
+ }
+
+ // Fill the map
+ pbEntry = (LPBYTE)(pListMap + 1);
+ for(size_t i = 0; i < pListMap->nEntries; i++)
+ {
+ // Get the listfile entry
+ pListEntry = (PLISTFILE_ENTRY)pbEntry;
+ pbEntry += pListEntry->cbEntrySize;
+
+ // Insert the entry to the map
+ Map_InsertObject(pMap, pListEntry);
+ }
+
+ return pListMap;
+}
+
+PLISTFILE_MAP ListFile_CreateMap(const TCHAR * szListFile)
+{
+ PLISTFILE_MAP pListMap = NULL;
+ void * pvListFile;
+ char szFileName[MAX_PATH+1];
+ size_t nLength;
+
+ // Only if the listfile name has been given
+ if(szListFile != NULL)
+ {
+ // Create map for the listfile
+ pListMap = ListMap_Create();
+ if(pListMap != NULL)
+ {
+ // Open the external listfile
+ pvListFile = ListFile_OpenExternal(szListFile);
+ if(pvListFile != NULL)
+ {
+ // Go through the entire listfile and insert each name to the map
+ while((nLength = ListFile_GetNext(pvListFile, "*", szFileName, MAX_PATH)) != 0)
+ {
+ // Insert the file name to the map
+ pListMap = ListMap_InsertName(pListMap, szFileName, nLength);
+ if(pListMap == NULL)
+ break;
+ }
+
+ // Finish the listfile map
+ pListMap = ListMap_Finish(pListMap);
+
+ // Free the listfile
+ ListFile_Free(pvListFile);
+ }
+ }
+ }
+
+ // Return the created map
+ return pListMap;
+}
+
+const char * ListFile_FindName(PLISTFILE_MAP pListMap, ULONGLONG FileNameHash)
+{
+ PLISTFILE_ENTRY pListEntry = NULL;
+
+ if(pListMap != NULL)
+ pListEntry = (PLISTFILE_ENTRY)Map_FindObject(pListMap->pNameMap, &FileNameHash);
+ return (pListEntry != NULL) ? pListEntry->szFileName : "";
+}
+
+void ListFile_FreeMap(PLISTFILE_MAP pListMap)
+{
+ if(pListMap != NULL)
+ {
+ if(pListMap->pNameMap != NULL)
+ Map_Free(pListMap->pNameMap);
+ CASC_FREE(pListMap);
+ }
+}
diff --git a/dep/CascLib/src/common/ListFile.h b/dep/CascLib/src/common/ListFile.h
index 1c603af3766..9815160e1ea 100644
--- a/dep/CascLib/src/common/ListFile.h
+++ b/dep/CascLib/src/common/ListFile.h
@@ -11,8 +11,40 @@
#ifndef __LISTFILE_H__
#define __LISTFILE_H__
+//-----------------------------------------------------------------------------
+// Structures
+
+typedef struct _LISTFILE_ENTRY
+{
+ ULONGLONG FileNameHash; // Hash of the file name
+ DWORD cbEntrySize; // Length of this entry, in bytes
+ char szFileName[1]; // File name, aligned to 8-byte boundary
+
+} LISTFILE_ENTRY, *PLISTFILE_ENTRY;
+
+typedef struct _LISTFILE_MAP
+{
+ PCASC_MAP pNameMap; // Map of hash-to-name
+ size_t cbBufferMax; // Total size of the buffer, in bytes
+ size_t cbBuffer; // Current size of the buffer, in bytes
+ size_t nEntries; // Number of entries
+
+ // First LISTFILE_ENTRY starts here
+
+} LISTFILE_MAP, *PLISTFILE_MAP;
+
+//-----------------------------------------------------------------------------
+// Functions for parsing an external listfile
+
void * ListFile_OpenExternal(const TCHAR * szListFile);
size_t ListFile_GetNext(void * pvListFile, const char * szMask, char * szBuffer, size_t nMaxChars);
void ListFile_Free(void * pvListFile);
+//-----------------------------------------------------------------------------
+// Functions for creating a listfile map
+
+PLISTFILE_MAP ListFile_CreateMap(const TCHAR * szListFile);
+const char * ListFile_FindName(PLISTFILE_MAP pListMap, ULONGLONG FileNameHash);
+void ListFile_FreeMap(PLISTFILE_MAP pListMap);
+
#endif // __LISTFILE_H__
diff --git a/dep/CascLib/src/common/Map.h b/dep/CascLib/src/common/Map.h
index b4b9c918df3..40ea4238b81 100644
--- a/dep/CascLib/src/common/Map.h
+++ b/dep/CascLib/src/common/Map.h
@@ -14,7 +14,7 @@
//-----------------------------------------------------------------------------
// Structures
-#define KEY_LENGTH_STRING 0xFFFFFFFF // Pass this to Map_Create as dwKeyLength when you want map of string->object
+#define KEY_LENGTH_STRING 0xFFFFFFFF // Pass this to Map_Create as dwKeyLength when you want map of string->object
typedef struct _CASC_MAP
{