forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpqapi.cpp
More file actions
655 lines (572 loc) · 16.8 KB
/
mpqapi.cpp
File metadata and controls
655 lines (572 loc) · 16.8 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/**
* @file mpqapi.cpp
*
* Implementation of functions for creating and editing MPQ files.
*/
#include "all.h"
#include "../3rdParty/Storm/Source/storm.h"
DWORD sgdwMpqOffset;
BYTE mpq_buf[4096];
_HASHENTRY *sgpHashTbl;
/** Has the savegame-file been modified in memory. */
BOOL save_archive_modified;
_BLOCKENTRY *sgpBlockTbl;
/** Is the savegame-file currently open. */
BOOLEAN save_archive_open;
#define INDEX_ENTRIES 2048
#define BLOCK_TABLE_SIZE (INDEX_ENTRIES * sizeof(_BLOCKENTRY))
#define HASH_TABLE_SIZE (INDEX_ENTRIES * sizeof(_HASHENTRY))
//note: 32872 = 32768 + 104 (sizeof(_FILEHEADER))
/* data */
HANDLE sghArchive = INVALID_HANDLE_VALUE;
static void mpqapi_xor_buf(char *pbData)
{
DWORD mask;
char *pbCurrentData;
int i;
mask = 0xF0761AB;
pbCurrentData = pbData;
for (i = 0; i < 8; i++) {
*pbCurrentData ^= mask;
pbCurrentData++;
mask = _rotl(mask, 1);
}
}
static BOOL mpqapi_reg_load_modification_time(char *dst, int size)
{
char *pszDst;
char *pbData;
DWORD nbytes_read;
pszDst = dst;
memset(dst, 0, size);
#ifdef SPAWN
if (!SRegLoadData(APP_NAME, "Audio Playback ", 0, (BYTE *)pszDst, size, &nbytes_read)) {
#else
if (!SRegLoadData(APP_NAME, "Video Player ", 0, (BYTE *)pszDst, size, &nbytes_read)) {
#endif
return FALSE;
}
if (nbytes_read != size)
return FALSE;
for (; size >= 8u; size -= 8) {
pbData = pszDst;
pszDst += 8;
mpqapi_xor_buf(pbData);
}
return TRUE;
}
static BOOLEAN mpqapi_reg_store_modification_time(char *pbData, DWORD dwLen)
{
char *pbCurrentData, *pbDataToXor;
DWORD i;
pbCurrentData = pbData;
if (dwLen >= 8) {
i = dwLen >> 3;
do {
pbDataToXor = pbCurrentData;
pbCurrentData += 8;
mpqapi_xor_buf(pbDataToXor);
i--;
} while (i);
}
#ifdef SPAWN
return SRegSaveData(APP_NAME, "Audio Playback ", 0, (BYTE *)pbData, dwLen);
#else
return SRegSaveData(APP_NAME, "Video Player ", 0, (BYTE *)pbData, dwLen);
#endif
}
BOOL mpqapi_set_hidden(const char *pszArchive, BOOL hidden)
{
DWORD dwFileAttributes;
DWORD dwFileAttributesToSet;
dwFileAttributes = GetFileAttributes(pszArchive);
if (dwFileAttributes == INVALID_FILE_ATTRIBUTES)
return GetLastError() == ERROR_FILE_NOT_FOUND;
dwFileAttributesToSet = hidden ? FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN : 0;
if (dwFileAttributes == dwFileAttributesToSet)
return TRUE;
else
return SetFileAttributes(pszArchive, dwFileAttributesToSet);
}
void mpqapi_store_default_time(DWORD dwChar)
{
/*
DWORD idx;
char dst[160];
if(gbMaxPlayers == 1) {
return;
}
/// ASSERT: assert(dwChar < MAX_CHARACTERS);
idx = 16 * dwChar;
mpqapi_reg_load_modification_time(dst, sizeof(dst));
*(DWORD *)&dst[idx + 4] = 0x78341348; // dwHighDateTime
mpqapi_reg_store_modification_time(dst, sizeof(dst));
*/
}
static void mpqapi_store_modified_time(const char *pszArchive, DWORD dwChar)
{
HANDLE handle;
struct _WIN32_FIND_DATAA FindFileData;
char dst[160];
if (gbMaxPlayers != 1) {
mpqapi_reg_load_modification_time(dst, 160);
handle = FindFirstFile(pszArchive, &FindFileData);
if (handle != INVALID_HANDLE_VALUE) {
FindClose(handle);
*((FILETIME *)(dst) + dwChar * 2 + 1) = FindFileData.ftLastWriteTime;
mpqapi_reg_store_modification_time(dst, 160);
}
}
}
void mpqapi_store_creation_time(const char *pszArchive, DWORD dwChar)
{
HANDLE handle;
struct _WIN32_FIND_DATAA FindFileData;
char dst[160];
if (gbMaxPlayers != 1) {
mpqapi_reg_load_modification_time(dst, 160);
handle = FindFirstFile(pszArchive, &FindFileData);
if (handle != INVALID_HANDLE_VALUE) {
FindClose(handle);
*((FILETIME *)(dst) + dwChar * 2) = FindFileData.ftCreationTime;
mpqapi_reg_store_modification_time(dst, 160);
}
}
}
static _BLOCKENTRY *mpqapi_new_block(int *block_index)
{
_BLOCKENTRY *blockEntry = sgpBlockTbl;
for (DWORD i = 0; i < INDEX_ENTRIES; i++, blockEntry++) {
if (blockEntry->offset != 0)
continue;
if (blockEntry->sizealloc != 0)
continue;
if (blockEntry->flags != 0)
continue;
if (blockEntry->sizefile != 0)
continue;
if (block_index)
*block_index = i;
return blockEntry;
}
app_fatal("Out of free block entries");
return NULL;
}
static void mpqapi_alloc_block(int block_offset, int block_size)
{
_BLOCKENTRY *block;
int i;
block = sgpBlockTbl;
i = INDEX_ENTRIES;
while (i-- != 0) {
if (block->offset && !block->flags && !block->sizefile) {
if (block->offset + block->sizealloc == block_offset) {
block_offset = block->offset;
block_size += block->sizealloc;
memset(block, 0, sizeof(_BLOCKENTRY));
mpqapi_alloc_block(block_offset, block_size);
return;
}
if (block_offset + block_size == block->offset) {
block_size += block->sizealloc;
memset(block, 0, sizeof(_BLOCKENTRY));
mpqapi_alloc_block(block_offset, block_size);
return;
}
}
block++;
}
if (block_offset + block_size > sgdwMpqOffset) {
app_fatal("MPQ free list error");
}
if (block_offset + block_size == sgdwMpqOffset) {
sgdwMpqOffset = block_offset;
} else {
block = mpqapi_new_block(NULL);
block->offset = block_offset;
block->sizealloc = block_size;
block->sizefile = 0;
block->flags = 0;
}
}
static int mpqapi_find_free_block(int size, int *block_size)
{
int result;
_BLOCKENTRY *pBlockTbl = sgpBlockTbl;
for (int i = INDEX_ENTRIES; i--; pBlockTbl++) {
if (pBlockTbl->offset == 0)
continue;
if (pBlockTbl->flags != 0)
continue;
if (pBlockTbl->sizefile != 0)
continue;
if ((DWORD)pBlockTbl->sizealloc < size)
continue;
result = pBlockTbl->offset;
*block_size = size;
pBlockTbl->offset += size;
pBlockTbl->sizealloc -= size;
if (pBlockTbl->sizealloc == 0)
memset(pBlockTbl, 0, sizeof(*pBlockTbl));
return result;
}
*block_size = size;
result = sgdwMpqOffset;
sgdwMpqOffset += size;
return result;
}
static int mpqapi_get_hash_index(int index, int hash_a, int hash_b, int locale)
{
DWORD idx, i;
i = INDEX_ENTRIES;
for (idx = index & 0x7FF; sgpHashTbl[idx].block != -1; idx = (idx + 1) & 0x7FF) {
if (i-- == 0)
break;
if (sgpHashTbl[idx].hashcheck[0] != hash_a)
continue;
if (sgpHashTbl[idx].hashcheck[1] != hash_b)
continue;
if (sgpHashTbl[idx].lcid != locale)
continue;
if (sgpHashTbl[idx].block == -2)
continue;
return idx;
}
return -1;
}
static BOOL WriteMPQHeader()
{
_FILEHEADER fhdr;
DWORD NumberOfBytesWritten;
memset(&fhdr, 0, sizeof(fhdr));
fhdr.signature = '\x1AQPM';
fhdr.headersize = 32;
fhdr.filesize = GetFileSize(sghArchive, 0);
fhdr.version = 0;
fhdr.sectorsizeid = 3;
fhdr.hashoffset = 32872;
fhdr.blockoffset = 104;
fhdr.hashcount = INDEX_ENTRIES;
fhdr.blockcount = INDEX_ENTRIES;
if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1)
return FALSE;
if (!WriteFile(sghArchive, &fhdr, sizeof(fhdr), &NumberOfBytesWritten, 0))
return FALSE;
return NumberOfBytesWritten == 104;
}
static BOOL mpqapi_write_block_table()
{
BOOL success;
DWORD NumberOfBytesWritten;
if (SetFilePointer(sghArchive, 104, NULL, FILE_BEGIN) == -1)
return FALSE;
Encrypt((DWORD *)sgpBlockTbl, BLOCK_TABLE_SIZE, Hash("(block table)", 3));
success = WriteFile(sghArchive, sgpBlockTbl, BLOCK_TABLE_SIZE, &NumberOfBytesWritten, 0);
Decrypt((DWORD *)sgpBlockTbl, BLOCK_TABLE_SIZE, Hash("(block table)", 3));
return success && NumberOfBytesWritten == BLOCK_TABLE_SIZE;
}
static BOOL mpqapi_write_hash_table()
{
BOOL success;
DWORD NumberOfBytesWritten;
if (SetFilePointer(sghArchive, 32872, NULL, FILE_BEGIN) == -1)
return FALSE;
Encrypt((DWORD *)sgpHashTbl, HASH_TABLE_SIZE, Hash("(hash table)", 3));
success = WriteFile(sghArchive, sgpHashTbl, HASH_TABLE_SIZE, &NumberOfBytesWritten, 0);
Decrypt((DWORD *)sgpHashTbl, HASH_TABLE_SIZE, Hash("(hash table)", 3));
return success && NumberOfBytesWritten == HASH_TABLE_SIZE;
}
static BOOL mpqapi_can_seek()
{
if (SetFilePointer(sghArchive, sgdwMpqOffset, NULL, FILE_BEGIN) == -1)
return FALSE;
return SetEndOfFile(sghArchive);
}
static BOOL ParseMPQHeader(_FILEHEADER *pHdr, DWORD *pdwNextFileStart)
{
DWORD size;
DWORD NumberOfBytesRead;
size = GetFileSize(sghArchive, 0);
*pdwNextFileStart = size;
if (size == -1
|| size < sizeof(*pHdr)
|| !ReadFile(sghArchive, pHdr, sizeof(*pHdr), &NumberOfBytesRead, NULL)
|| NumberOfBytesRead != 104
|| pHdr->signature != '\x1AQPM'
|| pHdr->headersize != 32
|| pHdr->version > 0
|| pHdr->sectorsizeid != 3
|| pHdr->filesize != size
|| pHdr->hashoffset != 32872
|| pHdr->blockoffset != 104
|| pHdr->hashcount != INDEX_ENTRIES
|| pHdr->blockcount != INDEX_ENTRIES) {
if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1)
return FALSE;
if (!SetEndOfFile(sghArchive))
return FALSE;
memset(pHdr, 0, sizeof(*pHdr));
pHdr->signature = '\x1AQPM';
pHdr->headersize = 32;
pHdr->sectorsizeid = 3;
pHdr->version = 0;
*pdwNextFileStart = 0x10068;
save_archive_modified = TRUE;
save_archive_open = TRUE;
}
return TRUE;
}
static int FetchHandle(const char *pszName)
{
return mpqapi_get_hash_index(Hash(pszName, 0), Hash(pszName, 1), Hash(pszName, 2), 0);
}
void mpqapi_remove_hash_entry(const char *pszName)
{
_HASHENTRY *pHashTbl;
_BLOCKENTRY *blockEntry;
int hIdx, block_offset, block_size;
hIdx = FetchHandle(pszName);
if (hIdx != -1) {
pHashTbl = &sgpHashTbl[hIdx];
blockEntry = &sgpBlockTbl[pHashTbl->block];
pHashTbl->block = -2;
block_offset = blockEntry->offset;
block_size = blockEntry->sizealloc;
memset(blockEntry, 0, sizeof(*blockEntry));
mpqapi_alloc_block(block_offset, block_size);
save_archive_modified = TRUE;
}
}
void mpqapi_remove_hash_entries(BOOL(__stdcall *fnGetName)(DWORD, char *))
{
DWORD dwIndex, i;
char pszFileName[MAX_PATH];
dwIndex = 1;
for (i = fnGetName(0, pszFileName); i; i = fnGetName(dwIndex++, pszFileName)) {
mpqapi_remove_hash_entry(pszFileName);
}
}
static _BLOCKENTRY *mpqapi_add_file(const char *pszName, _BLOCKENTRY *pBlk, int block_index)
{
DWORD h1, h2, h3;
int i, hIdx;
h1 = Hash(pszName, 0);
h2 = Hash(pszName, 1);
h3 = Hash(pszName, 2);
if (mpqapi_get_hash_index(h1, h2, h3, 0) != -1)
app_fatal("Hash collision between \"%s\" and existing file\n", pszName);
hIdx = h1 & 0x7FF;
i = INDEX_ENTRIES;
while (i--) {
if (sgpHashTbl[hIdx].block == -1 || sgpHashTbl[hIdx].block == -2)
break;
hIdx = (hIdx + 1) & 0x7FF;
}
if (i < 0)
app_fatal("Out of hash space");
if (!pBlk)
pBlk = mpqapi_new_block(&block_index);
sgpHashTbl[hIdx].hashcheck[0] = h2;
sgpHashTbl[hIdx].hashcheck[1] = h3;
sgpHashTbl[hIdx].lcid = 0;
sgpHashTbl[hIdx].block = block_index;
return pBlk;
}
static BOOL mpqapi_write_file_contents(const char *pszName, const BYTE *pbData, DWORD dwLen, _BLOCKENTRY *pBlk)
{
DWORD *sectoroffsettable;
DWORD destsize, num_bytes, block_size, nNumberOfBytesToWrite;
const char *tmp;
int i, j;
while ((tmp = strchr(pszName, ':'))) {
pszName = tmp + 1;
}
while ((tmp = strchr(pszName, '\\'))) {
pszName = tmp + 1;
}
Hash(pszName, 3);
num_bytes = (dwLen + 4095) >> 12;
nNumberOfBytesToWrite = 4 * num_bytes + 4;
pBlk->offset = mpqapi_find_free_block(dwLen + nNumberOfBytesToWrite, &pBlk->sizealloc);
pBlk->sizefile = dwLen;
pBlk->flags = 0x80000100;
if (SetFilePointer(sghArchive, pBlk->offset, NULL, FILE_BEGIN) == (DWORD)-1)
return FALSE;
j = 0;
destsize = 0;
sectoroffsettable = NULL;
while (dwLen != 0) {
DWORD len;
for (i = 0; i < 4096; i++)
mpq_buf[i] -= 86;
len = dwLen;
if (dwLen >= 4096)
len = 4096;
memcpy(mpq_buf, pbData, len);
pbData += len;
len = PkwareCompress(mpq_buf, len);
if (j == 0) {
nNumberOfBytesToWrite = 4 * num_bytes + 4;
sectoroffsettable = (DWORD *)DiabloAllocPtr(nNumberOfBytesToWrite);
memset(sectoroffsettable, 0, nNumberOfBytesToWrite);
if (!WriteFile(sghArchive, sectoroffsettable, nNumberOfBytesToWrite, &nNumberOfBytesToWrite, 0)) {
goto on_error;
}
destsize += nNumberOfBytesToWrite;
}
sectoroffsettable[j] = destsize;
if (!WriteFile(sghArchive, mpq_buf, len, &len, NULL)) {
goto on_error;
}
j++;
if (dwLen > 4096)
dwLen -= 4096;
else
dwLen = 0;
destsize += len;
}
sectoroffsettable[j] = destsize;
if (SetFilePointer(sghArchive, -destsize, NULL, FILE_CURRENT) == (DWORD)-1) {
goto on_error;
}
if (!WriteFile(sghArchive, sectoroffsettable, nNumberOfBytesToWrite, &nNumberOfBytesToWrite, 0)) {
goto on_error;
}
if (SetFilePointer(sghArchive, destsize - nNumberOfBytesToWrite, NULL, FILE_CURRENT) == (DWORD)-1) {
goto on_error;
}
mem_free_dbg(sectoroffsettable);
if (destsize < pBlk->sizealloc) {
block_size = pBlk->sizealloc - destsize;
if (block_size >= 1024) {
pBlk->sizealloc = destsize;
mpqapi_alloc_block(pBlk->sizealloc + pBlk->offset, block_size);
}
}
return TRUE;
on_error:
if (sectoroffsettable)
mem_free_dbg(sectoroffsettable);
return FALSE;
}
BOOL mpqapi_write_file(const char *pszName, const BYTE *pbData, DWORD dwLen)
{
_BLOCKENTRY *blockEntry;
save_archive_modified = TRUE;
mpqapi_remove_hash_entry(pszName);
blockEntry = mpqapi_add_file(pszName, 0, 0);
if (!mpqapi_write_file_contents(pszName, pbData, dwLen, blockEntry)) {
mpqapi_remove_hash_entry(pszName);
return FALSE;
}
return TRUE;
}
void mpqapi_rename(char *pszOld, char *pszNew)
{
int index, block;
_HASHENTRY *hashEntry;
_BLOCKENTRY *blockEntry;
index = FetchHandle(pszOld);
if (index != -1) {
hashEntry = &sgpHashTbl[index];
block = hashEntry->block;
blockEntry = &sgpBlockTbl[block];
hashEntry->block = -2;
mpqapi_add_file(pszNew, blockEntry, block);
save_archive_modified = TRUE;
}
}
BOOL mpqapi_has_file(const char *pszName)
{
return FetchHandle(pszName) != -1;
}
static void CloseMPQ(const char *pszArchive, BOOL bFree, DWORD dwChar)
{
if (bFree) {
MemFreeDbg(sgpBlockTbl);
MemFreeDbg(sgpHashTbl);
}
if (sghArchive != INVALID_HANDLE_VALUE) {
CloseHandle(sghArchive);
sghArchive = INVALID_HANDLE_VALUE;
}
if (save_archive_modified) {
save_archive_modified = FALSE;
mpqapi_store_modified_time(pszArchive, dwChar);
}
if (save_archive_open) {
save_archive_open = FALSE;
mpqapi_store_creation_time(pszArchive, dwChar);
}
}
BOOL OpenMPQ(const char *pszArchive, BOOL hidden, DWORD dwChar)
{
DWORD dwFlagsAndAttributes;
DWORD key;
DWORD dwTemp;
_FILEHEADER fhdr;
InitHash();
if (!mpqapi_set_hidden(pszArchive, hidden)) {
return FALSE;
}
dwFlagsAndAttributes = gbMaxPlayers > 1 ? FILE_FLAG_WRITE_THROUGH : 0;
save_archive_open = FALSE;
sghArchive = CreateFile(pszArchive, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
if (sghArchive == INVALID_HANDLE_VALUE) {
sghArchive = CreateFile(pszArchive, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, dwFlagsAndAttributes | (hidden ? FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN : 0), NULL);
if (sghArchive == INVALID_HANDLE_VALUE)
return FALSE;
save_archive_open = TRUE;
save_archive_modified = TRUE;
}
if (sgpBlockTbl == NULL || sgpHashTbl == NULL) {
memset(&fhdr, 0, sizeof(fhdr));
if (ParseMPQHeader(&fhdr, &sgdwMpqOffset) == FALSE) {
goto on_error;
}
sgpBlockTbl = (_BLOCKENTRY *)DiabloAllocPtr(BLOCK_TABLE_SIZE);
memset(sgpBlockTbl, 0, BLOCK_TABLE_SIZE);
if (fhdr.blockcount) {
if (SetFilePointer(sghArchive, 104, NULL, FILE_BEGIN) == -1)
goto on_error;
if (!ReadFile(sghArchive, sgpBlockTbl, BLOCK_TABLE_SIZE, &dwTemp, NULL))
goto on_error;
key = Hash("(block table)", 3);
Decrypt((DWORD *)sgpBlockTbl, BLOCK_TABLE_SIZE, key);
}
sgpHashTbl = (_HASHENTRY *)DiabloAllocPtr(HASH_TABLE_SIZE);
memset(sgpHashTbl, 255, HASH_TABLE_SIZE);
if (fhdr.hashcount) {
if (SetFilePointer(sghArchive, 32872, NULL, FILE_BEGIN) == -1)
goto on_error;
if (!ReadFile(sghArchive, sgpHashTbl, HASH_TABLE_SIZE, &dwTemp, NULL))
goto on_error;
key = Hash("(hash table)", 3);
Decrypt((DWORD *)sgpHashTbl, HASH_TABLE_SIZE, key);
}
return TRUE;
}
return TRUE;
on_error:
CloseMPQ(pszArchive, TRUE, dwChar);
return FALSE;
}
BOOL mpqapi_flush_and_close(const char *pszArchive, BOOL bFree, DWORD dwChar)
{
BOOL ret = FALSE;
if (sghArchive == INVALID_HANDLE_VALUE)
ret = TRUE;
else {
ret = FALSE;
if (!save_archive_modified)
ret = TRUE;
else if (mpqapi_can_seek() && WriteMPQHeader() && mpqapi_write_block_table()) {
if (mpqapi_write_hash_table())
ret = TRUE;
else
ret = FALSE;
}
}
CloseMPQ(pszArchive, bFree, dwChar);
return ret;
}