| 303 | // Public functions |
| 304 | |
| 305 | bool WINAPI CascOpenFile(HANDLE hStorage, const void * pvFileName, DWORD dwLocaleFlags, DWORD dwOpenFlags, HANDLE * PtrFileHandle) |
| 306 | { |
| 307 | PCASC_CKEY_ENTRY pCKeyEntry = NULL; |
| 308 | TCascStorage * hs; |
| 309 | const char * szFileName; |
| 310 | DWORD FileDataId = CASC_INVALID_ID; |
| 311 | BYTE CKeyEKeyBuffer[MD5_HASH_SIZE]; |
| 312 | DWORD dwErrCode = ERROR_SUCCESS; |
| 313 | |
| 314 | // This parameter is not used |
| 315 | CASCLIB_UNUSED(dwLocaleFlags); |
| 316 | |
| 317 | // Validate the storage handle |
| 318 | hs = TCascStorage::IsValid(hStorage); |
| 319 | if(hs == NULL) |
| 320 | { |
| 321 | SetCascError(ERROR_INVALID_HANDLE); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | // Validate the other parameters |
| 326 | if(PtrFileHandle == NULL) |
| 327 | { |
| 328 | SetCascError(ERROR_INVALID_PARAMETER); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | // Retrieve the CKey/EKey from the file name in different modes |
| 333 | switch(dwOpenFlags & CASC_OPEN_TYPE_MASK) |
| 334 | { |
| 335 | case CASC_OPEN_BY_NAME: |
| 336 | |
| 337 | // The 'pvFileName' must be zero terminated ANSI file name |
| 338 | szFileName = (const char *)pvFileName; |
| 339 | if(szFileName == NULL || szFileName[0] == 0) |
| 340 | { |
| 341 | SetCascError(ERROR_INVALID_PARAMETER); |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | // The first chance: Try to find the file by name (using the root handler) |
| 346 | pCKeyEntry = hs->pRootHandler->GetFile(hs, szFileName); |
| 347 | if(pCKeyEntry != NULL) |
| 348 | break; |
| 349 | |
| 350 | // Second chance: If the file name is actually a file data id, we convert it to file data ID |
| 351 | if(IsFileDataIdName(szFileName, FileDataId)) |
| 352 | { |
| 353 | pCKeyEntry = hs->pRootHandler->GetFile(hs, FileDataId); |
| 354 | if(pCKeyEntry != NULL) |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | // Third chance: If the file name is a string representation of CKey/EKey, we try to query for CKey |
| 359 | if(IsFileCKeyEKeyName(szFileName, CKeyEKeyBuffer)) |
| 360 | { |
| 361 | pCKeyEntry = FindCKeyEntry_CKey(hs, CKeyEKeyBuffer); |
| 362 | if(pCKeyEntry != NULL) |