1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*****************************************************************************/
/* CascFindFile.cpp Copyright (c) Ladislav Zezula 2014 */
/*---------------------------------------------------------------------------*/
/* System-dependent directory functions for CascLib */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 10.05.14 1.00 Lad The first version of CascFindFile.cpp */
/*****************************************************************************/
#define __CASCLIB_SELF__
#include "CascLib.h"
#include "CascCommon.h"
//-----------------------------------------------------------------------------
// Local functions
// Reset the search structure. Called before each search
static void ResetFindData(PCASC_FIND_DATA pFindData)
{
// Reset the variables
ZeroMemory16(pFindData->CKey);
ZeroMemory16(pFindData->EKey);
pFindData->szFileName[0] = 0;
pFindData->szPlainName = pFindData->szFileName;
pFindData->TagBitMask = 0;
pFindData->dwFileDataId = CASC_INVALID_ID;
pFindData->dwFileSize = CASC_INVALID_SIZE;
pFindData->dwLocaleFlags = CASC_INVALID_ID;
pFindData->dwContentFlags = CASC_INVALID_ID;
pFindData->NameType = CascNameFull;
pFindData->bFileAvailable = false;
pFindData->bCanOpenByName = false;
pFindData->bCanOpenByDataId = false;
pFindData->bCanOpenByCKey = false;
pFindData->bCanOpenByEKey = false;
}
static void SupplyFakeFileName(PCASC_FIND_DATA pFindData)
{
// If the file can be open by file data id, create fake file name
if(pFindData->bCanOpenByDataId)
{
CascStrPrintf(pFindData->szFileName, _countof(pFindData->szFileName), "FILE%08X.dat", pFindData->dwFileDataId);
pFindData->NameType = CascNameDataId;
return;
}
// If the file can be open by CKey, convert the CKey to file name
if(pFindData->bCanOpenByCKey)
{
StringFromBinary(pFindData->CKey, MD5_HASH_SIZE, pFindData->szFileName);
pFindData->NameType = CascNameCKey;
return;
}
// CKey should be always present
StringFromBinary(pFindData->EKey, MD5_HASH_SIZE, pFindData->szFileName);
pFindData->NameType = CascNameEKey;
assert(pFindData->bCanOpenByEKey != false);
}
static bool CopyCKeyEntryToFindData(PCASC_FIND_DATA pFindData, PCASC_CKEY_ENTRY pCKeyEntry)
{
// Supply both keys
CopyMemory16(pFindData->CKey, pCKeyEntry->CKey);
CopyMemory16(pFindData->EKey, pCKeyEntry->EKey);
pFindData->bCanOpenByCKey = (pCKeyEntry->Flags & CASC_CE_HAS_CKEY) ? true : false;
pFindData->bCanOpenByEKey = (pCKeyEntry->Flags & CASC_CE_HAS_EKEY) ? true : false;
// Supply the tag mask
pFindData->TagBitMask = pCKeyEntry->TagBitMask;
// Supply the plain name. Only do that if the found name is not a CKey/EKey
if(pFindData->szFileName[0] != 0)
pFindData->szPlainName = (char *)GetPlainFileName(pFindData->szFileName);
// If we retrieved the file size directly from the root provider, use it
// Otherwise, supply EncodedSize or ContentSize, whichever is available (but ContentSize > EncodedSize)
if(pFindData->dwFileSize == CASC_INVALID_SIZE)
pFindData->dwFileSize = pCKeyEntry->ContentSize;
// Set flag indicating that the file is locally available
pFindData->bFileAvailable = (pCKeyEntry->Flags & CASC_CE_FILE_IS_LOCAL);
// Supply a fake file name, if there is none supplied by the root handler
if(pFindData->szFileName[0] == 0)
SupplyFakeFileName(pFindData);
return true;
}
// Perform searching using root-specific provider.
// The provider may need the listfile
static bool DoStorageSearch_RootFile(TCascSearch * pSearch, PCASC_FIND_DATA pFindData)
{
PCASC_CKEY_ENTRY pCKeyEntry;
TCascStorage * hs = pSearch->hs;
// Reset the search structure
ResetFindData(pFindData);
// Enumerate over all files
for(;;)
{
// Attempt to find (the next) file from the root handler
pCKeyEntry = hs->pRootHandler->Search(pSearch, pFindData);
if(pCKeyEntry == NULL)
return false;
// The entry is expected to be referenced by the root directory
assert(pCKeyEntry->RefCount != 0);
// Copy the CKey entry to the find data and return it
return CopyCKeyEntryToFindData(pFindData, pCKeyEntry);
}
}
static bool DoStorageSearch_CKey(TCascSearch * pSearch, PCASC_FIND_DATA pFindData)
{
PCASC_CKEY_ENTRY pCKeyEntry;
TCascStorage * hs = pSearch->hs;
size_t nTotalItems = hs->CKeyArray.ItemCount();
// Reset the find data structure
ResetFindData(pFindData);
// Check for CKeys that haven't been found yet
while(pSearch->nFileIndex < nTotalItems)
{
// Locate the n-th CKey entry. If this entry is not referenced by the root handler, we include it in the search result
pCKeyEntry = (PCASC_CKEY_ENTRY)hs->CKeyArray.ItemAt(pSearch->nFileIndex++);
if((pCKeyEntry->Flags & CASC_CE_FOLDER_ENTRY) == 0 && pCKeyEntry->RefCount == 0)
{
return CopyCKeyEntryToFindData(pFindData, pCKeyEntry);
}
}
// Nameless search ended
return false;
}
static bool DoStorageSearch(TCascSearch * pSearch, PCASC_FIND_DATA pFindData)
{
// State 0: No search done yet
if(pSearch->nSearchState == 0)
{
// Does the search specify listfile?
if(pSearch->szListFile != NULL)
pSearch->pCache = ListFile_OpenExternal(pSearch->szListFile);
// Move the search phase to the listfile searching
pSearch->nSearchState = 1;
pSearch->nFileIndex = 0;
}
// State 1: Searching the list file
if(pSearch->nSearchState == 1)
{
if(DoStorageSearch_RootFile(pSearch, pFindData))
return true;
// Move to the nameless search state
pSearch->nSearchState = 2;
pSearch->nFileIndex = 0;
}
// State 2: Searching the remaining entries by CKey
if(pSearch->nSearchState == 2 && (pSearch->szMask == NULL || !strcmp(pSearch->szMask, "*")))
{
if(DoStorageSearch_CKey(pSearch, pFindData))
return true;
// Move to the final search state
pSearch->nSearchState++;
pSearch->nFileIndex = 0;
}
return false;
}
//-----------------------------------------------------------------------------
// Public functions
HANDLE WINAPI CascFindFirstFile(
HANDLE hStorage,
LPCSTR szMask,
PCASC_FIND_DATA pFindData,
LPCTSTR szListFile)
{
TCascStorage * hs;
TCascSearch * pSearch = NULL;
int nError = ERROR_SUCCESS;
// Check parameters
if((hs = TCascStorage::IsValid(hStorage)) == NULL)
nError = ERROR_INVALID_HANDLE;
if(szMask == NULL || pFindData == NULL)
nError = ERROR_INVALID_PARAMETER;
// Init the search structure and search handle
if(nError == ERROR_SUCCESS)
{
// Allocate the search handle
pSearch = new TCascSearch(hs, szListFile, szMask);
if(pSearch == NULL)
nError = ERROR_NOT_ENOUGH_MEMORY;
}
// Perform search
if(nError == ERROR_SUCCESS)
{
if(!DoStorageSearch(pSearch, pFindData))
nError = ERROR_NO_MORE_FILES;
}
if(nError != ERROR_SUCCESS)
{
delete pSearch;
pSearch = (TCascSearch *)INVALID_HANDLE_VALUE;
}
return (HANDLE)pSearch;
}
bool WINAPI CascFindNextFile(
HANDLE hFind,
PCASC_FIND_DATA pFindData)
{
TCascSearch * pSearch;
pSearch = TCascSearch::IsValid(hFind);
if(pSearch == NULL || pFindData == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
// Perform search
return DoStorageSearch(pSearch, pFindData);
}
bool WINAPI CascFindClose(HANDLE hFind)
{
TCascSearch * pSearch;
pSearch = TCascSearch::IsValid(hFind);
if(pSearch == NULL)
{
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
delete pSearch;
return true;
}
|