TrinityCore
CascFindFile.cpp File Reference
#include "CascLib.h"
#include "CascCommon.h"
+ Include dependency graph for CascFindFile.cpp:

Macros

#define __CASCLIB_SELF__
 

Functions

static void ResetFindData (PCASC_FIND_DATA pFindData)
 
static void SupplyFakeFileName (PCASC_FIND_DATA pFindData, PCASC_CKEY_ENTRY pCKeyEntry)
 
static bool CopyCKeyEntryToFindData (PCASC_FIND_DATA pFindData, PCASC_CKEY_ENTRY pCKeyEntry)
 
static bool DoStorageSearch_RootFile (TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
 
static bool DoStorageSearch_CKey (TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
 
static bool DoStorageSearch (TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
 
HANDLE WINAPI CascFindFirstFile (HANDLE hStorage, LPCSTR szMask, PCASC_FIND_DATA pFindData, LPCTSTR szListFile)
 
bool WINAPI CascFindNextFile (HANDLE hFind, PCASC_FIND_DATA pFindData)
 
bool WINAPI CascFindClose (HANDLE hFind)
 

Macro Definition Documentation

◆ __CASCLIB_SELF__

#define __CASCLIB_SELF__

Function Documentation

◆ CascFindClose()

bool WINAPI CascFindClose ( HANDLE  hFind)
259 {
260  TCascSearch * pSearch;
261 
262  pSearch = TCascSearch::IsValid(hFind);
263  if(pSearch == NULL)
264  {
266  return false;
267  }
268 
269  delete pSearch;
270  return true;
271 }
void SetCascError(DWORD dwErrCode)
Definition: Common.cpp:91
Definition: CascCommon.h:380
static TCascSearch * IsValid(HANDLE hFind)
Definition: CascCommon.h:411
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:237
+ Here is the call graph for this function:

◆ CascFindFirstFile()

HANDLE WINAPI CascFindFirstFile ( HANDLE  hStorage,
LPCSTR  szMask,
PCASC_FIND_DATA  pFindData,
LPCTSTR  szListFile 
)
201 {
202  TCascStorage * hs;
203  TCascSearch * pSearch = NULL;
204  DWORD dwErrCode = ERROR_SUCCESS;
205 
206  // Check parameters
207  if((hs = TCascStorage::IsValid(hStorage)) == NULL)
208  dwErrCode = ERROR_INVALID_HANDLE;
209  if(pFindData == NULL)
210  dwErrCode = ERROR_INVALID_PARAMETER;
211 
212  // Supply default mask, if needed
213  if(szMask == NULL || szMask[0] == 0)
214  szMask = "*";
215 
216  // Init the search structure and search handle
217  if(dwErrCode == ERROR_SUCCESS)
218  {
219  // Allocate the search handle
220  pSearch = new TCascSearch(hs, szListFile, szMask);
221  if(pSearch == NULL)
222  dwErrCode = ERROR_NOT_ENOUGH_MEMORY;
223  }
224 
225  // Perform search
226  if(dwErrCode == ERROR_SUCCESS)
227  {
228  if(!DoStorageSearch(pSearch, pFindData))
229  dwErrCode = ERROR_NO_MORE_FILES;
230  }
231 
232  if(dwErrCode != ERROR_SUCCESS)
233  {
234  delete pSearch;
235  pSearch = (TCascSearch *)INVALID_HANDLE_VALUE;
236  }
237 
238  return (HANDLE)pSearch;
239 }
static bool DoStorageSearch(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:154
void * HANDLE
Definition: CascPort.h:168
Definition: CascCommon.h:380
#define ERROR_NOT_ENOUGH_MEMORY
Definition: CascPort.h:235
Definition: CascCommon.h:257
static TCascStorage * IsValid(HANDLE hStorage)
Definition: CascCommon.h:265
#define ERROR_NO_MORE_FILES
Definition: CascPort.h:242
#define ERROR_INVALID_HANDLE
Definition: CascPort.h:234
unsigned int DWORD
Definition: CascPort.h:162
#define INVALID_HANDLE_VALUE
Definition: CascPort.h:194
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:237
#define ERROR_SUCCESS
Definition: CascPort.h:230
+ Here is the call graph for this function:

◆ CascFindNextFile()

bool WINAPI CascFindNextFile ( HANDLE  hFind,
PCASC_FIND_DATA  pFindData 
)
244 {
245  TCascSearch * pSearch;
246 
247  pSearch = TCascSearch::IsValid(hFind);
248  if(pSearch == NULL || pFindData == NULL)
249  {
251  return false;
252  }
253 
254  // Perform search
255  return DoStorageSearch(pSearch, pFindData);
256 }
static bool DoStorageSearch(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:154
void SetCascError(DWORD dwErrCode)
Definition: Common.cpp:91
Definition: CascCommon.h:380
static TCascSearch * IsValid(HANDLE hFind)
Definition: CascCommon.h:411
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:237
+ Here is the call graph for this function:

◆ CopyCKeyEntryToFindData()

static bool CopyCKeyEntryToFindData ( PCASC_FIND_DATA  pFindData,
PCASC_CKEY_ENTRY  pCKeyEntry 
)
static
66 {
67  ULONGLONG ContentSize = 0;
68  ULONGLONG EncodedSize = 0;
69 
70  // Supply both keys
71  CopyMemory16(pFindData->CKey, pCKeyEntry->CKey);
72  CopyMemory16(pFindData->EKey, pCKeyEntry->EKey);
73 
74  // Supply the tag mask
75  pFindData->TagBitMask = pCKeyEntry->TagBitMask;
76 
77  // Supply the plain name. Only do that if the found name is not a CKey/EKey
78  if(pFindData->szFileName[0] != 0)
79  pFindData->szPlainName = (char *)GetPlainFileName(pFindData->szFileName);
80 
81  // If we retrieved the file size directly from the root provider, use it
82  // Otherwise, supply EncodedSize or ContentSize, whichever is available (but ContentSize > EncodedSize)
83  pFindData->dwSpanCount = GetFileSpanInfo(pCKeyEntry, &ContentSize, &EncodedSize);
84  if(pFindData->FileSize == CASC_INVALID_SIZE64)
85  {
86  if(ContentSize != CASC_INVALID_SIZE64)
87  pFindData->FileSize = ContentSize;
88  else
89  pFindData->FileSize = EncodedSize;
90  }
91 
92  // Set flag indicating that the file is locally available
93  pFindData->bFileAvailable = (pCKeyEntry->Flags & CASC_CE_FILE_IS_LOCAL);
94 
95  // Supply a fake file name, if there is none supplied by the root handler
96  if(pFindData->szFileName[0] == 0)
97  SupplyFakeFileName(pFindData, pCKeyEntry);
98  return true;
99 }
DWORD GetFileSpanInfo(PCASC_CKEY_ENTRY pCKeyEntry, PULONGLONG PtrContentSize, PULONGLONG PtrEncodedSize=NULL)
Definition: CascFiles.cpp:1318
unsigned long long ULONGLONG
Definition: CascPort.h:166
DWORD bFileAvailable
Definition: CascLib.h:229
BYTE CKey[MD5_HASH_SIZE]
Definition: Common.h:109
ULONGLONG TagBitMask
Definition: CascLib.h:208
char * szPlainName
Definition: CascLib.h:214
BYTE CKey[MD5_HASH_SIZE]
Definition: CascLib.h:202
BYTE EKey[MD5_HASH_SIZE]
Definition: CascLib.h:205
BYTE EKey[MD5_HASH_SIZE]
Definition: Common.h:110
ULONGLONG TagBitMask
Definition: Common.h:112
const XCHAR * GetPlainFileName(const XCHAR *szFileName)
Definition: Common.h:476
void CopyMemory16(void *Target, void *Source)
Definition: Common.h:284
static void SupplyFakeFileName(PCASC_FIND_DATA pFindData, PCASC_CKEY_ENTRY pCKeyEntry)
Definition: CascFindFile.cpp:36
char szFileName[MAX_PATH]
Definition: CascLib.h:199
#define CASC_CE_FILE_IS_LOCAL
Definition: Common.h:62
DWORD Flags
Definition: Common.h:115
DWORD dwSpanCount
Definition: CascLib.h:226
#define CASC_INVALID_SIZE64
Definition: CascLib.h:131
ULONGLONG FileSize
Definition: CascLib.h:211
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DoStorageSearch()

static bool DoStorageSearch ( TCascSearch pSearch,
PCASC_FIND_DATA  pFindData 
)
static
155 {
156  // State 0: No search done yet
157  if(pSearch->nSearchState == 0)
158  {
159  // Does the search specify listfile?
160  if(pSearch->szListFile != NULL)
161  pSearch->pCache = ListFile_OpenExternal(pSearch->szListFile);
162 
163  // Move the search phase to the listfile searching
164  pSearch->nSearchState = 1;
165  pSearch->nFileIndex = 0;
166  }
167 
168  // State 1: Searching the list file
169  if(pSearch->nSearchState == 1)
170  {
171  if(DoStorageSearch_RootFile(pSearch, pFindData))
172  return true;
173 
174  // Move to the nameless search state
175  pSearch->nSearchState = 2;
176  pSearch->nFileIndex = 0;
177  }
178 
179  // State 2: Searching the remaining entries by CKey
180  if(pSearch->nSearchState == 2 && (pSearch->szMask == NULL || !strcmp(pSearch->szMask, "*")))
181  {
182  if(DoStorageSearch_CKey(pSearch, pFindData))
183  return true;
184 
185  // Move to the final search state
186  pSearch->nSearchState++;
187  pSearch->nFileIndex = 0;
188  }
189 
190  return false;
191 }
LPTSTR szListFile
Definition: CascCommon.h:424
char * szMask
Definition: CascCommon.h:426
static bool DoStorageSearch_RootFile(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:103
static bool DoStorageSearch_CKey(TCascSearch *pSearch, PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:127
void * ListFile_OpenExternal(LPCTSTR szListFile)
Definition: ListFile.cpp:124
void * pCache
Definition: CascCommon.h:425
DWORD nSearchState
Definition: CascCommon.h:430
size_t nFileIndex
Definition: CascCommon.h:429
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DoStorageSearch_CKey()

static bool DoStorageSearch_CKey ( TCascSearch pSearch,
PCASC_FIND_DATA  pFindData 
)
static
128 {
129  PCASC_CKEY_ENTRY pCKeyEntry;
130  TCascStorage * hs = pSearch->hs;
131  size_t nTotalItems = hs->CKeyArray.ItemCount();
132 
133  // Reset the find data structure
134  ResetFindData(pFindData);
135 
136  // Check for CKeys that haven't been found yet
137  while(pSearch->nFileIndex < nTotalItems)
138  {
139  // Locate the n-th CKey entry.
140  pCKeyEntry = (PCASC_CKEY_ENTRY)hs->CKeyArray.ItemAt(pSearch->nFileIndex++);
141  //BREAK_ON_XKEY3(pCKeyEntry->CKey, 0x2B, 0xfc, 0xe4);
142 
143  // Only report files that are unreferenced by the ROOT handler
144  if(pCKeyEntry->IsFile() && pCKeyEntry->RefCount == 0)
145  {
146  return CopyCKeyEntryToFindData(pFindData, pCKeyEntry);
147  }
148  }
149 
150  // Nameless search ended
151  return false;
152 }
TCascStorage * hs
Definition: CascCommon.h:423
Definition: CascCommon.h:257
Definition: Common.h:76
CASC_ARRAY CKeyArray
Definition: CascCommon.h:322
size_t ItemCount()
Definition: Array.h:136
CASC_CKEY_ENTRY * PCASC_CKEY_ENTRY
Definition: Common.h:120
bool IsFile()
Definition: Common.h:92
void * ItemAt(size_t ItemIndex)
Definition: Array.h:84
static void ResetFindData(PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:19
USHORT RefCount
Definition: Common.h:116
size_t nFileIndex
Definition: CascCommon.h:429
static bool CopyCKeyEntryToFindData(PCASC_FIND_DATA pFindData, PCASC_CKEY_ENTRY pCKeyEntry)
Definition: CascFindFile.cpp:65
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DoStorageSearch_RootFile()

static bool DoStorageSearch_RootFile ( TCascSearch pSearch,
PCASC_FIND_DATA  pFindData 
)
static
104 {
105  PCASC_CKEY_ENTRY pCKeyEntry;
106  TCascStorage * hs = pSearch->hs;
107 
108  // Reset the search structure
109  ResetFindData(pFindData);
110 
111  // Enumerate over all files
112  for(;;)
113  {
114  // Attempt to find (the next) file from the root handler
115  pCKeyEntry = hs->pRootHandler->Search(pSearch, pFindData);
116  if(pCKeyEntry == NULL)
117  return false;
118 
119  // The entry is expected to be referenced by the root directory
120  assert(pCKeyEntry->RefCount != 0);
121 
122  // Copy the CKey entry to the find data and return it
123  return CopyCKeyEntryToFindData(pFindData, pCKeyEntry);
124  }
125 }
#define assert(e)
Definition: assert.h:9
TCascStorage * hs
Definition: CascCommon.h:423
Definition: CascCommon.h:257
Definition: Common.h:76
TRootHandler * pRootHandler
Definition: CascCommon.h:320
virtual PCASC_CKEY_ENTRY Search(struct TCascSearch *, struct _CASC_FIND_DATA *)
Definition: RootHandler.h:68
static void ResetFindData(PCASC_FIND_DATA pFindData)
Definition: CascFindFile.cpp:19
USHORT RefCount
Definition: Common.h:116
static bool CopyCKeyEntryToFindData(PCASC_FIND_DATA pFindData, PCASC_CKEY_ENTRY pCKeyEntry)
Definition: CascFindFile.cpp:65
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ResetFindData()

static void ResetFindData ( PCASC_FIND_DATA  pFindData)
static
20 {
21  // Reset the variables
22  ZeroMemory16(pFindData->CKey);
23  ZeroMemory16(pFindData->EKey);
24  pFindData->szFileName[0] = 0;
25  pFindData->szPlainName = pFindData->szFileName;
26  pFindData->TagBitMask = 0;
27  pFindData->FileSize = CASC_INVALID_SIZE64;
28  pFindData->dwFileDataId = CASC_INVALID_ID;
29  pFindData->dwLocaleFlags = CASC_INVALID_ID;
30  pFindData->dwContentFlags = CASC_INVALID_ID;
31  pFindData->dwSpanCount = 1;
32  pFindData->NameType = CascNameFull;
33  pFindData->bFileAvailable = false;
34 }
DWORD dwFileDataId
Definition: CascLib.h:217
DWORD bFileAvailable
Definition: CascLib.h:229
ULONGLONG TagBitMask
Definition: CascLib.h:208
char * szPlainName
Definition: CascLib.h:214
BYTE CKey[MD5_HASH_SIZE]
Definition: CascLib.h:202
BYTE EKey[MD5_HASH_SIZE]
Definition: CascLib.h:205
#define CASC_INVALID_ID
Definition: CascLib.h:129
Definition: CascLib.h:188
void ZeroMemory16(void *Buffer)
Definition: Common.h:273
char szFileName[MAX_PATH]
Definition: CascLib.h:199
CASC_NAME_TYPE NameType
Definition: CascLib.h:233
DWORD dwLocaleFlags
Definition: CascLib.h:220
DWORD dwContentFlags
Definition: CascLib.h:223
DWORD dwSpanCount
Definition: CascLib.h:226
#define CASC_INVALID_SIZE64
Definition: CascLib.h:131
ULONGLONG FileSize
Definition: CascLib.h:211
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SupplyFakeFileName()

static void SupplyFakeFileName ( PCASC_FIND_DATA  pFindData,
PCASC_CKEY_ENTRY  pCKeyEntry 
)
static
37 {
38  // If there is a file data ID, create fake file name
39  if(pFindData->dwFileDataId != CASC_INVALID_ID)
40  {
41  CascStrPrintf(pFindData->szFileName, _countof(pFindData->szFileName), "FILE%08X.dat", pFindData->dwFileDataId);
42  pFindData->NameType = CascNameDataId;
43  return;
44  }
45 
46  // If there is a CKey, convert the CKey to file name
47  if(pCKeyEntry->Flags & CASC_CE_HAS_CKEY)
48  {
49  StringFromBinary(pFindData->CKey, MD5_HASH_SIZE, pFindData->szFileName);
50  pFindData->NameType = CascNameCKey;
51  return;
52  }
53 
54  // EKey should be always present
55  if(pCKeyEntry->Flags & CASC_CE_HAS_EKEY)
56  {
57  StringFromBinary(pFindData->EKey, MD5_HASH_SIZE, pFindData->szFileName);
58  pFindData->NameType = CascNameEKey;
59  return;
60  }
61 
62  assert(false);
63 }
DWORD dwFileDataId
Definition: CascLib.h:217
#define assert(e)
Definition: assert.h:9
#define CASC_CE_HAS_CKEY
Definition: Common.h:63
BYTE CKey[MD5_HASH_SIZE]
Definition: CascLib.h:202
#define _countof(x)
Definition: CascPort.h:273
BYTE EKey[MD5_HASH_SIZE]
Definition: CascLib.h:205
#define CASC_INVALID_ID
Definition: CascLib.h:129
Definition: CascLib.h:191
#define MD5_HASH_SIZE
Definition: CascLib.h:121
Definition: CascLib.h:189
Definition: CascLib.h:190
#define CASC_CE_HAS_EKEY
Definition: Common.h:64
char szFileName[MAX_PATH]
Definition: CascLib.h:199
CASC_NAME_TYPE NameType
Definition: CascLib.h:233
DWORD Flags
Definition: Common.h:115
size_t CascStrPrintf(char *buffer, size_t nCount, const char *format,...)
Definition: Common.cpp:296
xchar * StringFromBinary(LPBYTE pbBinary, size_t cbBinary, xchar *szBuffer)
Definition: Common.h:419
+ Here is the call graph for this function:
+ Here is the caller graph for this function: