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

Macros

#define __CASCLIB_SELF__
 

Functions

TCascFileIsValidFileHandle (HANDLE hFile)
 
PCASC_INDEX_ENTRY FindIndexEntry (TCascStorage *hs, PQUERY_KEY pIndexKey)
 
PCASC_ENCODING_ENTRY FindEncodingEntry (TCascStorage *hs, PQUERY_KEY pEncodingKey, PDWORD PtrIndex)
 
static TCascFileCreateFileHandle (TCascStorage *hs, PCASC_INDEX_ENTRY pIndexEntry)
 
static bool OpenFileByIndexKey (TCascStorage *hs, PQUERY_KEY pIndexKey, DWORD dwFlags, TCascFile **ppCascFile)
 
static bool OpenFileByEncodingKey (TCascStorage *hs, PQUERY_KEY pEncodingKey, DWORD dwFlags, TCascFile **ppCascFile)
 
bool WINAPI CascOpenFileByIndexKey (HANDLE hStorage, PQUERY_KEY pIndexKey, DWORD dwFlags, HANDLE *phFile)
 
bool WINAPI CascOpenFileByEncodingKey (HANDLE hStorage, PQUERY_KEY pEncodingKey, DWORD dwFlags, HANDLE *phFile)
 
bool WINAPI CascOpenFile (HANDLE hStorage, const char *szFileName, DWORD dwLocale, DWORD dwFlags, HANDLE *phFile)
 
bool WINAPI CascCloseFile (HANDLE hFile)
 

Macro Definition Documentation

#define __CASCLIB_SELF__

Function Documentation

bool WINAPI CascCloseFile ( HANDLE  hFile)
274 {
275  TCascFile * hf;
276 
277  hf = IsValidFileHandle(hFile);
278  if(hf != NULL)
279  {
280  // Close (dereference) the archive handle
281  if(hf->hs != NULL)
282  CascCloseStorage((HANDLE)hf->hs);
283  hf->hs = NULL;
284 
285  // Free the file cache and frame array
286  if(hf->pbFileCache != NULL)
287  CASC_FREE(hf->pbFileCache);
288  if(hf->pFrames != NULL)
289  CASC_FREE(hf->pFrames);
290 
291  // Free the structure itself
292  hf->szClassName = NULL;
293  CASC_FREE(hf);
294  return true;
295  }
296 
298  return false;
299 }
const char * szClassName
Definition: CascCommon.h:231
void * HANDLE
Definition: CascPort.h:146
TCascFile * IsValidFileHandle(HANDLE hFile)
Definition: CascOpenFile.cpp:18
arena_t NULL
Definition: jemalloc_internal.h:624
LPBYTE pbFileCache
Definition: CascCommon.h:246
#define CASC_FREE(ptr)
Definition: CascCommon.h:303
#define ERROR_INVALID_HANDLE
Definition: CascPort.h:207
bool WINAPI CascCloseStorage(HANDLE hStorage)
Definition: CascOpenStorage.cpp:1133
void SetLastError(int nError)
Definition: Common.cpp:75
PCASC_FILE_FRAME pFrames
Definition: CascCommon.h:243
TCascStorage * hs
Definition: CascCommon.h:229
Definition: CascCommon.h:227

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool WINAPI CascOpenFile ( HANDLE  hStorage,
const char *  szFileName,
DWORD  dwLocale,
DWORD  dwFlags,
HANDLE phFile 
)
197 {
198  TCascStorage * hs;
199  QUERY_KEY EncodingKey;
200  LPBYTE pbEncodingKey;
201  BYTE KeyBuffer[MD5_HASH_SIZE];
202  int nError = ERROR_SUCCESS;
203 
204  CASCLIB_UNUSED(dwLocale);
205 
206  // Validate the storage handle
207  hs = IsValidStorageHandle(hStorage);
208  if(hs == NULL)
209  {
211  return false;
212  }
213 
214  // Validate the other parameters
215  if(szFileName == NULL || szFileName[0] == 0 || phFile == NULL)
216  {
218  return false;
219  }
220 
221  // If the user is opening the file via encoding key, skip the ROOT file processing
222  if((dwFlags & CASC_OPEN_BY_ENCODING_KEY) == 0)
223  {
224  // Let the root directory provider get us the encoding key
225  pbEncodingKey = RootHandler_GetKey(hs->pRootHandler, szFileName);
226  if(pbEncodingKey == NULL)
227  {
229  return false;
230  }
231 
232  // Setup the encoding key
233  EncodingKey.pbData = pbEncodingKey;
234  EncodingKey.cbData = MD5_HASH_SIZE;
235  }
236  else
237  {
238  // Check the length of the file name
239  if(strlen(szFileName) < MD5_STRING_SIZE)
240  {
242  return false;
243  }
244 
245  // Convert the file name to binary blob
246  EncodingKey.pbData = KeyBuffer;
247  EncodingKey.cbData = MD5_HASH_SIZE;
248  nError = ConvertStringToBinary(szFileName, MD5_STRING_SIZE, KeyBuffer);
249  }
250 
251  // Use the encoding key to find the file in the encoding table entry
252  if(nError == ERROR_SUCCESS)
253  {
254  if(!OpenFileByEncodingKey(hs, &EncodingKey, dwFlags, (TCascFile **)phFile))
255  {
256  assert(GetLastError() != ERROR_SUCCESS);
257  nError = GetLastError();
258  }
259  }
260 
261 #ifdef CASCLIB_TEST
262 // if(phFile[0] != NULL && pRootEntryMndx != NULL)
263 // {
264 // ((TCascFile *)(phFile[0]))->FileSize_RootEntry = pRootEntryMndx->FileSize;
265 // }
266 #endif
267 
268  if(nError != ERROR_SUCCESS)
269  SetLastError(nError);
270  return (nError == ERROR_SUCCESS);
271 }
TRootHandler * pRootHandler
Definition: CascCommon.h:223
int ConvertStringToBinary(const char *szString, size_t nMaxDigits, LPBYTE pbBinary)
Definition: Common.cpp:338
Definition: CascLib.h:138
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASCLIB_UNUSED(p)
Definition: CascCommon.h:75
BYTE * LPBYTE
Definition: CascPort.h:152
static bool OpenFileByEncodingKey(TCascStorage *hs, PQUERY_KEY pEncodingKey, DWORD dwFlags, TCascFile **ppCascFile)
Definition: CascOpenFile.cpp:110
DWORD cbData
Definition: CascLib.h:141
#define ERROR_INVALID_HANDLE
Definition: CascPort.h:207
LPBYTE RootHandler_GetKey(TRootHandler *pRootHandler, const char *szFileName)
Definition: RootHandler.cpp:44
#define MD5_HASH_SIZE
Definition: CascLib.h:105
void SetLastError(int nError)
Definition: Common.cpp:75
#define ERROR_FILE_NOT_FOUND
Definition: CascPort.h:205
Definition: CascCommon.h:186
LPBYTE pbData
Definition: CascLib.h:140
int GetLastError()
Definition: Common.cpp:70
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:210
unsigned char BYTE
Definition: CascPort.h:136
Definition: CascCommon.h:227
#define MD5_STRING_SIZE
Definition: CascLib.h:106
TCascStorage * IsValidStorageHandle(HANDLE hStorage)
Definition: CascOpenStorage.cpp:100
#define CASC_OPEN_BY_ENCODING_KEY
Definition: CascLib.h:42
#define ERROR_SUCCESS
Definition: CascPort.h:204

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool WINAPI CascOpenFileByEncodingKey ( HANDLE  hStorage,
PQUERY_KEY  pEncodingKey,
DWORD  dwFlags,
HANDLE phFile 
)
174 {
175  TCascStorage * hs;
176 
177  // Validate the storage handle
178  hs = IsValidStorageHandle(hStorage);
179  if(hs == NULL)
180  {
182  return false;
183  }
184 
185  // Validate the other parameters
186  if(pEncodingKey == NULL || pEncodingKey->pbData == NULL || pEncodingKey->cbData == 0 || phFile == NULL)
187  {
189  return false;
190  }
191 
192  // Use the internal function fo open the file
193  return OpenFileByEncodingKey(hs, pEncodingKey, dwFlags, (TCascFile **)phFile);
194 }
arena_t NULL
Definition: jemalloc_internal.h:624
static bool OpenFileByEncodingKey(TCascStorage *hs, PQUERY_KEY pEncodingKey, DWORD dwFlags, TCascFile **ppCascFile)
Definition: CascOpenFile.cpp:110
DWORD cbData
Definition: CascLib.h:141
#define ERROR_INVALID_HANDLE
Definition: CascPort.h:207
void SetLastError(int nError)
Definition: Common.cpp:75
Definition: CascCommon.h:186
LPBYTE pbData
Definition: CascLib.h:140
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:210
Definition: CascCommon.h:227
TCascStorage * IsValidStorageHandle(HANDLE hStorage)
Definition: CascOpenStorage.cpp:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool WINAPI CascOpenFileByIndexKey ( HANDLE  hStorage,
PQUERY_KEY  pIndexKey,
DWORD  dwFlags,
HANDLE phFile 
)
151 {
152  TCascStorage * hs;
153 
154  // Validate the storage handle
155  hs = IsValidStorageHandle(hStorage);
156  if(hs == NULL)
157  {
159  return false;
160  }
161 
162  // Validate the other parameters
163  if(pIndexKey == NULL || pIndexKey->pbData == NULL || pIndexKey->cbData == 0 || phFile == NULL)
164  {
166  return false;
167  }
168 
169  // Use the internal function to open the file
170  return OpenFileByIndexKey(hs, pIndexKey, dwFlags, (TCascFile **)phFile);
171 }
arena_t NULL
Definition: jemalloc_internal.h:624
DWORD cbData
Definition: CascLib.h:141
#define ERROR_INVALID_HANDLE
Definition: CascPort.h:207
void SetLastError(int nError)
Definition: Common.cpp:75
Definition: CascCommon.h:186
LPBYTE pbData
Definition: CascLib.h:140
static bool OpenFileByIndexKey(TCascStorage *hs, PQUERY_KEY pIndexKey, DWORD dwFlags, TCascFile **ppCascFile)
Definition: CascOpenFile.cpp:78
#define ERROR_INVALID_PARAMETER
Definition: CascPort.h:210
Definition: CascCommon.h:227
TCascStorage * IsValidStorageHandle(HANDLE hStorage)
Definition: CascOpenStorage.cpp:100

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static TCascFile* CreateFileHandle ( TCascStorage hs,
PCASC_INDEX_ENTRY  pIndexEntry 
)
static
46 {
47  ULONGLONG FileOffsMask = ((ULONGLONG)1 << hs->KeyMapping[0].SegmentBits) - 1;
48  ULONGLONG FileOffset = ConvertBytesToInteger_5(pIndexEntry->FileOffsetBE);
49  TCascFile * hf;
50 
51  // Allocate the CASC file structure
52  hf = (TCascFile *)CASC_ALLOC(TCascFile, 1);
53  if(hf != NULL)
54  {
55  // Initialize the structure
56  memset(hf, 0, sizeof(TCascFile));
57  hf->ArchiveIndex = (DWORD)(FileOffset >> hs->KeyMapping[0].SegmentBits);
58  hf->HeaderOffset = (DWORD)(FileOffset & FileOffsMask);
59  hf->szClassName = "TCascFile";
60 
61  // Copy the file size. Note that for all files except ENCODING,
62  // this is the compressed file size
64 
65  // For now, we set the file size to be equal to compressed size
66  // This is used when loading the ENCODING file, which does not
67  // have entry in the encoding table
68  hf->FileSize = hf->CompressedSize;
69 
70  // Increment the number of references to the archive
71  hs->dwRefCount++;
72  hf->hs = hs;
73  }
74 
75  return hf;
76 }
const char * szClassName
Definition: CascCommon.h:231
DWORD HeaderOffset
Definition: CascCommon.h:236
#define CASC_ALLOC(type, count)
Definition: CascCommon.h:302
unsigned long long ULONGLONG
Definition: CascPort.h:144
BYTE SegmentBits
Definition: CascCommon.h:118
CASC_MAPPING_TABLE KeyMapping[CASC_INDEX_COUNT]
Definition: CascCommon.h:216
arena_t NULL
Definition: jemalloc_internal.h:624
DWORD FileSize
Definition: CascCommon.h:240
DWORD ArchiveIndex
Definition: CascCommon.h:235
ULONGLONG ConvertBytesToInteger_5(LPBYTE ValueAsBytes)
Definition: CascCommon.cpp:56
TCascStorage * hs
Definition: CascCommon.h:229
unsigned int DWORD
Definition: CascPort.h:139
DWORD dwRefCount
Definition: CascCommon.h:195
BYTE FileOffsetBE[5]
Definition: CascCommon.h:105
DWORD CompressedSize
Definition: CascCommon.h:239
DWORD ConvertBytesToInteger_4_LE(LPBYTE ValueAsBytes)
Definition: CascCommon.cpp:43
Definition: CascCommon.h:227
BYTE FileSizeLE[4]
Definition: CascCommon.h:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

PCASC_ENCODING_ENTRY FindEncodingEntry ( TCascStorage hs,
PQUERY_KEY  pEncodingKey,
PDWORD  PtrIndex 
)
36 {
37  PCASC_ENCODING_ENTRY pEncodingEntry = NULL;
38 
39  if(hs->pEncodingMap != NULL)
40  pEncodingEntry = (PCASC_ENCODING_ENTRY)Map_FindObject(hs->pEncodingMap, pEncodingKey->pbData, PtrIndex);
41 
42  return pEncodingEntry;
43 }
void * Map_FindObject(PCASC_MAP pMap, void *pvKey, PDWORD PtrIndex)
Definition: Map.cpp:138
arena_t NULL
Definition: jemalloc_internal.h:624
struct _CASC_ENCODING_ENTRY * PCASC_ENCODING_ENTRY
LPBYTE pbData
Definition: CascLib.h:140
PCASC_MAP pEncodingMap
Definition: CascCommon.h:220
Definition: CascCommon.h:159

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

PCASC_INDEX_ENTRY FindIndexEntry ( TCascStorage hs,
PQUERY_KEY  pIndexKey 
)
26 {
27  PCASC_INDEX_ENTRY pIndexEntry = NULL;
28 
29  if(hs->pIndexEntryMap != NULL)
30  pIndexEntry = (PCASC_INDEX_ENTRY)Map_FindObject(hs->pIndexEntryMap, pIndexKey->pbData, NULL);
31 
32  return pIndexEntry;
33 }
void * Map_FindObject(PCASC_MAP pMap, void *pvKey, PDWORD PtrIndex)
Definition: Map.cpp:138
PCASC_MAP pIndexEntryMap
Definition: CascCommon.h:217
arena_t NULL
Definition: jemalloc_internal.h:624
struct _CASC_INDEX_ENTRY * PCASC_INDEX_ENTRY
LPBYTE pbData
Definition: CascLib.h:140
Definition: CascCommon.h:102

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

TCascFile* IsValidFileHandle ( HANDLE  hFile)
19 {
20  TCascFile * hf = (TCascFile *)hFile;
21 
22  return (hf != NULL && hf->hs != NULL && hf->szClassName != NULL && !strcmp(hf->szClassName, "TCascFile")) ? hf : NULL;
23 }
const char * szClassName
Definition: CascCommon.h:231
arena_t NULL
Definition: jemalloc_internal.h:624
TCascStorage * hs
Definition: CascCommon.h:229
Definition: CascCommon.h:227

+ Here is the caller graph for this function:

static bool OpenFileByEncodingKey ( TCascStorage hs,
PQUERY_KEY  pEncodingKey,
DWORD  dwFlags,
TCascFile **  ppCascFile 
)
static
111 {
112  PCASC_ENCODING_ENTRY pEncodingEntry;
113  QUERY_KEY IndexKey;
114 
115  // Find the encoding entry
116  pEncodingEntry = FindEncodingEntry(hs, pEncodingKey, NULL);
117  if(pEncodingEntry == NULL)
118  {
120  return false;
121  }
122 
123  // Prepare the file index and open the file by index
124  // Note: We don't know what to do if there is more than just one index key
125  // We always take the first file present. Is that correct?
126  IndexKey.pbData = GET_INDEX_KEY(pEncodingEntry);
127  IndexKey.cbData = MD5_HASH_SIZE;
128  if(OpenFileByIndexKey(hs, &IndexKey, dwFlags, ppCascFile))
129  {
130  // Check if the file handle was created
131  if(ppCascFile[0] != NULL)
132  {
133  // Fill-in the file size. For all files except ENCODING,
134  // this overrides the value stored in the index entry.
135  ppCascFile[0]->FileSize = ConvertBytesToInteger_4(pEncodingEntry->FileSizeBE);
136 
137 #ifdef CASCLIB_TEST
138  ppCascFile[0]->FileSize_EncEntry = ConvertBytesToInteger_4(pEncodingEntry->FileSizeBE);
139 #endif
140  return true;
141  }
142  }
143 
144  return false;
145 }
BYTE FileSizeBE[4]
Definition: CascCommon.h:162
PCASC_ENCODING_ENTRY FindEncodingEntry(TCascStorage *hs, PQUERY_KEY pEncodingKey, PDWORD PtrIndex)
Definition: CascOpenFile.cpp:35
Definition: CascLib.h:138
arena_t NULL
Definition: jemalloc_internal.h:624
DWORD FileSize
Definition: CascCommon.h:240
DWORD cbData
Definition: CascLib.h:141
#define MD5_HASH_SIZE
Definition: CascLib.h:105
void SetLastError(int nError)
Definition: Common.cpp:75
#define ERROR_FILE_NOT_FOUND
Definition: CascPort.h:205
LPBYTE pbData
Definition: CascLib.h:140
static bool OpenFileByIndexKey(TCascStorage *hs, PQUERY_KEY pIndexKey, DWORD dwFlags, TCascFile **ppCascFile)
Definition: CascOpenFile.cpp:78
Definition: CascCommon.h:159
#define GET_INDEX_KEY(pEncodingEntry)
Definition: CascCommon.h:180
DWORD ConvertBytesToInteger_4(LPBYTE ValueAsBytes)
Definition: CascCommon.cpp:31

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool OpenFileByIndexKey ( TCascStorage hs,
PQUERY_KEY  pIndexKey,
DWORD  dwFlags,
TCascFile **  ppCascFile 
)
static
79 {
80  PCASC_INDEX_ENTRY pIndexEntry;
81  int nError = ERROR_SUCCESS;
82 
83  CASCLIB_UNUSED(dwFlags);
84 
85  // Find the key entry in the array of file keys
86  pIndexEntry = FindIndexEntry(hs, pIndexKey);
87  if(pIndexEntry == NULL)
88  nError = ERROR_FILE_NOT_FOUND;
89 
90  // Create the file handle structure
91  if(nError == ERROR_SUCCESS)
92  {
93  ppCascFile[0] = CreateFileHandle(hs, pIndexEntry);
94  if(ppCascFile[0] == NULL)
95  nError = ERROR_FILE_NOT_FOUND;
96  }
97 
98 #ifdef CASCLIB_TEST
99  if(nError == ERROR_SUCCESS && ppCascFile[0] != NULL)
100  {
101  ppCascFile[0]->FileSize_IdxEntry = ConvertBytesToInteger_4_LE(pIndexEntry->FileSizeLE);
102  }
103 #endif
104 
105  if(nError != ERROR_SUCCESS)
106  SetLastError(nError);
107  return (nError == ERROR_SUCCESS);
108 }
arena_t NULL
Definition: jemalloc_internal.h:624
#define CASCLIB_UNUSED(p)
Definition: CascCommon.h:75
static TCascFile * CreateFileHandle(TCascStorage *hs, PCASC_INDEX_ENTRY pIndexEntry)
Definition: CascOpenFile.cpp:45
void SetLastError(int nError)
Definition: Common.cpp:75
#define ERROR_FILE_NOT_FOUND
Definition: CascPort.h:205
Definition: CascCommon.h:102
DWORD ConvertBytesToInteger_4_LE(LPBYTE ValueAsBytes)
Definition: CascCommon.cpp:43
PCASC_INDEX_ENTRY FindIndexEntry(TCascStorage *hs, PQUERY_KEY pIndexKey)
Definition: CascOpenFile.cpp:25
BYTE FileSizeLE[4]
Definition: CascCommon.h:106
#define ERROR_SUCCESS
Definition: CascPort.h:204

+ Here is the call graph for this function:

+ Here is the caller graph for this function: