aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/CascOpenFile.cpp
blob: e7cdb9aa1072dadf435e3499e5bf2f4b93018e47 (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
/*****************************************************************************/
/* 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"
#include "CascMndxRoot.h"

//-----------------------------------------------------------------------------
// Local structures

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

TCascFile * IsValidFileHandle(HANDLE hFile)
{
    TCascFile * hf = (TCascFile *)hFile;

    return (hf != NULL && hf->hs != NULL && hf->szClassName != NULL && !strcmp(hf->szClassName, "TCascFile")) ? hf : NULL;
}

PCASC_INDEX_ENTRY FindIndexEntry(TCascStorage * hs, PQUERY_KEY pIndexKey)
{
    PCASC_INDEX_ENTRY pIndexEntry = NULL;

    if(hs->pIndexEntryMap != NULL)
        pIndexEntry = (PCASC_INDEX_ENTRY)Map_FindObject(hs->pIndexEntryMap, pIndexKey->pbData);

    return pIndexEntry;
}

PCASC_ENCODING_ENTRY FindEncodingEntry(TCascStorage * hs, PQUERY_KEY pEncodingKey, size_t * PtrIndex)
{
    PCASC_ENCODING_ENTRY pEncodingEntry;
    size_t StartEntry = 0;
    size_t MidlEntry;
    size_t EndEntry = hs->nEncodingEntries;
    int nResult;

    // Perform binary search
    while(StartEntry < EndEntry)
    {
        // Calculate the middle of the interval
        MidlEntry = StartEntry + ((EndEntry - StartEntry) / 2);
        pEncodingEntry = hs->ppEncodingEntries[MidlEntry];

        // Did we find it?
        nResult = memcmp(pEncodingKey->pbData, pEncodingEntry->EncodingKey, MD5_HASH_SIZE);
        if(nResult == 0)
        {
            if(PtrIndex != NULL)
                PtrIndex[0] = MidlEntry;
            return pEncodingEntry;
        }

        // Move the interval to the left or right
        (nResult < 0) ? EndEntry = MidlEntry : StartEntry = MidlEntry + 1;
    }

    // Not found, sorry
    return NULL;
}

// Also used in CascSearchFile
PCASC_ROOT_ENTRY FindFirstRootEntry(TCascStorage * hs, const char * szFileName, size_t * PtrIndex)
{
    PCASC_ROOT_ENTRY pFoundEntry = NULL;
    PCASC_ROOT_ENTRY pRootEntry;
    ULONGLONG FileNameHash;
    uint32_t dwHashHigh = 0;
    uint32_t dwHashLow = 0;
    size_t StartEntry = 0;
    size_t MidlEntry = 0;
    size_t EndEntry = hs->nRootEntries;

    // Calculate the HASH value of the normalized file name
    hashlittle2(szFileName, strlen(szFileName), &dwHashHigh, &dwHashLow);
    FileNameHash = ((ULONGLONG)dwHashHigh << 0x20) | dwHashLow;

    // Perform binary search
    while(StartEntry < EndEntry)
    {
        // Calculate the middle of the interval
        MidlEntry = StartEntry + ((EndEntry - StartEntry) / 2);
        pRootEntry = hs->ppRootEntries[MidlEntry];

        // Did we find it?
        if(pRootEntry->FileNameHash == FileNameHash)
        {
            pFoundEntry = pRootEntry;
            break;
        }

        // Move the interval to the left or right
        (FileNameHash < pRootEntry->FileNameHash) ? EndEntry = MidlEntry : StartEntry = MidlEntry + 1;
    }

    // Move the pointer back to the first entry with that hash
    if(pFoundEntry != NULL)
    {
        while(MidlEntry > 0 && hs->ppRootEntries[MidlEntry - 1]->FileNameHash == FileNameHash)
        {
            pFoundEntry = hs->ppRootEntries[MidlEntry - 1];
            MidlEntry--;
        }
    }

    // Return what we found
    if(PtrIndex != NULL)
        *PtrIndex = MidlEntry;
    return pFoundEntry;
}

// Check the root directory for that hash
PCASC_ROOT_ENTRY FindRootEntryLocale(TCascStorage * hs, char * szFileName, DWORD Locale)
{
    PCASC_ROOT_ENTRY pThatEntry = NULL;
    PCASC_ROOT_ENTRY pENUSEntry = NULL;
    PCASC_ROOT_ENTRY pENGBEntry = NULL;
    PCASC_ROOT_ENTRY pAnyEntry = NULL;
    PCASC_ROOT_ENTRY pEndEntry = NULL;
    PCASC_ROOT_ENTRY pRootEntry = NULL;
    ULONGLONG FileNameHash;
    size_t EntryIndex = 0;
    size_t EndEntry = hs->nRootEntries;

    // Find a root entry with the given name hash
    pRootEntry = FindFirstRootEntry(hs, szFileName, &EntryIndex);
    if(pRootEntry != NULL)
    {
        // Rememeber the file name hash
        pEndEntry = hs->pRootEntries + hs->nRootEntries;
        FileNameHash = pRootEntry->FileNameHash;

        // Find all suitable root entries
        while(EntryIndex < EndEntry)
        {
            // Get the root entry
            pRootEntry = hs->ppRootEntries[EntryIndex++];
            if(pRootEntry->FileNameHash != FileNameHash)
                break;

            // If a locale has been given, check it
            if(pThatEntry == NULL && Locale != 0 && (Locale & pRootEntry->Locales))
                pThatEntry = pRootEntry;
            if(pENUSEntry == NULL && (pRootEntry->Locales & CASC_LOCALE_ENUS))
                pENUSEntry = pRootEntry;
            if(pENGBEntry == NULL && (pRootEntry->Locales & CASC_LOCALE_ENGB))
                pENGBEntry = pRootEntry;
            if(pAnyEntry == NULL)
                pAnyEntry = pRootEntry;
            
            // Move to the next one
            pRootEntry++;
        }

        // Return the key by priority
        if(pThatEntry != NULL)
            return pThatEntry;
        if(pENGBEntry != NULL)
            return pENGBEntry;
        if(pENUSEntry != NULL)
            return pENUSEntry;
    }

    // Return whatever we got
    return pAnyEntry;
}

static TCascFile * CreateFileHandle(TCascStorage * hs, PCASC_INDEX_ENTRY pIndexEntry)
{
    ULONGLONG FileOffsMask = ((ULONGLONG)1 << hs->KeyMapping[0].SegmentBits) - 1;
    ULONGLONG FileOffset = ConvertBytesToInteger_5(pIndexEntry->FileOffset);
    TCascFile * hf;

    // Allocate the CASC file structure
    hf = (TCascFile *)CASC_ALLOC(TCascFile, 1);
    if(hf != NULL)
    {
        // Initialize the structure
        memset(hf, 0, sizeof(TCascFile));
        hf->ArchiveIndex = (DWORD)(FileOffset >> hs->KeyMapping[0].SegmentBits);
        hf->HeaderOffset = (DWORD)(FileOffset & FileOffsMask);
        hf->szClassName = "TCascFile";
        
        // Copy the compressed file size
        hf->CompressedSize = ConvertBytesToInteger_4_LE(pIndexEntry->FileSize) - 0x1E;

        // For now, we set the file size to be equal to compressed size
        // This is used when loading the "encoding" file, which does not
        // have entry in the encoding itself
        hf->FileSize = hf->CompressedSize;

        // Increment the number of references to the archive
        hs->dwRefCount++;
        hf->hs = hs;
    }

    return hf;
}

static bool OpenFileByIndexKey(TCascStorage * hs, PQUERY_KEY pIndexKey, DWORD dwFlags, HANDLE * phFile)
{
    PCASC_INDEX_ENTRY pIndexEntry;
    TCascFile * hf = NULL;
    int nError = ERROR_SUCCESS;

    CASCLIB_UNUSED(dwFlags);

    // Find the key entry in the array of file keys
    pIndexEntry = FindIndexEntry(hs, pIndexKey);
    if(pIndexEntry == NULL)
        nError = ERROR_FILE_NOT_FOUND;

    // Create the file handle structure
    if(nError == ERROR_SUCCESS)
    {
        hf = CreateFileHandle(hs, pIndexEntry);
        *phFile = (HANDLE)hf;
        if(hf == NULL)
            nError = ERROR_FILE_NOT_FOUND;
    }

    if(nError != ERROR_SUCCESS)
        SetLastError(nError);
    return (nError == ERROR_SUCCESS);
}

static bool OpenFileByEncodingKey(TCascStorage * hs, PQUERY_KEY pEncodingKey, DWORD dwFlags, HANDLE * phFile)
{
    PCASC_ENCODING_ENTRY pEncodingEntry;
    QUERY_KEY IndexKey;
    TCascFile * hf = NULL;
    int nError = ERROR_SUCCESS;

    // Find the encoding entry
    pEncodingEntry = FindEncodingEntry(hs, pEncodingKey, NULL);
    if(pEncodingEntry == NULL)
        nError = ERROR_FILE_NOT_FOUND;

    // Prepare the file index and open the file by index
    // Note: We don't know what to do if there is more than just one index key
    // We always take the first file present. Is that correct?
//  IndexKey.pbData = pEncodingEntry->EncodingKey + (MD5_HASH_SIZE * pEncodingEntry->KeyCount);
//  assert(pEncodingEntry->KeyCount == 1);
    IndexKey.pbData = pEncodingEntry->EncodingKey + MD5_HASH_SIZE;
    IndexKey.cbData = MD5_HASH_SIZE;
    if(OpenFileByIndexKey(hs, &IndexKey, dwFlags, phFile))
    {
        // Fix the file size from the encoding key
        hf = IsValidFileHandle(*phFile);
        if(hf != NULL)
        {
            hf->FileSize = ConvertBytesToInteger_4(pEncodingEntry->FileSizeBytes);
            return true;
        }
    }

    return false;
}

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

bool WINAPI CascOpenFileByIndexKey(HANDLE hStorage, PQUERY_KEY pIndexKey, DWORD dwFlags, HANDLE * phFile)
{
    TCascStorage * hs;

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

    // Validate the other parameters
    if(pIndexKey == NULL || pIndexKey->pbData == NULL || pIndexKey->cbData == 0 || phFile == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return false;
    }

    // Use the internal function to open the file
    return OpenFileByIndexKey(hs, pIndexKey, dwFlags, phFile);
}

bool WINAPI CascOpenFileByEncodingKey(HANDLE hStorage, PQUERY_KEY pEncodingKey, DWORD dwFlags, HANDLE * phFile)
{
    TCascStorage * hs;

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

    // Validate the other parameters
    if(pEncodingKey == NULL || pEncodingKey->pbData == NULL || pEncodingKey->cbData == 0 || phFile == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return false;
    }

    // Use the internal function fo open the file
    return OpenFileByEncodingKey(hs, pEncodingKey, dwFlags, phFile);
}

bool WINAPI CascOpenFile(HANDLE hStorage, const char * szFileName, DWORD dwLocale, DWORD dwFlags, HANDLE * phFile)
{
    CASC_ROOT_KEY_INFO EncodingKeyInfo;
    PCASC_ROOT_ENTRY pRootEntry;
    PCASC_PACKAGE pPackage;
    TCascStorage * hs;
    QUERY_KEY EncodingKey;
    char * szStrippedName;
    char * szFileName2;
    int nError = ERROR_SUCCESS;

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

    // Validate the other parameters
    if(szFileName == NULL || szFileName[0] == 0 || phFile == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return false;
    }

    // Create the copy of the file name
    szFileName2 = NewStr(szFileName, 0);
    if(szFileName2 != NULL)
    {
        // If the storage has a MNDX root directory, use it to search the entry
        if(hs->pMndxInfo != NULL)
        {
            // Convert the file name to lowercase + slashes
            NormalizeFileName_LowerSlash(szFileName2);

            // Find the package number
            pPackage = FindMndxPackage(hs, szFileName2);
            if(pPackage != NULL)
            {
                // Cut the package name off the full path
                szStrippedName = szFileName2 + pPackage->nLength;
                while(szStrippedName[0] == '/')
                    szStrippedName++;

                nError = SearchMndxInfo(hs->pMndxInfo, szStrippedName, (DWORD)(pPackage - hs->pPackages->Packages), &EncodingKeyInfo);
                if(nError == ERROR_SUCCESS)
                {
                    // Prepare the encoding key
                    EncodingKey.pbData = EncodingKeyInfo.EncodingKey;
                    EncodingKey.cbData = MD5_HASH_SIZE;
                }
            }
            else
            {
                nError = ERROR_FILE_NOT_FOUND;
            }
        }
        else
        {
            // Convert the file name to lowercase + slashes
            NormalizeFileName_UpperBkSlash(szFileName2);

            // Check the root directory for that hash
            pRootEntry = FindRootEntryLocale(hs, szFileName2, dwLocale);
            nError = (pRootEntry != NULL) ? ERROR_SUCCESS : ERROR_FILE_NOT_FOUND;
            if(pRootEntry != NULL)
            {
                // Prepare the root key
                EncodingKey.pbData = pRootEntry->EncodingKey;
                EncodingKey.cbData = MD5_HASH_SIZE;
            }
        }

        // Use the root key to find the file in the encoding table entry
        if(nError == ERROR_SUCCESS)
        {
            if(!OpenFileByEncodingKey(hs, &EncodingKey, dwFlags, phFile))
            {
                assert(GetLastError() != ERROR_SUCCESS);
                nError = GetLastError();
            }
        }

        // Delete the file name copy
        delete [] szFileName2;
    }
    else
        nError = ERROR_NOT_ENOUGH_MEMORY;

    if(nError != ERROR_SUCCESS)
        SetLastError(nError);
    return (nError == ERROR_SUCCESS);
}

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

    hf = IsValidFileHandle(hFile);
    if(hf != NULL)
    {
        // Close (dereference) the archive handle
        if(hf->hs != NULL)
            CascCloseStorage((HANDLE)hf->hs);
        hf->hs = NULL;

        // Free the file cache and frame array
        if(hf->pbFileCache != NULL)
            CASC_FREE(hf->pbFileCache);
        if(hf->pFrames != NULL)
            CASC_FREE(hf->pFrames);

        // Free the structure itself
        hf->szClassName = NULL;
        CASC_FREE(hf);
        return true;
    }

    SetLastError(ERROR_INVALID_HANDLE);
    return false;
}