aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/CascOpenFile.cpp
blob: 8ff9739d6e30d18a313a8e177e7e9c02f98c2876 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*****************************************************************************/
/* CascOpenFile.cpp                       Copyright (c) Ladislav Zezula 2014 */
/*---------------------------------------------------------------------------*/
/* System-dependent directory functions for CascLib                          */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 01.05.14  1.00  Lad  The first version of CascOpenFile.cpp                */
/*****************************************************************************/

#define __CASCLIB_SELF__
#include "CascLib.h"
#include "CascCommon.h"

//-----------------------------------------------------------------------------
// TCascFile class functions

TCascFile::TCascFile(TCascStorage * ahs, PCASC_CKEY_ENTRY apCKeyEntry)
{
    // Reference the storage handle
    if((hs = ahs) != NULL)
        hs->AddRef();
    ClassName = CASC_MAGIC_FILE;

    FilePointer = 0;
    pCKeyEntry = apCKeyEntry;
    SpanCount = (pCKeyEntry->SpanCount != 0) ? pCKeyEntry->SpanCount : 1;
    bVerifyIntegrity = false;
    bDownloadFileIf = false;
    bCloseFileStream = false;
    bFreeCKeyEntries = false;

    // Allocate the array of file spans
    if((pFileSpan = CASC_ALLOC_ZERO<CASC_FILE_SPAN>(SpanCount)) != NULL)
    {
        InitFileSpans(pFileSpan, SpanCount);
        InitCacheStrategy();
    }
}

TCascFile::~TCascFile()
{
    // Free all stuff related to file spans
    if (pFileSpan != NULL)
    {
        PCASC_FILE_SPAN pSpanPtr = pFileSpan;

        for(DWORD i = 0; i < SpanCount; i++, pSpanPtr++)
        {
            // Close the span file stream if this is a local file
            if(bCloseFileStream)
                FileStream_Close(pSpanPtr->pStream);
            pSpanPtr->pStream = NULL;

            // Free the span frames
            CASC_FREE(pSpanPtr->pFrames);
        }

        CASC_FREE(pFileSpan);
    }

    // Free the CKey entries, if needed
    if(pCKeyEntry && bFreeCKeyEntries)
        delete [] pCKeyEntry;
    pCKeyEntry = NULL;

    // Free the file cache
    CASC_FREE(pbFileCache);

    // Close (dereference) the archive handle
    if(hs != NULL)
        hs = hs->Release();
    ClassName = 0;
}

DWORD TCascFile::OpenFileSpans(LPCTSTR szSpanList)
{
    TFileStream * pStream;
    ULONGLONG FileSize = 0;
    DWORD dwErrCode = ERROR_SUCCESS;

    for(DWORD i = 0; i < SpanCount; i++)
    {
        // Open the file span
        pFileSpan[i].pStream = pStream = FileStream_OpenFile(szSpanList, BASE_PROVIDER_FILE | STREAM_PROVIDER_FLAT);
        if(pFileSpan[i].pStream == NULL)
        {
            dwErrCode = GetLastError();
            break;
        }

        // If succeeded, we assign the span to the 
        FileStream_GetSize(pStream, &FileSize);
        if((FileSize >> 0x1E) != 0)
        {
            dwErrCode = ERROR_NOT_SUPPORTED;
            break;
        }

        pCKeyEntry[i].EncodedSize = (DWORD)FileSize;
    }

    // Free the so-far-opened files
    if(dwErrCode != ERROR_SUCCESS)
    {
        for(DWORD i = 0; i < SpanCount; i++)
        {
            if(pFileSpan[i].pStream != NULL)
                FileStream_Close(pFileSpan[i].pStream);
            pFileSpan[i].pStream = NULL;
        }
    }

    return dwErrCode;
}

void TCascFile::InitFileSpans(PCASC_FILE_SPAN pSpans, DWORD dwSpanCount)
{
    ULONGLONG FileOffsetBits = 30;
    ULONGLONG FileOffsetMask = 0;
    ULONGLONG FileOffset = 0;

    // Initialize the file sizes. Note that if any of the spans has invalid size,
    // the entire file size will be set to CASC_INVALID_SIZE64.
    GetFileSpanInfo(pCKeyEntry, &ContentSize, &EncodedSize);

    // Resolve the file offset bits and file offset mask
    if(hs != NULL)
        FileOffsetBits = hs->FileOffsetBits;
    FileOffsetMask = ((ULONGLONG)1 << FileOffsetBits) - 1;

    // Add all span sizes
    for(DWORD i = 0; i < dwSpanCount; i++, pSpans++)
    {
        // Put the archive index and archive offset
        pSpans->ArchiveIndex = (DWORD)(pCKeyEntry[i].StorageOffset >> FileOffsetBits);
        pSpans->ArchiveOffs = (DWORD)(pCKeyEntry[i].StorageOffset & FileOffsetMask);

        // Add to the total encoded size
        if(ContentSize != CASC_INVALID_SIZE64)
        {
            pSpans->StartOffset = FileOffset;
            FileOffset = FileOffset + pCKeyEntry[i].ContentSize;
            pSpans->EndOffset = FileOffset;
        }
    }
}

void TCascFile::InitCacheStrategy()
{
    CacheStrategy = CascCacheLastFrame;
    FileCacheStart = FileCacheEnd = 0;
    pbFileCache = NULL;
}

//-----------------------------------------------------------------------------
// Local functions

static size_t GetSpanFileCount(LPTSTR szSpanList)
{
    LPTSTR szSpanPtr = szSpanList;
    size_t nSpanCount = 1;

    while(szSpanPtr[0] != 0)
    {
        // End of a file?
        if(szSpanPtr[0] == ';' && szSpanPtr[1] != 0)
        {
            szSpanPtr[0] = 0;
            nSpanCount++;
        }

        szSpanPtr++;
    }

    // Place an additional zero to make the list terminated by double EOS
    szSpanPtr[1] = 0;
    return nSpanCount;
}

PCASC_CKEY_ENTRY FindCKeyEntry_CKey(TCascStorage * hs, LPBYTE pbCKey, PDWORD PtrIndex)
{
    return (PCASC_CKEY_ENTRY)hs->CKeyMap.FindObject(pbCKey, PtrIndex);
}

PCASC_CKEY_ENTRY FindCKeyEntry_EKey(TCascStorage * hs, LPBYTE pbEKey, PDWORD PtrIndex)
{
    return (PCASC_CKEY_ENTRY)hs->EKeyMap.FindObject(pbEKey, PtrIndex);
}

bool OpenFileByCKeyEntry(TCascStorage * hs, PCASC_CKEY_ENTRY pCKeyEntry, DWORD dwOpenFlags, HANDLE * PtrFileHandle)
{
    TCascFile * hf = NULL;
    DWORD dwErrCode = ERROR_FILE_NOT_FOUND;

    // If the CKey entry is NULL, we consider the file non-existant
    if(pCKeyEntry != NULL)
    {
        // Create the file handle structure
        if((hf = new TCascFile(hs, pCKeyEntry)) != NULL)
        {
            hf->bVerifyIntegrity   = (dwOpenFlags & CASC_STRICT_DATA_CHECK)  ? true : false;
            hf->bDownloadFileIf    = (hs->dwFeatures & CASC_FEATURE_ONLINE)  ? true : false;
            hf->bOvercomeEncrypted = (dwOpenFlags & CASC_OVERCOME_ENCRYPTED) ? true : false;
            dwErrCode = ERROR_SUCCESS;
        }
        else
        {
            dwErrCode = ERROR_NOT_ENOUGH_MEMORY;
        }
    }

    // Give the output parameter, no matter what
    PtrFileHandle[0] = (HANDLE)hf;

    // Handle last error
    if(dwErrCode != ERROR_SUCCESS)
        SetLastError(dwErrCode);
    return (dwErrCode == ERROR_SUCCESS);
}

bool OpenLocalFile(LPCTSTR szFileName, DWORD dwOpenFlags, HANDLE * PtrFileHandle)
{
    PCASC_CKEY_ENTRY pCKeyEntry;
    TCascFile * hf = NULL;
    LPTSTR szSpanList;
    size_t nSpanCount;
    DWORD dwErrCode = ERROR_NOT_ENOUGH_MEMORY;

    // Create a copy of the file name. It is actually a file name list,
    // separated by comma (for supporting multi-span files)
    if((szSpanList = CascNewStr(szFileName, 1)) != NULL)
    {
        // Calculate the span count
        if((nSpanCount = GetSpanFileCount(szSpanList)) != 0 || nSpanCount > 0xFF)
        {
            // Allocate CKey array for the file. Each entry describes one file span
            if((pCKeyEntry = new CASC_CKEY_ENTRY[nSpanCount]) != NULL)
            {
                // Prepare the span count to the first item
                pCKeyEntry->SpanCount = (BYTE)nSpanCount;

                // Prepare the archive offset in each CKey entry
                for(size_t i = 0; i < nSpanCount; i++)
                    pCKeyEntry[i].StorageOffset = 0;

                // Create an instance of the TCascFile
                if((hf = new TCascFile(NULL, pCKeyEntry)) != NULL)
                {
                    // Prepare the structure
                    hf->bVerifyIntegrity   = (dwOpenFlags & CASC_STRICT_DATA_CHECK)  ? true : false;
                    hf->bOvercomeEncrypted = (dwOpenFlags & CASC_OVERCOME_ENCRYPTED) ? true : false;
                    hf->bCloseFileStream = true;

                    // Open all local file spans
                    dwErrCode = hf->OpenFileSpans(szSpanList);
                    if(dwErrCode != ERROR_SUCCESS)
                    {
                        delete hf;
                        hf = NULL;
                    }
                }
            }
        }
        else
        {
            dwErrCode = ERROR_INVALID_PARAMETER;
        }

        delete [] szSpanList;
    }

    // Give the output parameter, no matter what
    PtrFileHandle[0] = (HANDLE)hf;

    // Handle last error
    if(dwErrCode != ERROR_SUCCESS)
        SetLastError(dwErrCode);
    return (dwErrCode == ERROR_SUCCESS);
}

bool SetCacheStrategy(HANDLE hFile, CSTRTG CacheStrategy)
{
    TCascFile * hf;

    // Validate the file handle
    if((hf = TCascFile::IsValid(hFile)) != NULL)
    {
        // The cache must not be initialized yet
        if(hf->pbFileCache == NULL)
        {
            hf->CacheStrategy = CacheStrategy;
            return true;
        }
    }

    // Failed. This should never happen
    assert(false);
    return false;
}

//-----------------------------------------------------------------------------
// Public functions

bool WINAPI CascOpenFile(HANDLE hStorage, const void * pvFileName, DWORD dwLocaleFlags, DWORD dwOpenFlags, HANDLE * PtrFileHandle)
{
    PCASC_CKEY_ENTRY pCKeyEntry = NULL;
    TCascStorage * hs;
    const char * szFileName;
    DWORD FileDataId = CASC_INVALID_ID;
    BYTE CKeyEKeyBuffer[MD5_HASH_SIZE];
    DWORD dwErrCode = ERROR_SUCCESS;

    // This parameter is not used
    CASCLIB_UNUSED(dwLocaleFlags);

    // Validate the storage handle
    hs = TCascStorage::IsValid(hStorage);
    if(hs == NULL)
    {
        SetLastError(ERROR_INVALID_HANDLE);
        return false;
    }

    // Validate the other parameters
    if(PtrFileHandle == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return false;
    }

    // Retrieve the CKey/EKey from the file name in different modes
    switch(dwOpenFlags & CASC_OPEN_TYPE_MASK)
    {
        case CASC_OPEN_BY_NAME:

            // The 'pvFileName' must be zero terminated ANSI file name
            szFileName = (const char *)pvFileName;
            if(szFileName == NULL || szFileName[0] == 0)
            {
                SetLastError(ERROR_INVALID_PARAMETER);
                return false;
            }

            // The first chance: Try to find the file by name (using the root handler)
            pCKeyEntry = hs->pRootHandler->GetFile(hs, szFileName);
            if(pCKeyEntry != NULL)
                break;

            // Second chance: If the file name is actually a file data id, we convert it to file data ID
            if(IsFileDataIdName(szFileName, FileDataId))
            {
                pCKeyEntry = hs->pRootHandler->GetFile(hs, FileDataId);
                if(pCKeyEntry != NULL)
                    break;
            }

            // Third chance: If the file name is a string representation of CKey/EKey, we try to query for CKey
            if(IsFileCKeyEKeyName(szFileName, CKeyEKeyBuffer))
            {
                pCKeyEntry = FindCKeyEntry_CKey(hs, CKeyEKeyBuffer);
                if(pCKeyEntry != NULL)
                    break;

                pCKeyEntry = FindCKeyEntry_EKey(hs, CKeyEKeyBuffer);
                if(pCKeyEntry != NULL)
                    break;
            }

            SetLastError(ERROR_FILE_NOT_FOUND);
            return false;

        case CASC_OPEN_BY_CKEY:

            // The 'pvFileName' must be a pointer to 16-byte CKey or EKey
            if(pvFileName == NULL)
            {
                SetLastError(ERROR_INVALID_PARAMETER);
                return false;
            }

            // Search the CKey map in order to find the CKey entry
            pCKeyEntry = FindCKeyEntry_CKey(hs, (LPBYTE)pvFileName);
            break;

        case CASC_OPEN_BY_EKEY:

            // The 'pvFileName' must be a pointer to 16-byte CKey or EKey
            if(pvFileName == NULL)
            {
                SetLastError(ERROR_INVALID_PARAMETER);
                return false;
            }

            // Search the CKey map in order to find the CKey entry
            pCKeyEntry = FindCKeyEntry_EKey(hs, (LPBYTE)pvFileName);
            break;

        case CASC_OPEN_BY_FILEID:

            // Retrieve the file CKey/EKey
            pCKeyEntry = hs->pRootHandler->GetFile(hs, CASC_FILE_DATA_ID_FROM_STRING(pvFileName));
            break;

        default:

            // Unknown open mode
            dwErrCode = ERROR_INVALID_PARAMETER;
            break;
    }

    // Perform the open operation
    return OpenFileByCKeyEntry(hs, pCKeyEntry, dwOpenFlags, PtrFileHandle);
}

bool WINAPI CascOpenLocalFile(LPCTSTR szFileName, DWORD dwOpenFlags, HANDLE * PtrFileHandle)
{
    // Verify parameters
    if(szFileName == NULL || szFileName[0] == 0 || PtrFileHandle == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return false;
    }

    return OpenLocalFile(szFileName, dwOpenFlags, PtrFileHandle);
}

bool WINAPI CascCloseFile(HANDLE hFile)
{
    TCascFile * hf;

    hf = TCascFile::IsValid(hFile);
    if (hf != NULL)
    {
        delete hf;
        return true;
    }

    SetLastError(ERROR_INVALID_HANDLE);
    return false;
}