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
829 lines (773 loc) · 20.5 KB
/
mpqapi.cpp
File metadata and controls
829 lines (773 loc) · 20.5 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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
//HEADER_GOES_HERE
#include "../types.h"
int mpqapi_cpp_init_value; // weak
int sgdwMpqOffset; // idb
char mpq_buf[4096];
_HASHENTRY *sgpHashTbl;
bool save_archive_modified; // weak
_BLOCKENTRY *sgpBlockTbl;
bool save_archive_open; // weak
int mpqapi_inf = 0x7F800000; // weak
/* rdata */
HANDLE sghArchive = (HANDLE)0xFFFFFFFF; // idb
//----- (0043AE95) --------------------------------------------------------
struct mpqapi_cpp_init
{
mpqapi_cpp_init()
{
mpqapi_cpp_init_value = mpqapi_inf;
}
} _mpqapi_cpp_init;
// 47F148: using guessed type int mpqapi_inf;
// 659B00: using guessed type int mpqapi_cpp_init_value;
//----- (0043AEA0) --------------------------------------------------------
bool __fastcall mpqapi_set_hidden(char *pszArchive, bool hidden)
{
char *v2; // edi
BOOL v3; // esi
DWORD v4; // eax
bool result; // al
DWORD v6; // esi
v2 = pszArchive;
v3 = hidden;
v4 = GetFileAttributesA(pszArchive);
if ( v4 == -1 )
return GetLastError() == ERROR_FILE_NOT_FOUND;
v6 = v3 != 0 ? FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN : 0;
if ( v4 == v6 )
result = 1;
else
result = SetFileAttributesA(v2, v6);
return result;
}
//----- (0043AEDC) --------------------------------------------------------
void __fastcall mpqapi_store_creation_time(char *pszArchive, int dwChar)
{
int v2; // esi
char *v3; // ebx
HANDLE v4; // eax
int v5; // esi
struct _WIN32_FIND_DATAA FindFileData; // [esp+8h] [ebp-1E0h]
char dst[160]; // [esp+148h] [ebp-A0h]
v2 = dwChar;
v3 = pszArchive;
if ( gbMaxPlayers != 1 )
{
mpqapi_reg_load_modification_time(dst, 160);
v4 = FindFirstFileA(v3, &FindFileData);
if ( v4 != (HANDLE)-1 )
{
FindClose(v4);
v5 = 16 * v2;
*(_DWORD *)&dst[v5] = FindFileData.ftCreationTime.dwLowDateTime;
*(_DWORD *)&dst[v5 + 4] = FindFileData.ftCreationTime.dwHighDateTime;
mpqapi_reg_store_modification_time(dst, 160);
}
}
}
// 679660: using guessed type char gbMaxPlayers;
//----- (0043AF4F) --------------------------------------------------------
bool __fastcall mpqapi_reg_load_modification_time(char *dst, int size)
{
unsigned int v2; // esi
char *v3; // edi
unsigned int v6; // esi
char *v7; // ecx
int nbytes_read; // [esp+8h] [ebp-4h]
v2 = size;
v3 = dst;
memset(dst, 0, size);
if ( !SRegLoadData("Diablo", "Video Player ", 0, (unsigned char *)v3, v2, (unsigned long *)&nbytes_read) || nbytes_read != v2 )
return 0;
if ( v2 >= 8 )
{
v6 = v2 >> 3;
do
{
v7 = v3;
v3 += 8;
mpqapi_xor_buf(v7);
--v6;
}
while ( v6 );
}
return 1;
}
//----- (0043AFA5) --------------------------------------------------------
void __fastcall mpqapi_xor_buf(char *pbData)
{
signed int v1; // eax
char *v2; // esi
signed int v3; // edi
v1 = 0xF0761AB;
v2 = pbData;
v3 = 8;
do
{
*v2 ^= v1;
++v2;
v1 = _rotl(v1, 1);
--v3;
}
while ( v3 );
}
//----- (0043AFC4) --------------------------------------------------------
bool __fastcall mpqapi_reg_store_modification_time(char *pbData, int dwLen)
{
int v2; // ebx
char *v3; // ebp
char *v4; // edi
unsigned int v5; // esi
char *v6; // ecx
v2 = dwLen;
v3 = pbData;
v4 = pbData;
if ( (unsigned int)dwLen >= 8 )
{
v5 = (unsigned int)dwLen >> 3;
do
{
v6 = v4;
v4 += 8;
mpqapi_xor_buf(v6);
--v5;
}
while ( v5 );
}
return SRegSaveData("Diablo", "Video Player ", 0, (unsigned char *)v3, v2);
}
//----- (0043B002) --------------------------------------------------------
void __fastcall mpqapi_remove_hash_entry(char *pszName)
{
int v1; // eax
_HASHENTRY *v2; // ecx
_BLOCKENTRY *v3; // eax
int v4; // esi
int v5; // edi
v1 = mpqapi_get_hash_index_of_path(pszName);
if ( v1 != -1 )
{
v2 = &sgpHashTbl[v1];
v3 = &sgpBlockTbl[v2->block];
v2->block = -2;
v4 = v3->offset;
v5 = v3->sizealloc;
memset(v3, 0, 0x10u);
mpqapi_alloc_block(v4, v5);
save_archive_modified = 1;
}
}
// 65AB0C: using guessed type int save_archive_modified;
//----- (0043B054) --------------------------------------------------------
void __fastcall mpqapi_alloc_block(int block_offset, int block_size)
{
int v2; // esi
int v3; // edi
_BLOCKENTRY *v4; // eax
signed int v5; // edx
signed int v6; // ecx
int v7; // ecx
bool v8; // zf
_BLOCKENTRY *v9; // eax
v2 = block_size;
v3 = block_offset;
LABEL_2:
v4 = sgpBlockTbl;
v5 = 2048;
while ( 1 )
{
v6 = v5--;
if ( !v6 )
break;
v7 = v4->offset;
if ( v4->offset && !v4->flags && !v4->sizefile )
{
if ( v7 + v4->sizealloc == v3 )
{
v3 = v4->offset;
LABEL_11:
v2 += v4->sizealloc;
memset(v4, 0, 0x10u);
goto LABEL_2;
}
if ( v3 + v2 == v7 )
goto LABEL_11;
}
++v4;
}
v8 = v3 + v2 == sgdwMpqOffset;
if ( v3 + v2 > sgdwMpqOffset )
{
TermMsg("MPQ free list error");
v8 = v3 + v2 == sgdwMpqOffset;
}
if ( v8 )
{
sgdwMpqOffset = v3;
}
else
{
v9 = mpqapi_new_block(0);
v9->offset = v3;
v9->sizealloc = v2;
v9->sizefile = 0;
v9->flags = 0;
}
}
//----- (0043B0E4) --------------------------------------------------------
_BLOCKENTRY *__fastcall mpqapi_new_block(int *block_index)
{
_BLOCKENTRY *result; // eax
unsigned int v2; // edx
result = sgpBlockTbl;
v2 = 0;
while ( result->offset || result->sizealloc || result->flags || result->sizefile )
{
++v2;
++result;
if ( v2 >= 0x800 )
{
TermMsg("Out of free block entries");
return 0;
}
}
if ( block_index )
*block_index = v2;
return result;
}
//----- (0043B123) --------------------------------------------------------
int __fastcall mpqapi_get_hash_index_of_path(char *pszName) // FetchHandle
{
char *v1; // esi
int v2; // ST00_4
int v3; // edi
short v4; // ax
v1 = pszName;
v2 = encrypt_hash(pszName, 2); // MPQ_HASH_NAME_B
v3 = encrypt_hash(v1, 1); // MPQ_HASH_NAME_A
v4 = encrypt_hash(v1, 0); // MPQ_HASH_TABLE_INDEX
return mpqapi_get_hash_index(v4, v3, v2, 0);
}
//----- (0043B153) --------------------------------------------------------
int __fastcall mpqapi_get_hash_index(short index, int hash_a, int hash_b, int locale)
{
int v4; // ecx
signed int v5; // eax
signed int v6; // edx
_HASHENTRY *v7; // ecx
int v8; // edi
int v10; // [esp+Ch] [ebp-8h]
int i; // [esp+10h] [ebp-4h]
v4 = index & 0x7FF;
v10 = hash_a;
v5 = 2048;
for ( i = v4; ; i = (i + 1) & 0x7FF )
{
v7 = &sgpHashTbl[v4];
v8 = v7->block;
if ( v8 == -1 )
return -1;
v6 = v5--;
if ( !v6 )
return -1;
if ( v7->hashcheck[0] == v10 && v7->hashcheck[1] == hash_b && v7->lcid == locale && v8 != -2 )
break;
v4 = (i + 1) & 0x7FF;
}
return i;
}
//----- (0043B1BD) --------------------------------------------------------
void __fastcall mpqapi_remove_hash_entries(bool (__stdcall *fnGetName)(int, char *))
{
bool (__stdcall *v1)(int, char *); // edi
signed int v2; // esi
int i; // eax
int v4; // eax
char v5[260]; // [esp+8h] [ebp-104h]
v1 = fnGetName;
v2 = 1;
for ( i = fnGetName(0, v5); i; i = v1(v4, v5) )
{
mpqapi_remove_hash_entry(v5);
v4 = v2++;
}
}
//----- (0043B1F8) --------------------------------------------------------
bool __fastcall mpqapi_write_file(char *pszName, char *pbData, int dwLen)
{
char *v3; // edi
char *v4; // esi
_BLOCKENTRY *v5; // eax
v3 = pbData;
v4 = pszName;
save_archive_modified = 1;
mpqapi_remove_hash_entry(pszName);
v5 = mpqapi_add_file(v4, 0, 0);
if ( mpqapi_write_file_contents(v4, v3, dwLen, v5) )
return 1;
mpqapi_remove_hash_entry(v4);
return 0;
}
// 65AB0C: using guessed type int save_archive_modified;
//----- (0043B23D) --------------------------------------------------------
_BLOCKENTRY *__fastcall mpqapi_add_file(char *pszName, _BLOCKENTRY *pBlk, int block_index)
{
char *v3; // edi
short v4; // si
int v5; // ebx
signed int v6; // edx
int v7; // esi
int v8; // ecx
int v9; // esi
int v11; // [esp+Ch] [ebp-8h]
_BLOCKENTRY *v12; // [esp+10h] [ebp-4h]
v12 = pBlk;
v3 = pszName;
v4 = encrypt_hash(pszName, 0);
v5 = encrypt_hash(v3, 1);
v11 = encrypt_hash(v3, 2);
if ( mpqapi_get_hash_index(v4, v5, v11, 0) != -1 )
TermMsg("Hash collision between \"%s\" and existing file\n", v3);
v6 = 2048;
v7 = v4 & 0x7FF;
while ( 1 )
{
--v6;
v8 = sgpHashTbl[v7].block;
if ( v8 == -1 || v8 == -2 )
break;
v7 = (v7 + 1) & 0x7FF;
if ( !v6 )
{
v6 = -1;
break;
}
}
if ( v6 < 0 )
TermMsg("Out of hash space");
if ( !v12 )
v12 = mpqapi_new_block(&block_index);
v9 = v7;
sgpHashTbl[v9].hashcheck[0] = v5;
sgpHashTbl[v9].hashcheck[1] = v11;
sgpHashTbl[v9].lcid = 0;
sgpHashTbl[v9].block = block_index;
return v12;
}
//----- (0043B317) --------------------------------------------------------
bool __fastcall mpqapi_write_file_contents(char *pszName, char *pbData, int dwLen, _BLOCKENTRY *pBlk)
{
char *v4; // esi
char *v5; // eax
unsigned int destsize; // ebx
char *v7; // eax
unsigned int v8; // esi
_BLOCKENTRY *v9; // edi
int v10; // eax
signed int v11; // eax
unsigned int v13; // eax
unsigned int v14; // eax
int v15; // ecx
int size; // [esp+Ch] [ebp-10h]
char *v17; // [esp+10h] [ebp-Ch]
int v18; // [esp+14h] [ebp-8h]
DWORD nNumberOfBytesToWrite; // [esp+18h] [ebp-4h]
v4 = pszName;
v17 = pbData;
v5 = strchr(pszName, ':');
destsize = 0;
while ( v5 )
{
v4 = v5 + 1;
v5 = strchr(v5 + 1, ':');
}
while ( 1 )
{
v7 = strchr(v4, '\\');
if ( !v7 )
break;
v4 = v7 + 1;
}
encrypt_hash(v4, 3);
v8 = dwLen;
v9 = pBlk;
size = 4 * ((unsigned int)(dwLen + 4095) >> 12) + 4;
nNumberOfBytesToWrite = 4 * ((unsigned int)(dwLen + 4095) >> 12) + 4;
v10 = mpqapi_find_free_block(size + dwLen, &pBlk->sizealloc);
v9->offset = v10;
v9->sizefile = v8;
v9->flags = 0x80000100;
if ( SetFilePointer(sghArchive, v10, NULL, FILE_BEGIN) == -1 )
return 0;
pBlk = 0;
v18 = 0;
while ( v8 )
{
v11 = 0;
do
mpq_buf[v11++] -= 86;
while ( v11 < 4096 );
dwLen = v8;
if ( v8 >= 0x1000 )
dwLen = 4096;
memcpy(mpq_buf, v17, dwLen);
v17 += dwLen;
dwLen = encrypt_compress(mpq_buf, dwLen);
if ( !v18 )
{
nNumberOfBytesToWrite = size;
pBlk = (_BLOCKENTRY *)DiabloAllocPtr(size);
memset(pBlk, 0, nNumberOfBytesToWrite);
if ( !WriteFile(sghArchive, pBlk, nNumberOfBytesToWrite, &nNumberOfBytesToWrite, 0) )
goto LABEL_25;
destsize += nNumberOfBytesToWrite;
}
*(&pBlk->offset + v18) = destsize;
if ( !WriteFile(sghArchive, mpq_buf, dwLen, (LPDWORD)&dwLen, 0) )
goto LABEL_25;
++v18;
if ( v8 <= 0x1000 )
v8 = 0;
else
v8 -= 4096;
destsize += dwLen;
}
*(&pBlk->offset + v18) = destsize;
if ( SetFilePointer(sghArchive, -destsize, NULL, FILE_CURRENT) == -1
|| !WriteFile(sghArchive, pBlk, nNumberOfBytesToWrite, &nNumberOfBytesToWrite, 0)
|| SetFilePointer(sghArchive, destsize - nNumberOfBytesToWrite, NULL, FILE_CURRENT) == -1 )
{
LABEL_25:
if ( pBlk )
mem_free_dbg(pBlk);
return 0;
}
mem_free_dbg(pBlk);
v13 = v9->sizealloc;
if ( destsize < v13 )
{
v14 = v13 - destsize;
if ( v14 >= 0x400 )
{
v15 = destsize + v9->offset;
v9->sizealloc = destsize;
mpqapi_alloc_block(v15, v14);
}
}
return 1;
}
//----- (0043B51C) --------------------------------------------------------
int __fastcall mpqapi_find_free_block(int size, int *block_size)
{
_BLOCKENTRY *v2; // eax
signed int v3; // esi
int result; // eax
int v5; // esi
bool v6; // zf
v2 = sgpBlockTbl;
v3 = 2048;
while ( 1 )
{
--v3;
if ( v2->offset )
{
if ( !v2->flags && !v2->sizefile && v2->sizealloc >= (unsigned int)size )
break;
}
++v2;
if ( !v3 )
{
*block_size = size;
result = sgdwMpqOffset;
sgdwMpqOffset += size;
return result;
}
}
v5 = v2->offset;
*block_size = size;
v2->offset += size;
v6 = v2->sizealloc == size;
v2->sizealloc -= size;
if ( v6 )
memset(v2, 0, 0x10u);
return v5;
}
//----- (0043B570) --------------------------------------------------------
void __fastcall mpqapi_rename(char *pszOld, char *pszNew)
{
char *v2; // esi
int v3; // eax
_HASHENTRY *v4; // eax
int v5; // ST00_4
_BLOCKENTRY *v6; // edx
v2 = pszNew;
v3 = mpqapi_get_hash_index_of_path(pszOld);
if ( v3 != -1 )
{
v4 = &sgpHashTbl[v3];
v5 = v4->block;
v6 = &sgpBlockTbl[v5];
v4->block = -2;
mpqapi_add_file(v2, v6, v5);
save_archive_modified = 1;
}
}
// 65AB0C: using guessed type int save_archive_modified;
//----- (0043B5AF) --------------------------------------------------------
bool __fastcall mpqapi_has_file(char *pszName)
{
return mpqapi_get_hash_index_of_path(pszName) != -1;
}
//----- (0043B5BF) --------------------------------------------------------
bool __fastcall mpqapi_open_archive(char *pszArchive, bool hidden, int dwChar) // OpenMPQ
{
char *v3; // ebp
BOOL v4; // esi
DWORD v6; // edi
int v8; // eax
int v10; // eax
char *lpFileName; // [esp+10h] [ebp-70h]
DWORD dwTemp; // [esp+14h] [ebp-6Ch]
TMPQHeader fhdr; // [esp+18h] [ebp-68h]
v3 = pszArchive;
v4 = hidden;
lpFileName = pszArchive;
encrypt_init_lookup_table();
if ( !mpqapi_set_hidden(v3, v4) )
return 0;
v6 = (unsigned char)gbMaxPlayers > 1u ? FILE_FLAG_WRITE_THROUGH : 0;
save_archive_open = 0;
sghArchive = CreateFileA(v3, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, v6, NULL);
if ( sghArchive == (HANDLE)-1 )
{
sghArchive = CreateFileA(lpFileName, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, v6 | (v4 != 0 ? FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN : 0), NULL);
if ( sghArchive == (HANDLE)-1 )
return 0;
save_archive_open = 1;
save_archive_modified = 1;
}
if ( !sgpBlockTbl || !sgpHashTbl )
{
memset(&fhdr, 0, 0x68u);
if ( !mpqapi_parse_archive_header(&fhdr, &sgdwMpqOffset) )
{
LABEL_15:
mpqapi_close_archive(lpFileName, 1, dwChar);
return 0;
}
sgpBlockTbl = (_BLOCKENTRY *)DiabloAllocPtr(0x8000);
memset(sgpBlockTbl, 0, 0x8000u);
if ( fhdr.dwBlockTableSize )
{
if ( SetFilePointer(sghArchive, 104, NULL, FILE_BEGIN) == -1
|| !ReadFile(sghArchive, sgpBlockTbl, 0x8000u, &dwTemp, NULL) )
{
goto LABEL_15;
}
v8 = encrypt_hash("(block table)", 3);
encrypt_decrypt_block(sgpBlockTbl, 0x8000, v8);
}
sgpHashTbl = (_HASHENTRY *)DiabloAllocPtr(0x8000);
memset(sgpHashTbl, 255, 0x8000u);
if ( fhdr.dwHashTableSize )
{
if ( SetFilePointer(sghArchive, 32872, NULL, FILE_BEGIN) == -1
|| !ReadFile(sghArchive, sgpHashTbl, 0x8000u, &dwTemp, NULL) )
{
goto LABEL_15;
}
v10 = encrypt_hash("(hash table)", 3);
encrypt_decrypt_block(sgpHashTbl, 0x8000, v10);
}
}
return 1;
}
// 65AB0C: using guessed type int save_archive_modified;
// 65AB14: using guessed type char save_archive_open;
// 679660: using guessed type char gbMaxPlayers;
//----- (0043B791) --------------------------------------------------------
bool __fastcall mpqapi_parse_archive_header(TMPQHeader *pHdr, int *pdwNextFileStart) // ParseMPQHeader
{
int *v2; // ebp
TMPQHeader *v3; // esi
DWORD v4; // eax
DWORD v5; // edi
DWORD NumberOfBytesRead; // [esp+10h] [ebp-4h]
v2 = pdwNextFileStart;
v3 = pHdr;
v4 = GetFileSize(sghArchive, 0);
v5 = v4;
*v2 = v4;
if ( v4 == -1
|| v4 < 0x68
|| !ReadFile(sghArchive, v3, 0x68u, &NumberOfBytesRead, NULL)
|| NumberOfBytesRead != 104
|| v3->dwID != '\x1AQPM'
|| v3->dwHeaderSize != 32
|| v3->wFormatVersion > 0u
|| v3->wSectorSize != 3
|| v3->dwArchiveSize != v5
|| v3->dwHashTablePos != 32872
|| v3->dwBlockTablePos != 104
|| v3->dwHashTableSize != 2048
|| v3->dwBlockTableSize != 2048 )
{
if ( SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1 || !SetEndOfFile(sghArchive) )
return 0;
memset(v3, 0, 0x68u);
v3->dwID = '\x1AQPM';
v3->dwHeaderSize = 32;
v3->wSectorSize = 3;
v3->wFormatVersion = 0;
*v2 = 0x10068;
save_archive_modified = 1;
save_archive_open = 1;
}
return 1;
}
// 65AB0C: using guessed type int save_archive_modified;
// 65AB14: using guessed type char save_archive_open;
//----- (0043B882) --------------------------------------------------------
void __fastcall mpqapi_close_archive(char *pszArchive, bool bFree, int dwChar) // CloseMPQ
{
char *v3; // esi
_BLOCKENTRY *v4; // ecx
_HASHENTRY *v5; // ecx
v3 = pszArchive;
if ( bFree )
{
v4 = sgpBlockTbl;
sgpBlockTbl = 0;
mem_free_dbg(v4);
v5 = sgpHashTbl;
sgpHashTbl = 0;
mem_free_dbg(v5);
}
if ( sghArchive != (HANDLE)-1 )
{
CloseHandle(sghArchive);
sghArchive = (HANDLE)-1;
}
if ( save_archive_modified )
{
save_archive_modified = 0;
mpqapi_store_modified_time(v3, dwChar);
}
if ( save_archive_open )
{
save_archive_open = 0;
mpqapi_store_creation_time(v3, dwChar);
}
}
// 65AB0C: using guessed type int save_archive_modified;
// 65AB14: using guessed type char save_archive_open;
//----- (0043B8FD) --------------------------------------------------------
void __fastcall mpqapi_store_modified_time(char *pszArchive, int dwChar)
{
int v2; // esi
char *v3; // ebx
HANDLE v4; // eax
int v5; // esi
struct _WIN32_FIND_DATAA FindFileData; // [esp+8h] [ebp-1E0h]
char dst[160]; // [esp+148h] [ebp-A0h]
v2 = dwChar;
v3 = pszArchive;
if ( gbMaxPlayers != 1 )
{
mpqapi_reg_load_modification_time(dst, 160);
v4 = FindFirstFileA(v3, &FindFileData);
if ( v4 != (HANDLE)-1 )
{
FindClose(v4);
v5 = 16 * v2;
*(_DWORD *)&dst[v5 + 8] = FindFileData.ftLastWriteTime.dwLowDateTime;
*(_DWORD *)&dst[v5 + 12] = FindFileData.ftLastWriteTime.dwHighDateTime;
mpqapi_reg_store_modification_time(dst, 160);
}
}
}
// 679660: using guessed type char gbMaxPlayers;
//----- (0043B970) --------------------------------------------------------
void __fastcall mpqapi_flush_and_close(char *pszArchive, bool bFree, int dwChar)
{
if ( sghArchive != (HANDLE)-1 )
{
if ( save_archive_modified )
{
if ( mpqapi_can_seek() )
{
if ( mpqapi_write_header() )
{
if ( mpqapi_write_block_table() )
mpqapi_write_hash_table();
}
}
}
}
mpqapi_close_archive(pszArchive, bFree, dwChar);
}
// 65AB0C: using guessed type int save_archive_modified;
//----- (0043B9CA) --------------------------------------------------------
bool __cdecl mpqapi_write_header() // WriteMPQHeader
{
bool result; // al
TMPQHeader fhdr; // [esp+8h] [ebp-6Ch]
DWORD NumberOfBytesWritten; // [esp+70h] [ebp-4h]
memset(&fhdr, 0, 0x68u);
fhdr.dwID = '\x1AQPM';
fhdr.dwHeaderSize = 32;
fhdr.dwArchiveSize = GetFileSize(sghArchive, 0);
fhdr.wFormatVersion = 0;
fhdr.wSectorSize = 3;
fhdr.dwHashTablePos = 32872;
fhdr.dwBlockTablePos = 104;
fhdr.dwHashTableSize = 2048;
fhdr.dwBlockTableSize = 2048;
if ( SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) != -1 && WriteFile(sghArchive, &fhdr, 0x68u, &NumberOfBytesWritten, 0) )
result = NumberOfBytesWritten == 104;
else
result = 0;
return result;
}
//----- (0043BA60) --------------------------------------------------------
bool __cdecl mpqapi_write_block_table()
{
int v1; // eax
BOOL v2; // ebx
int v3; // eax
DWORD NumberOfBytesWritten; // [esp+4h] [ebp-4h]
if ( SetFilePointer(sghArchive, 104, NULL, FILE_BEGIN) == -1 )
return 0;
v1 = encrypt_hash("(block table)", 3);
encrypt_encrypt_block(sgpBlockTbl, 0x8000, v1);
v2 = WriteFile(sghArchive, sgpBlockTbl, 0x8000u, &NumberOfBytesWritten, 0);
v3 = encrypt_hash("(block table)", 3);
encrypt_decrypt_block(sgpBlockTbl, 0x8000, v3);
return v2 && NumberOfBytesWritten == 0x8000;
}
//----- (0043BAEB) --------------------------------------------------------
bool __cdecl mpqapi_write_hash_table()
{
int v1; // eax
BOOL v2; // ebx
int v3; // eax
DWORD NumberOfBytesWritten; // [esp+4h] [ebp-4h]
if ( SetFilePointer(sghArchive, 32872, NULL, FILE_BEGIN) == -1 )
return 0;
v1 = encrypt_hash("(hash table)", 3);
encrypt_encrypt_block(sgpHashTbl, 0x8000, v1);
v2 = WriteFile(sghArchive, sgpHashTbl, 0x8000u, &NumberOfBytesWritten, 0);
v3 = encrypt_hash("(hash table)", 3);
encrypt_decrypt_block(sgpHashTbl, 0x8000, v3);
return v2 && NumberOfBytesWritten == 0x8000;
}
//----- (0043BB79) --------------------------------------------------------
bool __cdecl mpqapi_can_seek()
{
bool result; // al
if ( SetFilePointer(sghArchive, sgdwMpqOffset, NULL, FILE_BEGIN) == -1 )
result = 0;
else
result = SetEndOfFile(sghArchive);
return result;
}