TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CascFindFile.cpp File Reference
#include "CascLib.h"
#include "CascCommon.h"
+ Include dependency graph for CascFindFile.cpp:

Macros

#define __CASCLIB_SELF__
 

Functions

static TCascSearchIsValidSearchHandle (HANDLE hFind)
 
static void FreeSearchHandle (TCascSearch *pSearch)
 
static TCascSearchAllocateSearchHandle (TCascStorage *hs, const TCHAR *szListFile, const char *szMask)
 
static bool DoStorageSearch_RootFile (TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
 
static bool DoStorageSearch_EncodingKey (TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
 
static bool DoStorageSearch (TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
 
HANDLE WINAPI CascFindFirstFile (HANDLE hStorage, const char *szMask, PCASC_FIND_DATA pFindData, const TCHAR *szListFile)
 
bool WINAPI CascFindNextFile (HANDLE hFind, PCASC_FIND_DATA pFindData)
 
bool WINAPI CascFindClose (HANDLE hFind)
 

Macro Definition Documentation

#define __CASCLIB_SELF__

Function Documentation

static TCascSearch* AllocateSearchHandle ( TCascStorage hs,
const TCHAR szListFile,
const char *  szMask 
)
static
57 {
58  TCascSearch * pSearch;
59  size_t cbToAllocate;
60 
61  // When using the MNDX info, do not allocate the extra bit array
62  cbToAllocate = sizeof(TCascSearch) + ((hs->pEncodingMap->TableSize + 7) / 8);
63  pSearch = (TCascSearch *)CASC_ALLOC(BYTE, cbToAllocate);
64  if(pSearch != NULL)
65  {
66  // Initialize the structure
67  memset(pSearch, 0, cbToAllocate);
68  pSearch->szClassName = "TCascSearch";
69 
70  // Save the search handle
71  pSearch->hs = hs;
72  hs->dwRefCount++;
73 
74  // If the mask was not given, use default
75  if(szMask == NULL)
76  szMask = "*";
77 
78  // Save the other variables
79  if(szListFile != NULL)
80  {
81  pSearch->szListFile = CascNewStr(szListFile, 0);
82  if(pSearch->szListFile == NULL)
83  {
84  FreeSearchHandle(pSearch);
85  return NULL;
86  }
87  }
88 
89  // Allocate the search mask
90  pSearch->szMask = CascNewStr(szMask, 0);
91  if(pSearch->szMask == NULL)
92  {
93  FreeSearchHandle(pSearch);
94  return NULL;
95  }
96  }
97 
98  return pSearch;
99 }
#define CASC_ALLOC(type, count)
Definition: CascCommon.h:302
struct _TCascSearch TCascSearch
TCascStorage * hs
Definition: CascCommon.h:263
arena_t NULL
Definition: jemalloc_internal.h:624
size_t TableSize
Definition: Map.h:21
const char * szClassName
Definition: CascCommon.h:264
char * szMask
Definition: CascCommon.h:267
PCASC_MAP pEncodingMap
Definition: CascCommon.h:220
Definition: CascCommon.h:261
DWORD dwRefCount
Definition: CascCommon.h:195
unsigned char BYTE
Definition: CascPort.h:136
static void FreeSearchHandle(TCascSearch *pSearch)
Definition: CascFindFile.cpp:25
char * CascNewStr(const char *szString, size_t nCharsToReserve)
Definition: Common.cpp:102
TCHAR * szListFile
Definition: CascCommon.h:265

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool WINAPI CascFindClose ( HANDLE  hFind)
311 {
312  TCascSearch * pSearch;
313 
314  pSearch = IsValidSearchHandle(hFind);
315  if(pSearch == NULL)
316  {
318  return false;
319  }
320 
321  FreeSearchHandle(pSearch);
322  return true;
323 }
arena_t NULL
Definition: jemalloc_internal.h:624
void SetLastError(int nError)
Definition: Common.cpp:75
Definition: CascCommon.h:261
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:210
static TCascSearch * IsValidSearchHandle(HANDLE hFind)
Definition: CascFindFile.cpp:18
static void FreeSearchHandle(TCascSearch *pSearch)
Definition: CascFindFile.cpp:25

+ Here is the call graph for this function:

HANDLE WINAPI CascFindFirstFile ( HANDLE  hStorage,
const char *  szMask,
PCASC_FIND_DATA  pFindData,
const TCHAR szListFile 
)
253 {
254  TCascStorage * hs;
255  TCascSearch * pSearch = NULL;
256  int nError = ERROR_SUCCESS;
257 
258  // Check parameters
259  if((hs = IsValidStorageHandle(hStorage)) == NULL)
260  nError = ERROR_INVALID_HANDLE;
261  if(szMask == NULL || pFindData == NULL)
262  nError = ERROR_INVALID_PARAMETER;
263 
264  // Init the search structure and search handle
265  if(nError == ERROR_SUCCESS)
266  {
267  // Clear the entire search structure
268  memset(pFindData, 0, sizeof(CASC_FIND_DATA));
269 
270  // Allocate the search handle
271  pSearch = AllocateSearchHandle(hs, szListFile, szMask);
272  if(pSearch == NULL)
273  nError = ERROR_NOT_ENOUGH_MEMORY;
274  }
275 
276  // Perform search
277  if(nError == ERROR_SUCCESS)
278  {
279  if(!DoStorageSearch(pSearch, pFindData))
280  nError = ERROR_NO_MORE_FILES;
281  }
282 
283  if(nError != ERROR_SUCCESS)
284  {
285  if(pSearch != NULL)
286  FreeSearchHandle(pSearch);
287  pSearch = NULL;
288  }
289 
290  return (HANDLE)pSearch;
291 }
static bool DoStorageSearch(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:207
void * HANDLE
Definition: CascPort.h:146
#define ERROR_NOT_ENOUGH_MEMORY
Definition: CascPort.h:208
arena_t NULL
Definition: jemalloc_internal.h:624
#define ERROR_NO_MORE_FILES
Definition: CascPort.h:215
Definition: CascLib.h:145
#define ERROR_INVALID_HANDLE
Definition: CascPort.h:207
static TCascSearch * AllocateSearchHandle(TCascStorage *hs, const TCHAR *szListFile, const char *szMask)
Definition: CascFindFile.cpp:56
Definition: CascCommon.h:186
Definition: CascCommon.h:261
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:210
TCascStorage * IsValidStorageHandle(HANDLE hStorage)
Definition: CascOpenStorage.cpp:100
static void FreeSearchHandle(TCascSearch *pSearch)
Definition: CascFindFile.cpp:25
#define ERROR_SUCCESS
Definition: CascPort.h:204

+ Here is the call graph for this function:

bool WINAPI CascFindNextFile ( HANDLE  hFind,
PCASC_FIND_DATA  pFindData 
)
296 {
297  TCascSearch * pSearch;
298 
299  pSearch = IsValidSearchHandle(hFind);
300  if(pSearch == NULL || pFindData == NULL)
301  {
303  return false;
304  }
305 
306  // Perform search
307  return DoStorageSearch(pSearch, pFindData);
308 }
static bool DoStorageSearch(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:207
arena_t NULL
Definition: jemalloc_internal.h:624
void SetLastError(int nError)
Definition: Common.cpp:75
Definition: CascCommon.h:261
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:210
static TCascSearch * IsValidSearchHandle(HANDLE hFind)
Definition: CascFindFile.cpp:18

+ Here is the call graph for this function:

static bool DoStorageSearch ( TCascSearch pSearch,
PCASC_FIND_DATA  pFindData 
)
static
208 {
209  // State 0: No search done yet
210  if(pSearch->dwState == 0)
211  {
212  // Does the search specify listfile?
213  if(pSearch->szListFile != NULL)
214  pSearch->pCache = ListFile_OpenExternal(pSearch->szListFile);
215 
216  // Move the search phase to the listfile searching
217  pSearch->IndexLevel1 = 0;
218  pSearch->dwState++;
219  }
220 
221  // State 1: Searching the list file
222  if(pSearch->dwState == 1)
223  {
224  if(DoStorageSearch_RootFile(pSearch, pFindData))
225  return true;
226 
227  // Move to the nameless search state
228  pSearch->IndexLevel1 = 0;
229  pSearch->dwState++;
230  }
231 
232  // State 2: Searching the remaining entries
233  if(pSearch->dwState == 2)
234  {
235  if(DoStorageSearch_EncodingKey(pSearch, pFindData))
236  return true;
237 
238  // Move to the final search state
239  pSearch->dwState++;
240  }
241 
242  return false;
243 }
void * ListFile_OpenExternal(const TCHAR *szListFile)
Definition: ListFile.cpp:52
DWORD dwState
Definition: CascCommon.h:274
static bool DoStorageSearch_RootFile(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:103
arena_t NULL
Definition: jemalloc_internal.h:624
void * pCache
Definition: CascCommon.h:266
static bool DoStorageSearch_EncodingKey(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:159
size_t IndexLevel1
Definition: CascCommon.h:272
TCHAR * szListFile
Definition: CascCommon.h:265

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool DoStorageSearch_EncodingKey ( TCascSearch pSearch,
PCASC_FIND_DATA  pFindData 
)
static
160 {
161  PCASC_ENCODING_ENTRY pEncodingEntry;
162  PCASC_INDEX_ENTRY pIndexEntry;
163  TCascStorage * hs = pSearch->hs;
164  QUERY_KEY IndexKey;
165  DWORD ByteIndex;
166  DWORD BitMask;
167 
168  // Check for encoding keys that haven't been found yet
169  while(pSearch->IndexLevel1 < hs->pEncodingMap->TableSize)
170  {
171  // Check if that entry has been reported before
172  ByteIndex = (DWORD)(pSearch->IndexLevel1 / 8);
173  BitMask = 1 << (pSearch->IndexLevel1 & 0x07);
174  if((pSearch->BitArray[ByteIndex] & BitMask) == 0)
175  {
176  // Locate the index entry
177  pEncodingEntry = (PCASC_ENCODING_ENTRY)hs->pEncodingMap->HashTable[pSearch->IndexLevel1];
178  if(pEncodingEntry != NULL)
179  {
180  IndexKey.pbData = GET_INDEX_KEY(pEncodingEntry);
181  IndexKey.cbData = MD5_HASH_SIZE;
182  pIndexEntry = FindIndexEntry(pSearch->hs, &IndexKey);
183  if(pIndexEntry != NULL)
184  {
185  // Fill-in the found file
186  memcpy(pFindData->EncodingKey, pEncodingEntry->EncodingKey, MD5_HASH_SIZE);
187  pFindData->szFileName[0] = 0;
188  pFindData->szPlainName = NULL;
189  pFindData->dwLocaleFlags = CASC_LOCALE_NONE;
190  pFindData->dwFileSize = ConvertBytesToInteger_4(pEncodingEntry->FileSizeBE);
191 
192  // Mark the entry as already-found
193  pSearch->BitArray[ByteIndex] |= BitMask;
194  return true;
195  }
196  }
197  }
198 
199  // Go to the next encoding entry
200  pSearch->IndexLevel1++;
201  }
202 
203  // Nameless search ended
204  return false;
205 }
BYTE FileSizeBE[4]
Definition: CascCommon.h:162
TCascStorage * hs
Definition: CascCommon.h:263
char * szPlainName
Definition: CascLib.h:148
Definition: CascLib.h:138
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASC_LOCALE_NONE
Definition: CascLib.h:65
size_t TableSize
Definition: Map.h:21
DWORD cbData
Definition: CascLib.h:141
DWORD dwFileSize
Definition: CascLib.h:151
#define MD5_HASH_SIZE
Definition: CascLib.h:105
struct _CASC_ENCODING_ENTRY * PCASC_ENCODING_ENTRY
Definition: CascCommon.h:186
LPBYTE pbData
Definition: CascLib.h:140
PCASC_MAP pEncodingMap
Definition: CascCommon.h:220
BYTE EncodingKey[MD5_HASH_SIZE]
Definition: CascLib.h:149
unsigned int DWORD
Definition: CascPort.h:139
char szFileName[MAX_PATH]
Definition: CascLib.h:147
BYTE BitArray[1]
Definition: CascCommon.h:276
Definition: CascCommon.h:102
BYTE EncodingKey[MD5_HASH_SIZE]
Definition: CascCommon.h:163
DWORD dwLocaleFlags
Definition: CascLib.h:150
void * HashTable[1]
Definition: Map.h:25
Definition: CascCommon.h:159
#define GET_INDEX_KEY(pEncodingEntry)
Definition: CascCommon.h:180
DWORD ConvertBytesToInteger_4(LPBYTE ValueAsBytes)
Definition: CascCommon.cpp:31
size_t IndexLevel1
Definition: CascCommon.h:272
PCASC_INDEX_ENTRY FindIndexEntry(TCascStorage *hs, PQUERY_KEY pIndexKey)
Definition: CascOpenFile.cpp:25

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool DoStorageSearch_RootFile ( TCascSearch pSearch,
PCASC_FIND_DATA  pFindData 
)
static
104 {
105  PCASC_ENCODING_ENTRY pEncodingEntry;
106  PCASC_INDEX_ENTRY pIndexEntry;
107  QUERY_KEY EncodingKey;
108  QUERY_KEY IndexKey;
109  LPBYTE pbEncodingKey;
110  DWORD EncodingIndex = 0;
111  DWORD LocaleFlags = 0;
112  DWORD FileSize = CASC_INVALID_SIZE;
113  DWORD ByteIndex;
114  DWORD BitMask;
115 
116  for(;;)
117  {
118  // Attempt to find (the next) file from the root entry
119  pbEncodingKey = RootHandler_Search(pSearch->hs->pRootHandler, pSearch, &FileSize, &LocaleFlags);
120  if(pbEncodingKey == NULL)
121  return false;
122 
123  // Verify whether the encoding key exists in the encoding table
124  EncodingKey.pbData = pbEncodingKey;
125  EncodingKey.cbData = MD5_HASH_SIZE;
126  pEncodingEntry = FindEncodingEntry(pSearch->hs, &EncodingKey, &EncodingIndex);
127  if(pEncodingEntry != NULL)
128  {
129  // Mark the item as already found
130  // Note: Duplicate items are allowed while we are searching using file names
131  // Do not exclude items from search if they were found before
132  ByteIndex = (DWORD)(EncodingIndex / 8);
133  BitMask = 1 << (EncodingIndex & 0x07);
134  pSearch->BitArray[ByteIndex] |= BitMask;
135 
136  // Locate the index entry
137  IndexKey.pbData = GET_INDEX_KEY(pEncodingEntry);
138  IndexKey.cbData = MD5_HASH_SIZE;
139  pIndexEntry = FindIndexEntry(pSearch->hs, &IndexKey);
140  if(pIndexEntry == NULL)
141  continue;
142 
143  // If we retrieved the file size directly from the root provider, use it
144  // Otherwise, we need to retrieve it from the encoding entry
145  if(FileSize == CASC_INVALID_SIZE)
146  FileSize = ConvertBytesToInteger_4(pEncodingEntry->FileSizeBE);
147 
148  // Fill-in the found file
149  strcpy(pFindData->szFileName, pSearch->szFileName);
150  memcpy(pFindData->EncodingKey, pEncodingEntry->EncodingKey, MD5_HASH_SIZE);
151  pFindData->szPlainName = (char *)GetPlainFileName(pFindData->szFileName);
152  pFindData->dwLocaleFlags = LocaleFlags;
153  pFindData->dwFileSize = FileSize;
154  return true;
155  }
156  }
157 }
TRootHandler * pRootHandler
Definition: CascCommon.h:223
const wchar_t * GetPlainFileName(const wchar_t *szFileName)
Definition: Common.cpp:398
BYTE FileSizeBE[4]
Definition: CascCommon.h:162
TCascStorage * hs
Definition: CascCommon.h:263
char * szPlainName
Definition: CascLib.h:148
Definition: CascLib.h:138
arena_t NULL
Definition: jemalloc_internal.h:624
BYTE * LPBYTE
Definition: CascPort.h:152
LPBYTE RootHandler_Search(TRootHandler *pRootHandler, struct _TCascSearch *pSearch, PDWORD PtrFileSize, PDWORD PtrLocaleFlags)
Definition: RootHandler.cpp:26
DWORD cbData
Definition: CascLib.h:141
DWORD dwFileSize
Definition: CascLib.h:151
#define MD5_HASH_SIZE
Definition: CascLib.h:105
#define CASC_INVALID_SIZE
Definition: CascLib.h:118
LPBYTE pbData
Definition: CascLib.h:140
BYTE EncodingKey[MD5_HASH_SIZE]
Definition: CascLib.h:149
char szFileName[MAX_PATH]
Definition: CascCommon.h:268
unsigned int DWORD
Definition: CascPort.h:139
char szFileName[MAX_PATH]
Definition: CascLib.h:147
BYTE BitArray[1]
Definition: CascCommon.h:276
Definition: CascCommon.h:102
BYTE EncodingKey[MD5_HASH_SIZE]
Definition: CascCommon.h:163
DWORD dwLocaleFlags
Definition: CascLib.h:150
Definition: CascCommon.h:159
#define GET_INDEX_KEY(pEncodingEntry)
Definition: CascCommon.h:180
PCASC_ENCODING_ENTRY FindEncodingEntry(TCascStorage *hs, PQUERY_KEY pEncodingKey, PDWORD PtrIndex)
Definition: CascOpenFile.cpp:35
DWORD ConvertBytesToInteger_4(LPBYTE ValueAsBytes)
Definition: CascCommon.cpp:31
PCASC_INDEX_ENTRY FindIndexEntry(TCascStorage *hs, PQUERY_KEY pIndexKey)
Definition: CascOpenFile.cpp:25

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void FreeSearchHandle ( TCascSearch pSearch)
static
26 {
27  // Only if the storage handle is valid
28  assert(pSearch != NULL);
29 
30  // Close (dereference) the archive handle
31  if(pSearch->hs != NULL)
32  {
33  // Give root handler chance to free their stuff
34  RootHandler_EndSearch(pSearch->hs->pRootHandler, pSearch);
35 
36  // Dereference the storage handle
37  CascCloseStorage((HANDLE)pSearch->hs);
38  pSearch->hs = NULL;
39  }
40 
41  // Free the file cache and frame array
42  if(pSearch->szMask != NULL)
43  CASC_FREE(pSearch->szMask);
44  if(pSearch->szListFile != NULL)
45  CASC_FREE(pSearch->szListFile);
46 // if(pSearch->pStruct1C != NULL)
47 // delete pSearch->pStruct1C;
48  if(pSearch->pCache != NULL)
49  ListFile_Free(pSearch->pCache);
50 
51  // Free the structure itself
52  pSearch->szClassName = NULL;
53  CASC_FREE(pSearch);
54 }
TRootHandler * pRootHandler
Definition: CascCommon.h:223
void * HANDLE
Definition: CascPort.h:146
TCascStorage * hs
Definition: CascCommon.h:263
void ListFile_Free(void *pvListFile)
Definition: ListFile.cpp:201
arena_t NULL
Definition: jemalloc_internal.h:624
void * pCache
Definition: CascCommon.h:266
#define CASC_FREE(ptr)
Definition: CascCommon.h:303
bool WINAPI CascCloseStorage(HANDLE hStorage)
Definition: CascOpenStorage.cpp:1133
void RootHandler_EndSearch(TRootHandler *pRootHandler, struct _TCascSearch *pSearch)
Definition: RootHandler.cpp:35
const char * szClassName
Definition: CascCommon.h:264
char * szMask
Definition: CascCommon.h:267
TCHAR * szListFile
Definition: CascCommon.h:265

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static TCascSearch* IsValidSearchHandle ( HANDLE  hFind)
static
19 {
20  TCascSearch * pSearch = (TCascSearch *)hFind;
21 
22  return (pSearch != NULL && pSearch->szClassName != NULL && !strcmp(pSearch->szClassName, "TCascSearch") && pSearch->szMask != NULL) ? pSearch : NULL;
23 }
arena_t NULL
Definition: jemalloc_internal.h:624
const char * szClassName
Definition: CascCommon.h:264
char * szMask
Definition: CascCommon.h:267
Definition: CascCommon.h:261

+ Here is the caller graph for this function: