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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
|
/*****************************************************************************/
/* FileStream.cpp Copyright (c) Ladislav Zezula 2010 */
/*---------------------------------------------------------------------------*/
/* File stream support for StormLib */
/* */
/* Windows support: Written by Ladislav Zezula */
/* Mac support: Written by Sam Wilkins */
/* Linux support: Written by Sam Wilkins and Ivan Komissarov */
/* Big-endian: Written & debugged by Sam Wilkins */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 11.06.10 1.00 Lad Derived from StormPortMac.cpp and StormPortLinux.cpp */
/*****************************************************************************/
#define __STORMLIB_SELF__
#include "StormLib.h"
#include "StormCommon.h"
//-----------------------------------------------------------------------------
// Local defines
#ifndef INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE ((HANDLE)-1)
#endif
#ifdef _MSC_VER
#pragma warning(disable: 4800) // 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
#endif
//-----------------------------------------------------------------------------
// Local structures
// Structure describing the PART file header
typedef struct _PART_FILE_HEADER
{
DWORD PartialVersion; // Always set to 2
char GameBuildNumber[8]; // Minimum build number of the game that can use this MPQ
DWORD Unknown0C;
DWORD Unknown10;
DWORD Unknown14; // Often contains 0x1C (size of the rest of the header ?)
DWORD Unknown18;
DWORD ZeroValue1C; // Seems to always be zero
DWORD ZeroValue20; // Seems to always be zero
DWORD ZeroValue24; // Seems to always be zero
DWORD FileSizeLo; // Low 32 bits of the file size
DWORD FileSizeHi; // High 32 bits of the file size
DWORD BlockSize; // Size of one file block, in bytes
} PART_FILE_HEADER, *PPART_FILE_HEADER;
// Structure describing the block-to-file map entry
typedef struct _PART_FILE_MAP_ENTRY
{
DWORD Flags; // 3 = the block is present in the file
DWORD BlockOffsLo; // Low 32 bits of the block position in the file
DWORD BlockOffsHi; // High 32 bits of the block position in the file
DWORD Unknown0C;
DWORD Unknown10;
} PART_FILE_MAP_ENTRY, *PPART_FILE_MAP_ENTRY;
struct TPartFileStream : public TFileStream
{
ULONGLONG VirtualSize; // Virtual size of the file
ULONGLONG VirtualPos; // Virtual position in the file
DWORD BlockCount; // Number of file blocks. Used by partial file stream
DWORD BlockSize; // Size of one block. Used by partial file stream
PART_FILE_MAP_ENTRY PartMap[1]; // File map, variable length
};
#define MPQE_CHUNK_SIZE 0x40 // Size of one chunk to be decrypted
struct TEncryptedStream : public TFileStream
{
BYTE Key[MPQE_CHUNK_SIZE]; // File key
};
static bool IsPartHeader(PPART_FILE_HEADER pPartHdr)
{
// Version number must be 2
if(pPartHdr->PartialVersion == 2)
{
// GameBuildNumber must be anm ASCII number
if(isdigit(pPartHdr->GameBuildNumber[0]) && isdigit(pPartHdr->GameBuildNumber[1]) && isdigit(pPartHdr->GameBuildNumber[2]))
{
// Block size must be power of 2
if((pPartHdr->BlockSize & (pPartHdr->BlockSize - 1)) == 0)
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Non-Windows support for LastError
#ifndef PLATFORM_WINDOWS
static int nLastError = ERROR_SUCCESS;
int GetLastError()
{
return nLastError;
}
void SetLastError(int nError)
{
nLastError = nError;
}
#endif
//-----------------------------------------------------------------------------
// Local functions - platform-specific functions
#ifndef PLATFORM_LITTLE_ENDIAN
void ConvertPartHeader(void * partHeader)
{
PPART_FILE_HEADER theHeader = (PPART_FILE_HEADER)partHeader;
theHeader->PartialVersion = SwapUInt32(theHeader->PartialVersion);
theHeader->Unknown0C = SwapUInt32(theHeader->Unknown0C);
theHeader->Unknown10 = SwapUInt32(theHeader->Unknown10);
theHeader->Unknown14 = SwapUInt32(theHeader->Unknown14);
theHeader->Unknown18 = SwapUInt32(theHeader->Unknown18);
theHeader->Unknown1C = SwapUInt32(theHeader->Unknown1C);
theHeader->Unknown20 = SwapUInt32(theHeader->Unknown20);
theHeader->ZeroValue = SwapUInt32(theHeader->ZeroValue);
theHeader->FileSizeLo = SwapUInt32(theHeader->FileSizeLo);
theHeader->FileSizeHi = SwapUInt32(theHeader->FileSizeHi);
theHeader->BlockSize = SwapUInt32(theHeader->BlockSize);
}
#endif
#ifdef PLATFORM_MAC
static void ConvertUTCDateTimeToFileTime(const UTCDateTimePtr inTime, ULONGLONG * pFT)
{
UInt64 intTime = ((UInt64)inTime->highSeconds << 32) + inTime->lowSeconds;
intTime *= 10000000;
intTime += 0x0153b281e0fb4000ull;
*pFT = intTime;
}
static OSErr FSOpenDFCompat(FSRef *ref, char permission, short *refNum)
{
HFSUniStr255 forkName;
OSErr theErr;
Boolean isFolder, wasChanged;
theErr = FSResolveAliasFile(ref, true, &isFolder, &wasChanged);
if (theErr != noErr)
{
return theErr;
}
FSGetDataForkName(&forkName);
#ifdef PLATFORM_64BIT
theErr = FSOpenFork(ref, forkName.length, forkName.unicode, permission, (FSIORefNum *)refNum);
#else
theErr = FSOpenFork(ref, forkName.length, forkName.unicode, permission, refNum);
#endif
return theErr;
}
#endif
#ifdef PLATFORM_LINUX
// time_t is number of seconds since 1.1.1970, UTC.
// 1 second = 10000000 (decimal) in FILETIME
static void ConvertTimeTToFileTime(ULONGLONG * pFileTime, time_t crt_time)
{
// Set the start to 1.1.1970 00:00:00
*pFileTime = 0x019DB1DED53E8000ULL + (10000000 * crt_time);
}
#endif
static HANDLE CreateNewFile(
const TCHAR * szFileName) // Name of the file to open
{
HANDLE hFile = INVALID_HANDLE_VALUE; // Pre-set the file handle to INVALID_HANDLE_VALUE
#ifdef PLATFORM_WINDOWS
{
DWORD dwShareMode = FILE_SHARE_READ;
if(dwGlobalFlags & SFILE_FLAG_ALLOW_WRITE_SHARE)
dwShareMode |= FILE_SHARE_WRITE;
hFile = CreateFile(szFileName,
GENERIC_READ | GENERIC_WRITE,
dwShareMode,
NULL,
CREATE_ALWAYS,
0,
NULL);
}
#endif
#ifdef PLATFORM_MAC
{
FSRef theParentRef;
FSRef theFileRef;
OSErr theErr;
short fileRef;
theErr = FSPathMakeRef((const UInt8 *)szFileName, &theFileRef, NULL);
if (theErr == noErr)
FSDeleteObject(&theFileRef);
// Create the FSRef for the parent directory.
UInt8 folderName[MAX_PATH];
memset(&theFileRef, 0, sizeof(FSRef));
CFStringRef filePathCFString = CFStringCreateWithCString(NULL, szFileName, kCFStringEncodingUTF8);
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, filePathCFString, kCFURLPOSIXPathStyle, false);
CFURLRef folderURL = CFURLCreateCopyDeletingLastPathComponent(NULL, fileURL);
CFURLGetFileSystemRepresentation(folderURL, true, folderName, MAX_PATH);
theErr = FSPathMakeRef(folderName, &theParentRef, NULL);
CFRelease(fileURL);
CFRelease(folderURL);
if (theErr != noErr)
{
nLastError = theErr;
return INVALID_HANDLE_VALUE;
}
// Create the file
UniChar unicodeFileName[256];
fileURL = CFURLCreateWithFileSystemPath(NULL, filePathCFString, kCFURLPOSIXPathStyle, false);
CFStringRef fileNameCFString = CFURLCopyLastPathComponent(fileURL);
CFStringGetCharacters(fileNameCFString, CFRangeMake(0, CFStringGetLength(fileNameCFString)),
unicodeFileName);
theErr = FSCreateFileUnicode(&theParentRef, CFStringGetLength(fileNameCFString), unicodeFileName,
kFSCatInfoNone, NULL, &theFileRef, NULL);
CFRelease(fileNameCFString);
CFRelease(filePathCFString);
CFRelease(fileURL);
if (theErr != noErr)
{
nLastError = theErr;
return INVALID_HANDLE_VALUE;
}
theErr = FSOpenDFCompat(&theFileRef, fsRdWrPerm, &fileRef);
if(theErr != noErr)
{
nLastError = theErr;
return INVALID_HANDLE_VALUE;
}
hFile = (HANDLE)(int)fileRef;
}
#endif
#ifdef PLATFORM_LINUX
{
intptr_t handle;
handle = open(szFileName, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(handle == -1)
{
nLastError = errno;
return INVALID_HANDLE_VALUE;
}
hFile = (HANDLE)handle;
}
#endif
// Return the file handle
return hFile;
}
static HANDLE OpenExistingFile(
const TCHAR * szFileName, // Name of the file to open
bool bWriteAccess) // false = read-only, true = read/write
{
HANDLE hFile = INVALID_HANDLE_VALUE; // Pre-set the file handle to INVALID_HANDLE_VALUE
#ifdef PLATFORM_WINDOWS
{
DWORD dwShareMode = FILE_SHARE_READ;
if(dwGlobalFlags & SFILE_FLAG_ALLOW_WRITE_SHARE)
dwShareMode |= FILE_SHARE_WRITE;
hFile = CreateFile(szFileName,
bWriteAccess ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_READ,
dwShareMode,
NULL,
OPEN_EXISTING,
0,
NULL);
}
#endif
#ifdef PLATFORM_MAC
{
FSRef theFileRef;
OSErr theErr;
short fileRef;
char permission = bWriteAccess ? fsRdWrPerm : fsRdPerm;
theErr = FSPathMakeRef((const UInt8 *)szFileName, &theFileRef, NULL);
if(theErr != noErr)
{
nLastError = theErr;
return INVALID_HANDLE_VALUE;
}
theErr = FSOpenDFCompat(&theFileRef, permission, &fileRef);
if (theErr != noErr)
{
nLastError = theErr;
return INVALID_HANDLE_VALUE;
}
hFile = (HANDLE)(int)fileRef;
}
#endif
#ifdef PLATFORM_LINUX
{
int oflag = bWriteAccess ? O_RDWR : O_RDONLY;
intptr_t handle;
handle = open(szFileName, oflag | O_LARGEFILE);
if(handle == -1)
{
nLastError = errno;
return INVALID_HANDLE_VALUE;
}
hFile = (HANDLE)handle;
}
#endif
// Return the file handle
return hFile;
}
static void CloseTheFile(HANDLE hFile)
{
#ifdef PLATFORM_WINDOWS
CloseHandle(hFile);
#endif
#ifdef PLATFORM_MAC
FSCloseFork((short)(long)hFile);
#endif
#ifdef PLATFORM_LINUX
close((intptr_t)hFile);
#endif
}
/**
* Renames a file to another name.
* Note that the "szNewFile" file usually exists when this function is called,
* so the function must deal with it properly
*/
static bool RenameFile(const TCHAR * szExistingFile, const TCHAR * szNewFile)
{
#ifdef PLATFORM_WINDOWS
// Delete the original stream file. Don't check the result value,
// because if the file doesn't exist, it would fail
DeleteFile(szNewFile);
// Rename the new file to the old stream's file
return (bool)MoveFile(szExistingFile, szNewFile);
#endif
#ifdef PLATFORM_MAC
OSErr theErr;
FSRef fromFileRef;
FSRef toFileRef;
if (FSPathMakeRef((const UInt8 *)szNewFile, &toFileRef, NULL) == noErr)
FSDeleteObject(&toFileRef);
// Get the path to the old file
theErr = FSPathMakeRef((const UInt8 *)szExistingFile, &fromFileRef, NULL);
if (theErr != noErr)
{
nLastError = theErr;
return false;
}
// Get a CFString for the new file name
CFStringRef newFileNameCFString = CFStringCreateWithCString(NULL, szNewFile, kCFStringEncodingUTF8);
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, newFileNameCFString, kCFURLPOSIXPathStyle, false);
CFRelease(newFileNameCFString);
newFileNameCFString = CFURLCopyLastPathComponent(fileURL);
CFRelease(fileURL);
// Convert CFString to Unicode and rename the file
UniChar unicodeFileName[256];
CFStringGetCharacters(newFileNameCFString, CFRangeMake(0, CFStringGetLength(newFileNameCFString)),
unicodeFileName);
theErr = FSRenameUnicode(&fromFileRef, CFStringGetLength(newFileNameCFString), unicodeFileName,
kTextEncodingUnknown, NULL);
if (theErr != noErr)
{
CFRelease(newFileNameCFString);
nLastError = theErr;
return false;
}
CFRelease(newFileNameCFString);
return true;
#endif
#ifdef PLATFORM_LINUX
// "rename" on Linux also works if the target file exists
if(rename(szExistingFile, szNewFile) == -1)
{
nLastError = errno;
return false;
}
return true;
#endif
}
//-----------------------------------------------------------------------------
// Stream functions - normal file stream
static bool File_GetPos(
TFileStream * pStream, // Pointer to an open stream
ULONGLONG & ByteOffset) // Pointer to file byte offset
{
ByteOffset = pStream->RawFilePos;
return true;
}
static bool File_Read(
TFileStream * pStream, // Pointer to an open stream
ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
void * pvBuffer, // Pointer to data to be read
DWORD dwBytesToRead) // Number of bytes to read from the file
{
DWORD dwBytesRead = 0; // Must be set by platform-specific code
// If the byte offset is not entered, use the current position
if(pByteOffset == NULL)
pByteOffset = &pStream->RawFilePos;
#ifdef PLATFORM_WINDOWS
{
// If the byte offset is different from the current file position,
// we have to update the file position
if(*pByteOffset != pStream->RawFilePos)
{
LONG ByteOffsetHi = (LONG)(*pByteOffset >> 32);
LONG ByteOffsetLo = (LONG)(*pByteOffset);
SetFilePointer(pStream->hFile, ByteOffsetLo, &ByteOffsetHi, FILE_BEGIN);
pStream->RawFilePos = *pByteOffset;
}
// Read the data
if(dwBytesToRead != 0)
{
if(!ReadFile(pStream->hFile, pvBuffer, dwBytesToRead, &dwBytesRead, NULL))
return false;
}
}
#endif
#ifdef PLATFORM_MAC
{
ByteCount nBytesToRead = (ByteCount)dwBytesToRead;
ByteCount nBytesRead = 0;
OSErr theErr;
// If the byte offset is different from the current file position,
// we have to update the file position
if(*pByteOffset != pStream->RawFilePos)
{
FSSetForkPosition((short)(long)pStream->hFile, fsFromStart, (SInt64)(*pByteOffset));
pStream->RawFilePos = *pByteOffset;
}
// Read the data
if(nBytesToRead != 0)
{
theErr = FSReadFork((short)(long)pStream->hFile, fsAtMark, 0, nBytesToRead, pvBuffer, &nBytesRead);
if (theErr != noErr && theErr != eofErr)
{
nLastError = theErr;
return false;
}
dwBytesRead = (DWORD)nBytesRead;
}
}
#endif
#ifdef PLATFORM_LINUX
{
ssize_t bytes_read;
// If the byte offset is different from the current file position,
// we have to update the file position
if(*pByteOffset != pStream->RawFilePos)
{
lseek64((intptr_t)pStream->hFile, (off64_t)(*pByteOffset), SEEK_SET);
pStream->RawFilePos = *pByteOffset;
}
// Perform the read operation
if(dwBytesToRead != 0)
{
bytes_read = read((intptr_t)pStream->hFile, pvBuffer, (size_t)dwBytesToRead);
if(bytes_read == -1)
{
nLastError = errno;
return false;
}
dwBytesRead = (DWORD)(size_t)bytes_read;
}
}
#endif
// Increment the current file position by number of bytes read
// If the number of bytes read doesn't match to required amount, return false
pStream->RawFilePos = *pByteOffset + dwBytesRead;
if(dwBytesRead != dwBytesToRead)
SetLastError(ERROR_HANDLE_EOF);
return (dwBytesRead == dwBytesToRead);
}
/**
* \a pStream Pointer to an open stream
* \a pByteOffset Pointer to file byte offset. If NULL, writes to current position
* \a pvBuffer Pointer to data to be written
* \a dwBytesToWrite Number of bytes to write to the file
*/
static bool File_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite)
{
DWORD dwBytesWritten = 0; // Must be set by platform-specific code
// If the byte offset is not entered, use the current position
if(pByteOffset == NULL)
pByteOffset = &pStream->RawFilePos;
#ifdef PLATFORM_WINDOWS
{
// If the byte offset is different from the current file position,
// we have to update the file position
if(*pByteOffset != pStream->RawFilePos)
{
LONG ByteOffsetHi = (LONG)(*pByteOffset >> 32);
LONG ByteOffsetLo = (LONG)(*pByteOffset);
SetFilePointer(pStream->hFile, ByteOffsetLo, &ByteOffsetHi, FILE_BEGIN);
pStream->RawFilePos = *pByteOffset;
}
// Read the data
if(!WriteFile(pStream->hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, NULL))
return false;
}
#endif
#ifdef PLATFORM_MAC
{
ByteCount nBytesToWrite = (ByteCount)dwBytesToWrite;
ByteCount nBytesWritten = 0;
OSErr theErr;
// If the byte offset is different from the current file position,
// we have to update the file position
if(*pByteOffset != pStream->RawFilePos)
{
FSSetForkPosition((short)(long)pStream->hFile, fsFromStart, (SInt64)(*pByteOffset));
pStream->RawFilePos = *pByteOffset;
}
theErr = FSWriteFork((short)(long)pStream->hFile, fsAtMark, 0, nBytesToWrite, pvBuffer, &nBytesWritten);
if (theErr != noErr)
{
nLastError = theErr;
return false;
}
dwBytesWritten = (DWORD)nBytesWritten;
}
#endif
#ifdef PLATFORM_LINUX
{
ssize_t bytes_written;
// If the byte offset is different from the current file position,
// we have to update the file position
if(*pByteOffset != pStream->RawFilePos)
{
lseek64((intptr_t)pStream->hFile, (off64_t)(*pByteOffset), SEEK_SET);
pStream->RawFilePos = *pByteOffset;
}
// Perform the read operation
bytes_written = write((intptr_t)pStream->hFile, pvBuffer, (size_t)dwBytesToWrite);
if(bytes_written == -1)
{
nLastError = errno;
return false;
}
dwBytesWritten = (DWORD)(size_t)bytes_written;
}
#endif
// Increment the current file position by number of bytes read
pStream->RawFilePos = *pByteOffset + dwBytesWritten;
if(dwBytesWritten != dwBytesToWrite)
SetLastError(ERROR_DISK_FULL);
return (dwBytesWritten == dwBytesToWrite);
}
static bool File_GetSize(
TFileStream * pStream, // Pointer to an open stream
ULONGLONG & FileSize) // Pointer where to store file size
{
#ifdef PLATFORM_WINDOWS
DWORD FileSizeHi = 0;
DWORD FileSizeLo;
FileSizeLo = GetFileSize(pStream->hFile, &FileSizeHi);
if(FileSizeLo == INVALID_FILE_SIZE && GetLastError() != ERROR_SUCCESS)
return false;
FileSize = MAKE_OFFSET64(FileSizeHi, FileSizeLo);
return true;
#endif
#ifdef PLATFORM_MAC
SInt64 fileLength = 0;
OSErr theErr;
theErr = FSGetForkSize((short)(long)pStream->hFile, &fileLength);
if(theErr != noErr)
{
nLastError = theErr;
return false;
}
FileSize = (ULONGLONG)fileLength;
return true;
#endif
#ifdef PLATFORM_LINUX
struct stat64 fileinfo;
if(fstat64((intptr_t)pStream->hFile, &fileinfo) == -1)
{
nLastError = errno;
return false;
}
FileSize = (ULONGLONG)fileinfo.st_size;
return true;
#endif
}
/**
* \a pStream Pointer to an open stream
* \a NewFileSize New size of the file
*/
static bool File_SetSize(TFileStream * pStream, ULONGLONG NewFileSize)
{
#ifdef PLATFORM_WINDOWS
{
LONG FileSizeHi = (LONG)(NewFileSize >> 32);
LONG FileSizeLo = (LONG)(NewFileSize);
DWORD dwNewPos;
bool bResult;
// Set the position at the new file size
dwNewPos = SetFilePointer(pStream->hFile, FileSizeLo, &FileSizeHi, FILE_BEGIN);
if(dwNewPos == INVALID_SET_FILE_POINTER && GetLastError() != ERROR_SUCCESS)
return false;
// Set the current file pointer as the end of the file
bResult = (bool)SetEndOfFile(pStream->hFile);
// Restore the file position
FileSizeHi = (LONG)(pStream->RawFilePos >> 32);
FileSizeLo = (LONG)(pStream->RawFilePos);
SetFilePointer(pStream->hFile, FileSizeLo, &FileSizeHi, FILE_BEGIN);
return bResult;
}
#endif
#ifdef PLATFORM_MAC
{
OSErr theErr;
theErr = FSSetForkSize((short)(long)pStream->hFile, fsFromStart, (SInt64)NewFileSize);
if(theErr != noErr)
{
nLastError = theErr;
return false;
}
return true;
}
#endif
#ifdef PLATFORM_LINUX
{
if(ftruncate((intptr_t)pStream->hFile, (off_t)NewFileSize) == -1)
{
nLastError = errno;
return false;
}
return true;
}
#endif
}
//-----------------------------------------------------------------------------
// Stream functions - partial normal file stream
/**
* \a pStream Pointer to an open stream
* \a ByteOffset File byte offset
*/
static bool PartFile_GetPos(TPartFileStream * pStream, ULONGLONG & ByteOffset)
{
ByteOffset = pStream->VirtualPos;
return true;
}
/**
* \a pStream Pointer to an open stream
* \a pByteOffset Pointer to file byte offset. If NULL, reads from the current position
* \a pvBuffer Pointer to data to be read
* \a dwBytesToRead Number of bytes to read from the file
*/
static bool PartFile_Read(TPartFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead)
{
ULONGLONG RawByteOffset;
LPBYTE pbBuffer = (LPBYTE)pvBuffer;
DWORD dwBytesRemaining = dwBytesToRead;
DWORD dwPartOffset;
DWORD dwPartIndex;
DWORD dwBytesRead = 0;
DWORD dwBlockSize = pStream->BlockSize;
bool bResult = false;
int nFailReason = ERROR_HANDLE_EOF; // Why it failed if not enough bytes was read
// If the byte offset is not entered, use the current position
if(pByteOffset == NULL)
pByteOffset = &pStream->VirtualPos;
// Check if the file position is not at or beyond end of the file
if(*pByteOffset >= pStream->VirtualSize)
{
SetLastError(ERROR_HANDLE_EOF);
return false;
}
// Get the part index where the read offset is
// Note that the part index should now be within the range,
// as read requests beyond-EOF are handled by the previous test
dwPartIndex = (DWORD)(*pByteOffset / pStream->BlockSize);
assert(dwPartIndex < pStream->BlockCount);
// If the number of bytes remaining goes past
// the end of the file, cut them
if((*pByteOffset + dwBytesRemaining) > pStream->VirtualSize)
dwBytesRemaining = (DWORD)(pStream->VirtualSize - *pByteOffset);
// Calculate the offset in the current part
dwPartOffset = (DWORD)(*pByteOffset) & (pStream->BlockSize - 1);
// Read all data, one part at a time
while(dwBytesRemaining != 0)
{
PPART_FILE_MAP_ENTRY PartMap = pStream->PartMap + dwPartIndex;
DWORD dwBytesInPart;
// If the part is not present in the file, we fail the read
if((PartMap->Flags & 3) == 0)
{
nFailReason = ERROR_CAN_NOT_COMPLETE;
bResult = false;
break;
}
// If we are in the last part, we have to cut the number of bytes in the last part
if(dwPartIndex == pStream->BlockCount - 1)
dwBlockSize = (DWORD)pStream->VirtualSize & (pStream->BlockSize - 1);
// Get the number of bytes reamining in the current part
dwBytesInPart = dwBlockSize - dwPartOffset;
// Compute the raw file offset of the file part
RawByteOffset = MAKE_OFFSET64(PartMap->BlockOffsHi, PartMap->BlockOffsLo);
if(RawByteOffset == 0)
{
nFailReason = ERROR_CAN_NOT_COMPLETE;
bResult = false;
break;
}
// If the number of bytes in part is too big, cut it
if(dwBytesInPart > dwBytesRemaining)
dwBytesInPart = dwBytesRemaining;
// Append the offset within the part
RawByteOffset += dwPartOffset;
if(!File_Read(pStream, &RawByteOffset, pbBuffer, dwBytesInPart))
{
nFailReason = ERROR_CAN_NOT_COMPLETE;
bResult = false;
break;
}
// Increment the file position
dwBytesRemaining -= dwBytesInPart;
dwBytesRead += dwBytesInPart;
pbBuffer += dwBytesInPart;
// Move to the next file part
dwPartOffset = 0;
dwPartIndex++;
}
// Move the file position by the number of bytes read
pStream->VirtualPos = *pByteOffset + dwBytesRead;
if(dwBytesRead != dwBytesToRead)
SetLastError(nFailReason);
return (dwBytesRead == dwBytesToRead);
}
static bool PartFile_Write(
TPartFileStream * pStream, // Pointer to an open stream
ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
const void * pvBuffer, // Pointer to data to be read
DWORD dwBytesToRead) // Number of bytes to read from the file
{
// Keep compiler happy
dwBytesToRead = dwBytesToRead;
pByteOffset = pByteOffset;
pvBuffer = pvBuffer;
pStream = pStream;
// Not allowed
return false;
}
static bool PartFile_GetSize(
TPartFileStream * pStream, // Pointer to an open stream
ULONGLONG & FileSize) // Pointer where to store file size
{
FileSize = pStream->VirtualSize;
return true;
}
static bool PartFile_SetSize(
TPartFileStream * pStream, // Pointer to an open stream
ULONGLONG NewSize) // new size of the file
{
// Keep compiler happy
pStream = pStream;
NewSize = NewSize;
// Not allowed
return false;
}
/*
* Stream functions - encrypted stream
*
* Note: In original Starcraft II Installer.exe: Suffix derived from battle.net auth. code
* Address of decryption routine: 0053A3D0 http://us.battle.net/static/mediakey/sc2-authenticationcode-enUS.txt
* Pointer to decryptor object: ECX Numbers mean offset of 4-char group of auth code
* Pointer to key: ECX+0x5C -0C- -1C--08- -18--04- -14--00- -10-
*/
static const char * MpqeKey_Starcraft2_Install_enUS = "expand 32-byte kTFD80000ETR5VM5G0000K859RE5N0000WT6F3DH500005LXG";
static const char * MpqeKey_Starcraft2_Install_enGB = "expand 32-byte kANGY000029ZH6NA20000HRGF8UDG0000NY82G8MN00006A3D";
static const char * MpqeKey_Starcraft2_Install_deDE = "expand 32-byte kSSXH00004XFXK4KX00008EKJD3CA0000Y64ZY45M0000YD9V";
static const char * MpqeKey_Starcraft2_Install_esES = "expand 32-byte kQU4Y0000XKTQ94PF0000N4R4UAXE0000AZ248WLK0000249P";
static const char * MpqeKey_Starcraft2_Install_frFR = "expand 32-byte kFWPQ00006EAJ8HJE0000PFER9K9300008MA2ZG7J0000UA76";
static const char * MpqeKey_Starcraft2_Install_itIT = "expand 32-byte kXV7E00008BL2TVAP0000GVMWUNNN0000SVBWNE7C00003G2B";
static const char * MpqeKey_Starcraft2_Install_plPL = "expand 32-byte k83U6000048L6LULJ00004MQDB8ME0000UP6K2NSF0000YHA3";
static const char * MpqeKey_Starcraft2_Install_ruRU = "expand 32-byte k9SH70000YEGT4BAT0000QDK978W60000V9NLVHB30000D68V";
static const char * MpqKeyArray[] =
{
MpqeKey_Starcraft2_Install_enUS,
MpqeKey_Starcraft2_Install_enGB,
MpqeKey_Starcraft2_Install_deDE,
MpqeKey_Starcraft2_Install_esES,
MpqeKey_Starcraft2_Install_frFR,
MpqeKey_Starcraft2_Install_itIT,
MpqeKey_Starcraft2_Install_plPL,
MpqeKey_Starcraft2_Install_ruRU,
NULL
};
static DWORD Rol32(DWORD dwValue, DWORD dwRolCount)
{
DWORD dwShiftRight = 32 - dwRolCount;
return (dwValue << dwRolCount) | (dwValue >> dwShiftRight);
}
static void DecryptFileChunk(
DWORD * MpqData,
LPBYTE pbKey,
ULONGLONG ByteOffset,
DWORD dwLength)
{
ULONGLONG ChunkOffset;
DWORD KeyShuffled[0x10];
DWORD KeyMirror[0x10];
DWORD RoundCount = 0x14;
// Prepare the key
ChunkOffset = ByteOffset / MPQE_CHUNK_SIZE;
memcpy(KeyMirror, pbKey, MPQE_CHUNK_SIZE);
BSWAP_ARRAY32_UNSIGNED(KeyMirror, MPQE_CHUNK_SIZE);
KeyMirror[0x05] = (DWORD)(ChunkOffset >> 32);
KeyMirror[0x08] = (DWORD)(ChunkOffset);
while(dwLength >= MPQE_CHUNK_SIZE)
{
// Shuffle the key - part 1
KeyShuffled[0x0E] = KeyMirror[0x00];
KeyShuffled[0x0C] = KeyMirror[0x01];
KeyShuffled[0x05] = KeyMirror[0x02];
KeyShuffled[0x0F] = KeyMirror[0x03];
KeyShuffled[0x0A] = KeyMirror[0x04];
KeyShuffled[0x07] = KeyMirror[0x05];
KeyShuffled[0x0B] = KeyMirror[0x06];
KeyShuffled[0x09] = KeyMirror[0x07];
KeyShuffled[0x03] = KeyMirror[0x08];
KeyShuffled[0x06] = KeyMirror[0x09];
KeyShuffled[0x08] = KeyMirror[0x0A];
KeyShuffled[0x0D] = KeyMirror[0x0B];
KeyShuffled[0x02] = KeyMirror[0x0C];
KeyShuffled[0x04] = KeyMirror[0x0D];
KeyShuffled[0x01] = KeyMirror[0x0E];
KeyShuffled[0x00] = KeyMirror[0x0F];
// Shuffle the key - part 2
for(DWORD i = 0; i < RoundCount; i += 2)
{
KeyShuffled[0x0A] = KeyShuffled[0x0A] ^ Rol32((KeyShuffled[0x0E] + KeyShuffled[0x02]), 0x07);
KeyShuffled[0x03] = KeyShuffled[0x03] ^ Rol32((KeyShuffled[0x0A] + KeyShuffled[0x0E]), 0x09);
KeyShuffled[0x02] = KeyShuffled[0x02] ^ Rol32((KeyShuffled[0x03] + KeyShuffled[0x0A]), 0x0D);
KeyShuffled[0x0E] = KeyShuffled[0x0E] ^ Rol32((KeyShuffled[0x02] + KeyShuffled[0x03]), 0x12);
KeyShuffled[0x07] = KeyShuffled[0x07] ^ Rol32((KeyShuffled[0x0C] + KeyShuffled[0x04]), 0x07);
KeyShuffled[0x06] = KeyShuffled[0x06] ^ Rol32((KeyShuffled[0x07] + KeyShuffled[0x0C]), 0x09);
KeyShuffled[0x04] = KeyShuffled[0x04] ^ Rol32((KeyShuffled[0x06] + KeyShuffled[0x07]), 0x0D);
KeyShuffled[0x0C] = KeyShuffled[0x0C] ^ Rol32((KeyShuffled[0x04] + KeyShuffled[0x06]), 0x12);
KeyShuffled[0x0B] = KeyShuffled[0x0B] ^ Rol32((KeyShuffled[0x05] + KeyShuffled[0x01]), 0x07);
KeyShuffled[0x08] = KeyShuffled[0x08] ^ Rol32((KeyShuffled[0x0B] + KeyShuffled[0x05]), 0x09);
KeyShuffled[0x01] = KeyShuffled[0x01] ^ Rol32((KeyShuffled[0x08] + KeyShuffled[0x0B]), 0x0D);
KeyShuffled[0x05] = KeyShuffled[0x05] ^ Rol32((KeyShuffled[0x01] + KeyShuffled[0x08]), 0x12);
KeyShuffled[0x09] = KeyShuffled[0x09] ^ Rol32((KeyShuffled[0x0F] + KeyShuffled[0x00]), 0x07);
KeyShuffled[0x0D] = KeyShuffled[0x0D] ^ Rol32((KeyShuffled[0x09] + KeyShuffled[0x0F]), 0x09);
KeyShuffled[0x00] = KeyShuffled[0x00] ^ Rol32((KeyShuffled[0x0D] + KeyShuffled[0x09]), 0x0D);
KeyShuffled[0x0F] = KeyShuffled[0x0F] ^ Rol32((KeyShuffled[0x00] + KeyShuffled[0x0D]), 0x12);
KeyShuffled[0x04] = KeyShuffled[0x04] ^ Rol32((KeyShuffled[0x0E] + KeyShuffled[0x09]), 0x07);
KeyShuffled[0x08] = KeyShuffled[0x08] ^ Rol32((KeyShuffled[0x04] + KeyShuffled[0x0E]), 0x09);
KeyShuffled[0x09] = KeyShuffled[0x09] ^ Rol32((KeyShuffled[0x08] + KeyShuffled[0x04]), 0x0D);
KeyShuffled[0x0E] = KeyShuffled[0x0E] ^ Rol32((KeyShuffled[0x09] + KeyShuffled[0x08]), 0x12);
KeyShuffled[0x01] = KeyShuffled[0x01] ^ Rol32((KeyShuffled[0x0C] + KeyShuffled[0x0A]), 0x07);
KeyShuffled[0x0D] = KeyShuffled[0x0D] ^ Rol32((KeyShuffled[0x01] + KeyShuffled[0x0C]), 0x09);
KeyShuffled[0x0A] = KeyShuffled[0x0A] ^ Rol32((KeyShuffled[0x0D] + KeyShuffled[0x01]), 0x0D);
KeyShuffled[0x0C] = KeyShuffled[0x0C] ^ Rol32((KeyShuffled[0x0A] + KeyShuffled[0x0D]), 0x12);
KeyShuffled[0x00] = KeyShuffled[0x00] ^ Rol32((KeyShuffled[0x05] + KeyShuffled[0x07]), 0x07);
KeyShuffled[0x03] = KeyShuffled[0x03] ^ Rol32((KeyShuffled[0x00] + KeyShuffled[0x05]), 0x09);
KeyShuffled[0x07] = KeyShuffled[0x07] ^ Rol32((KeyShuffled[0x03] + KeyShuffled[0x00]), 0x0D);
KeyShuffled[0x05] = KeyShuffled[0x05] ^ Rol32((KeyShuffled[0x07] + KeyShuffled[0x03]), 0x12);
KeyShuffled[0x02] = KeyShuffled[0x02] ^ Rol32((KeyShuffled[0x0F] + KeyShuffled[0x0B]), 0x07);
KeyShuffled[0x06] = KeyShuffled[0x06] ^ Rol32((KeyShuffled[0x02] + KeyShuffled[0x0F]), 0x09);
KeyShuffled[0x0B] = KeyShuffled[0x0B] ^ Rol32((KeyShuffled[0x06] + KeyShuffled[0x02]), 0x0D);
KeyShuffled[0x0F] = KeyShuffled[0x0F] ^ Rol32((KeyShuffled[0x0B] + KeyShuffled[0x06]), 0x12);
}
// Decrypt one data chunk
BSWAP_ARRAY32_UNSIGNED(MpqData, MPQE_CHUNK_SIZE);
MpqData[0x00] = MpqData[0x00] ^ (KeyShuffled[0x0E] + KeyMirror[0x00]);
MpqData[0x01] = MpqData[0x01] ^ (KeyShuffled[0x04] + KeyMirror[0x0D]);
MpqData[0x02] = MpqData[0x02] ^ (KeyShuffled[0x08] + KeyMirror[0x0A]);
MpqData[0x03] = MpqData[0x03] ^ (KeyShuffled[0x09] + KeyMirror[0x07]);
MpqData[0x04] = MpqData[0x04] ^ (KeyShuffled[0x0A] + KeyMirror[0x04]);
MpqData[0x05] = MpqData[0x05] ^ (KeyShuffled[0x0C] + KeyMirror[0x01]);
MpqData[0x06] = MpqData[0x06] ^ (KeyShuffled[0x01] + KeyMirror[0x0E]);
MpqData[0x07] = MpqData[0x07] ^ (KeyShuffled[0x0D] + KeyMirror[0x0B]);
MpqData[0x08] = MpqData[0x08] ^ (KeyShuffled[0x03] + KeyMirror[0x08]);
MpqData[0x09] = MpqData[0x09] ^ (KeyShuffled[0x07] + KeyMirror[0x05]);
MpqData[0x0A] = MpqData[0x0A] ^ (KeyShuffled[0x05] + KeyMirror[0x02]);
MpqData[0x0B] = MpqData[0x0B] ^ (KeyShuffled[0x00] + KeyMirror[0x0F]);
MpqData[0x0C] = MpqData[0x0C] ^ (KeyShuffled[0x02] + KeyMirror[0x0C]);
MpqData[0x0D] = MpqData[0x0D] ^ (KeyShuffled[0x06] + KeyMirror[0x09]);
MpqData[0x0E] = MpqData[0x0E] ^ (KeyShuffled[0x0B] + KeyMirror[0x06]);
MpqData[0x0F] = MpqData[0x0F] ^ (KeyShuffled[0x0F] + KeyMirror[0x03]);
BSWAP_ARRAY32_UNSIGNED(MpqData, MPQE_CHUNK_SIZE);
// Update byte offset in the key
KeyMirror[0x08]++;
if(KeyMirror[0x08] == 0)
KeyMirror[0x05]++;
// Move pointers and decrease number of bytes to decrypt
MpqData += (MPQE_CHUNK_SIZE / sizeof(DWORD));
dwLength -= MPQE_CHUNK_SIZE;
}
}
static bool DetectFileKey(TEncryptedStream * pStream)
{
ULONGLONG ByteOffset = 0;
BYTE EncryptedHeader[MPQE_CHUNK_SIZE];
BYTE FileHeader[MPQE_CHUNK_SIZE];
// Load the chunk from the file
if(!FileStream_Read(pStream, &ByteOffset, EncryptedHeader, sizeof(EncryptedHeader)))
return false;
// We just try all known keys one by one
for(int i = 0; MpqKeyArray[i] != NULL; i++)
{
// Copy the key there
memcpy(pStream->Key, MpqKeyArray[i], MPQE_CHUNK_SIZE);
BSWAP_ARRAY32_UNSIGNED(pStream->Key, MPQE_CHUNK_SIZE);
// Try to decrypt with the given key
memcpy(FileHeader, EncryptedHeader, MPQE_CHUNK_SIZE);
DecryptFileChunk((LPDWORD)FileHeader, pStream->Key, ByteOffset, MPQE_CHUNK_SIZE);
// We check the decrypoted data
// All known encrypted MPQs have header at the begin of the file,
// so we check for MPQ signature there.
if(FileHeader[0] == 'M' && FileHeader[1] == 'P' && FileHeader[2] == 'Q')
return true;
}
// Key not found, sorry
return false;
}
static bool EncryptedFile_Read(
TEncryptedStream * pStream, // Pointer to an open stream
ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
void * pvBuffer, // Pointer to data to be read
DWORD dwBytesToRead) // Number of bytes to read from the file
{
ULONGLONG StartOffset; // Offset of the first byte to be read from the file
ULONGLONG ByteOffset; // Offset that the caller wants
ULONGLONG EndOffset; // End offset that is to be read from the file
DWORD dwBytesToAllocate;
DWORD dwBytesToDecrypt;
DWORD dwOffsetInCache;
LPBYTE pbMpqData = NULL;
bool bResult = false;
// Get the byte offset
if(pByteOffset != NULL)
ByteOffset = *pByteOffset;
else
ByteOffset = pStream->RawFilePos;
// Cut it down to MPQE chunk size
StartOffset = ByteOffset;
StartOffset = StartOffset & ~(MPQE_CHUNK_SIZE - 1);
EndOffset = ByteOffset + dwBytesToRead;
// Calculate number of bytes to decrypt
dwBytesToDecrypt = (DWORD)(EndOffset - StartOffset);
dwBytesToAllocate = (dwBytesToDecrypt + (MPQE_CHUNK_SIZE - 1)) & ~(MPQE_CHUNK_SIZE - 1);
// Allocate buffers for encrypted and decrypted data
pbMpqData = STORM_ALLOC(BYTE, dwBytesToAllocate);
if(pbMpqData)
{
// Get the offset of the desired data in the cache
dwOffsetInCache = (DWORD)(ByteOffset - StartOffset);
// Read the file from the stream as-is
if(File_Read(pStream, &StartOffset, pbMpqData, dwBytesToDecrypt))
{
// Decrypt the data
DecryptFileChunk((LPDWORD)pbMpqData, pStream->Key, StartOffset, dwBytesToAllocate);
// Copy the decrypted data
memcpy(pvBuffer, pbMpqData + dwOffsetInCache, dwBytesToRead);
bResult = true;
}
else
{
assert(false);
}
// Free decryption buffer
STORM_FREE(pbMpqData);
}
// Free buffers and exit
return bResult;
}
static bool EncryptedFile_Write(
TEncryptedStream * pStream, // Pointer to an open stream
ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
const void * pvBuffer, // Pointer to data to be read
DWORD dwBytesToRead) // Number of bytes to read from the file
{
// Keep compiler happy
dwBytesToRead = dwBytesToRead;
pByteOffset = pByteOffset;
pvBuffer = pvBuffer;
pStream = pStream;
// Not allowed
return false;
}
static bool EncryptedFile_SetSize(
TEncryptedStream * pStream, // Pointer to an open stream
ULONGLONG NewSize) // new size of the file
{
// Keep compiler happy
pStream = pStream;
NewSize = NewSize;
// Not allowed
return false;
}
//-----------------------------------------------------------------------------
// Public functions
/**
* This function creates a new file for read or read-write access
*
* - If the current platform supports file sharing,
* the file must be created for read sharing (i.e. another application
* can open the file for read, but not for write)
* - If the file does not exist, the function must create new one
* - If the file exists, the function must rewrite it and set to zero size
* - The parameters of the function must be validate by the caller
* - The function must initialize all stream function pointers in TFileStream
* - If the function fails from any reason, it must close all handles
* and free all memory that has been allocated in the process of stream creation,
* including the TFileStream structure itself
*
* \a szFileName Name of the file to create
*/
TFileStream * FileStream_CreateFile(
const TCHAR * szFileName) // Name of the file to create
{
TFileStream * pStream = NULL;
HANDLE hFile;
// Create the file
hFile = CreateNewFile(szFileName);
if(hFile != INVALID_HANDLE_VALUE)
{
// Allocate the FileStream structure and fill it
pStream = STORM_ALLOC(TFileStream, 1);
if(pStream != NULL)
{
// Reset entire structure to zero
memset(pStream, 0, sizeof(TFileStream));
// Save file name and set function pointers
_tcscpy(pStream->szFileName, szFileName);
pStream->StreamGetPos = File_GetPos;
pStream->StreamRead = File_Read;
pStream->StreamWrite = File_Write;
pStream->StreamGetSize = File_GetSize;
pStream->StreamSetSize = File_SetSize;
pStream->hFile = hFile;
}
else
{
CloseTheFile(hFile);
}
}
// Return the stream
return pStream;
}
/**
* This function opens an existing file for read or read-write access
* - If the current platform supports file sharing,
* the file must be open for read sharing (i.e. another application
* can open the file for read, but not for write)
* - If the file does not exist, the function must return NULL
* - If the file exists but cannot be open, then function must return NULL
* - The parameters of the function must be validate by the caller
* - The function must check if the file is a PART file,
* and create TPartFileStream object if so.
* - The function must initialize all stream function pointers in TFileStream
* - If the function fails from any reason, it must close all handles
* and free all memory that has been allocated in the process of stream creation,
* including the TFileStream structure itself
*
* \a szFileName Name of the file to open
* \a bWriteAccess false for read only, true for read+write
*/
TFileStream * FileStream_OpenRawFile(
const TCHAR * szFileName, // Name of the file to create
bool bWriteAccess) // false = read-only, true = read+write
{
TFileStream * pStream;
HANDLE hFile;
// Create the file
hFile = OpenExistingFile(szFileName, bWriteAccess);
if(hFile == INVALID_HANDLE_VALUE)
return NULL;
// Initialize the file as normal file stream
pStream = STORM_ALLOC(TFileStream, 1);
if(pStream != NULL)
{
// Reset entire structure to zero
memset(pStream, 0, sizeof(TFileStream));
// Save file name and set function pointers
_tcscpy(pStream->szFileName, szFileName);
pStream->StreamGetPos = File_GetPos;
pStream->StreamRead = File_Read;
pStream->StreamWrite = File_Write;
pStream->StreamGetSize = File_GetSize;
pStream->StreamSetSize = File_SetSize;
if(bWriteAccess == false)
pStream->StreamFlags |= STREAM_FLAG_READ_ONLY;
pStream->hFile = hFile;
return pStream;
}
CloseTheFile(hFile);
return NULL;
}
/**
* Opens a file
*
* \a szFileName Name of the file to open
* \a bWriteAccess false for read only, true for read+write
*/
TFileStream * FileStream_OpenFile(const TCHAR * szFileName, bool bWriteAccess)
{
PART_FILE_HEADER PartHdr;
ULONGLONG VirtualSize; // Size of the file stored in part file
ULONGLONG ByteOffset = {0};
TFileStream * pStream;
size_t nStructLength;
DWORD BlockCount;
// Open the file as normal stream
pStream = FileStream_OpenRawFile(szFileName, bWriteAccess);
if(pStream == NULL)
return NULL;
// Attempt to read PART file header
if(FileStream_Read(pStream, &ByteOffset, &PartHdr, sizeof(PART_FILE_HEADER)))
{
// We need to swap PART file header on big-endian platforms
BSWAP_PART_HEADER(&PartHdr);
// Verify the PART file header
if(IsPartHeader(&PartHdr))
{
TPartFileStream * pPartStream;
// Calculate the number of parts in the file
VirtualSize = MAKE_OFFSET64(PartHdr.FileSizeHi, PartHdr.FileSizeLo);
BlockCount = (DWORD)((VirtualSize + PartHdr.BlockSize - 1) / PartHdr.BlockSize);
// Calculate the size of the entire structure
// Note that we decrement number of parts by one,
// because there already is one entry in the TPartFileStream structure
nStructLength = sizeof(TPartFileStream) + (BlockCount - 1) * sizeof(PART_FILE_MAP_ENTRY);
pPartStream = (TPartFileStream *)STORM_ALLOC(char, nStructLength);
if(pPartStream != NULL)
{
// Initialize the part file stream
memset(pPartStream, 0, nStructLength);
memcpy(pPartStream, pStream, sizeof(TFileStream));
// Load the block map
if(!FileStream_Read(pPartStream, NULL, pPartStream->PartMap, BlockCount * sizeof(PART_FILE_MAP_ENTRY)))
{
FileStream_Close(pStream);
STORM_FREE(pPartStream);
return NULL;
}
// Swap the array of file map entries
BSWAP_ARRAY32_UNSIGNED(pPartStream->PartMap, BlockCount * sizeof(PART_FILE_MAP_ENTRY));
// Set new function pointers
pPartStream->StreamGetPos = (STREAM_GETPOS)PartFile_GetPos;
pPartStream->StreamRead = (STREAM_READ)PartFile_Read;
pPartStream->StreamWrite = (STREAM_WRITE)PartFile_Write;
pPartStream->StreamGetSize = (STREAM_GETSIZE)PartFile_GetSize;
pPartStream->StreamSetSize = (STREAM_SETSIZE)PartFile_SetSize;
pPartStream->StreamFlags |= (STREAM_FLAG_READ_ONLY | STREAM_FLAG_PART_FILE);
// Fill the members of PART file stream
pPartStream->VirtualSize = ((ULONGLONG)PartHdr.FileSizeHi) + PartHdr.FileSizeLo;
pPartStream->VirtualPos = 0;
pPartStream->BlockCount = BlockCount;
pPartStream->BlockSize = PartHdr.BlockSize;
STORM_FREE(pStream);
}
return pPartStream;
}
}
// If the file doesn't contain PART file header,
// reset the file position to begin of the file
FileStream_Read(pStream, &ByteOffset, NULL, 0);
return pStream;
}
TFileStream * FileStream_OpenEncrypted(const TCHAR * szFileName)
{
TEncryptedStream * pEncryptedStream;
TFileStream * pStream;
// Open the file as raw stream
pStream = FileStream_OpenRawFile(szFileName, false);
if(pStream)
{
// Allocate new stream for handling encryption
pEncryptedStream = STORM_ALLOC(TEncryptedStream, 1);
if(pEncryptedStream != NULL)
{
// Copy the file stream to the encrypted stream
memset(pEncryptedStream, 0, sizeof(TEncryptedStream));
memcpy(pEncryptedStream, pStream, sizeof(TFileStream));
// Assign functions
pEncryptedStream->StreamRead = (STREAM_READ)EncryptedFile_Read;
pEncryptedStream->StreamWrite = (STREAM_WRITE)EncryptedFile_Write;
pEncryptedStream->StreamSetSize = (STREAM_SETSIZE)EncryptedFile_SetSize;
pEncryptedStream->StreamFlags |= (STREAM_FLAG_READ_ONLY | STREAM_FLAG_ENCRYPTED_FILE);
// Get the file key
if(DetectFileKey(pEncryptedStream))
return pEncryptedStream;
// Close the encrypted stream
STORM_FREE(pEncryptedStream);
pEncryptedStream = NULL;
}
FileStream_Close(pStream);
pStream = NULL;
}
SetLastError(ERROR_UNKNOWN_FILE_KEY);
return NULL;
}
/**
* This function returns the current file position
* \a pStream
* \a ByteOffset
*/
bool FileStream_GetPos(TFileStream * pStream, ULONGLONG & ByteOffset)
{
assert(pStream->StreamGetPos != NULL);
return pStream->StreamGetPos(pStream, ByteOffset);
}
/**
* Reads data from the stream
*
* - Returns true if the read operation succeeded and all bytes have been read
* - Returns false if either read failed or not all bytes have been read
* - If the pByteOffset is NULL, the function must read the data from the current file position
* - The function can be called with dwBytesToRead = 0. In that case, pvBuffer is ignored
* and the function just adjusts file pointer.
*
* \a pStream Pointer to an open stream
* \a pByteOffset Pointer to file byte offset. If NULL, it reads from the current position
* \a pvBuffer Pointer to data to be read
* \a dwBytesToRead Number of bytes to read from the file
*
* \returns
* - If the function reads the required amount of bytes, it returns true.
* - If the function reads less than required bytes, it returns false and GetLastError() returns ERROR_HANDLE_EOF
* - If the function fails, it reads false and GetLastError() returns an error code different from ERROR_HANDLE_EOF
*/
bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead)
{
assert(pStream->StreamRead != NULL);
return pStream->StreamRead(pStream, pByteOffset, pvBuffer, dwBytesToRead);
}
/**
* This function writes data to the stream
*
* - Returns true if the write operation succeeded and all bytes have been written
* - Returns false if either write failed or not all bytes have been written
* - If the pByteOffset is NULL, the function must write the data to the current file position
*
* \a pStream Pointer to an open stream
* \a pByteOffset Pointer to file byte offset. If NULL, it reads from the current position
* \a pvBuffer Pointer to data to be written
* \a dwBytesToWrite Number of bytes to write to the file
*/
bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite)
{
if(pStream->StreamFlags & STREAM_FLAG_READ_ONLY)
return false;
assert(pStream->StreamWrite != NULL);
return pStream->StreamWrite(pStream, pByteOffset, pvBuffer, dwBytesToWrite);
}
/**
* Returns the last write time of a file
*
* \a pStream Pointer to an open stream
* \a pFileType Pointer where to store the file last write time
*/
bool FileStream_GetLastWriteTime(TFileStream * pStream, ULONGLONG * pFileTime)
{
#ifdef PLATFORM_WINDOWS
FILETIME ft;
if(!GetFileTime(pStream->hFile, NULL, NULL, &ft))
return false;
*pFileTime = MAKE_OFFSET64(ft.dwHighDateTime, ft.dwLowDateTime);
return true;
#endif
#ifdef PLATFORM_MAC
OSErr theErr;
FSRef theFileRef;
FSCatalogInfo theCatInfo;
theErr = FSGetForkCBInfo((short)(long)pStream->hFile, 0, NULL, NULL, NULL, &theFileRef, NULL);
if(theErr != noErr)
{
nLastError = theErr;
return false;
}
theErr = FSGetCatalogInfo(&theFileRef, kFSCatInfoContentMod, &theCatInfo, NULL, NULL, NULL);
if(theErr != noErr)
{
nLastError = theErr;
return false;
}
ConvertUTCDateTimeToFileTime(&theCatInfo.contentModDate, pFileTime);
return true;
#endif
#ifdef PLATFORM_LINUX
struct stat file_stats;
if(fstat((int)(size_t)pStream->hFile, &file_stats) == -1)
{
nLastError = errno;
return false;
}
ConvertTimeTToFileTime(pFileTime, file_stats.st_mtime);
return true;
#endif
}
/**
* Returns the size of a file
*
* \a pStream Pointer to an open stream
* \a FileSize Pointer where to store the file size
*/
bool FileStream_GetSize(TFileStream * pStream, ULONGLONG & FileSize)
{
assert(pStream->StreamGetSize != NULL);
return pStream->StreamGetSize(pStream, FileSize);
}
/**
* Sets the size of a file
*
* \a pStream Pointer to an open stream
* \a NewFileSize File size to set
*/
bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize)
{
if(pStream->StreamFlags & STREAM_FLAG_READ_ONLY)
return false;
assert(pStream->StreamSetSize != NULL);
return pStream->StreamSetSize(pStream, NewFileSize);
}
/**
* Switches a stream with another. Used for final phase of archive compacting.
* Performs these steps:
*
* 1) Closes the handle to the existing MPQ
* 2) Renames the temporary MPQ to the original MPQ, overwrites existing one
* 3) Opens the MPQ stores the handle and stream position to the new stream structure
*
* \a pStream Pointer to an open stream
* \a pTempStream Temporary ("working") stream (created during archive compacting)
*/
bool FileStream_MoveFile(TFileStream * pStream, TFileStream * pTempStream)
{
bool bWriteAccess;
// Close the handle to the temporary file
CloseTheFile(pTempStream->hFile);
pTempStream->hFile = INVALID_HANDLE_VALUE;
// Close the handle to the source file
CloseTheFile(pStream->hFile);
pStream->hFile = INVALID_HANDLE_VALUE;
// Rename the temp file to the final file
if(!RenameFile(pTempStream->szFileName, pStream->szFileName))
return false;
// Now open the renamed file again, and store its handle to the old stream
bWriteAccess = (pStream->StreamFlags & STREAM_FLAG_READ_ONLY) ? false : true;
pStream->hFile = OpenExistingFile(pStream->szFileName, bWriteAccess);
if(pStream->hFile == INVALID_HANDLE_VALUE)
return false;
// Delete the temporary file stream
FileStream_Close(pTempStream);
// The file position has been reset to zero by reopening the file
pStream->RawFilePos = 0;
return true;
}
/**
* This function closes an archive file and frees any data buffers
* that have been allocated for stream management. The function must also
* support partially allocated structure, i.e. one or more buffers
* can be NULL, if there was an allocation failure during the process
*
* \a pStream Pointer to an open stream
*/
void FileStream_Close(TFileStream * pStream)
{
// Check if the stream structure is allocated at all
if(pStream != NULL)
{
// Close the file handle
if(pStream->hFile != INVALID_HANDLE_VALUE)
CloseTheFile(pStream->hFile);
// Free the stream itself
STORM_FREE(pStream);
}
}
//-----------------------------------------------------------------------------
// main - for testing purposes
/*
int main(void)
{
ULONGLONG FilePos;
ULONGLONG FileSize;
TMPQFileTime * pFT;
TFileStream * pTempStream;
TFileStream * pStream;
TMPQBlock * pBlock;
TMPQHash * pHash;
TMPQHeader2 MpqHeader;
char szString1[100] = "This is a single line\n\r";
char szString2[100];
char Buffer[0x80];
DWORD dwLength = strlen(szString1);
//
// Test 1: Write to a stream
//
pStream = FileStream_CreateFile("E:\\Stream.bin");
if(pStream == NULL)
{
printf("Failed to create new file\n");
return -1;
}
for(int i = 0; i < 10; i++)
{
if(!FileStream_Write(pStream, NULL, szString1, dwLength))
{
printf("Failed to write to the stream\n");
return -1;
}
}
FileStream_Close(pStream);
//
// Test2: Read from the stream
//
pStream = FileStream_OpenFile("E:\\Stream.bin", false);
if(pStream == NULL)
{
printf("Failed to open existing file\n");
return -1;
}
// This call must end with an error
if(FileStream_Write(pStream, NULL, "aaa", 3))
{
printf("Write succeeded while it should fail\n");
return -1;
}
for(int i = 0; i < 10; i++)
{
if(!FileStream_Read(pStream, NULL, szString2, dwLength))
{
printf("Failed to read from the stream\n");
return -1;
}
szString2[dwLength] = 0;
if(strcmp(szString1, szString2))
{
printf("Data read from file are different from data written\n");
return -1;
}
}
FileStream_Close(pStream);
//
// Test3: Open the temp stream, write some data and switch it to the original stream
//
pStream = FileStream_OpenFile("E:\\Stream.bin", false);
if(pStream == NULL)
{
printf("Failed to open existing file\n");
return -1;
}
pTempStream = FileStream_CreateFile("E:\\TempStream.bin");
if(pTempStream == NULL)
{
printf("Failed to create temp stream\n");
return -1;
}
// Copy the original stream to the temp
if(!FileStream_GetSize(pStream, &FileSize))
{
printf("Failed to get the file size\n");
return -1;
}
while(FileSize.QuadPart != 0)
{
DWORD dwBytesToRead = FileSize.LowPart;
if(dwBytesToRead > sizeof(Buffer))
dwBytesToRead = sizeof(Buffer);
if(!FileStream_Read(pStream, NULL, Buffer, dwBytesToRead))
{
printf("CopyStream: Read source file failed\n");
return -1;
}
if(!FileStream_Write(pTempStream, NULL, Buffer, dwBytesToRead))
{
printf("CopyStream: Write target file failed\n");
return -1;
}
FileSize.QuadPart -= dwBytesToRead;
}
// Switch the streams
// Note that the pTempStream is closed by the operation
FileStream_MoveFile(pStream, pTempStream);
FileStream_Close(pStream);
//
// Test4: Read from the stream again
//
pStream = FileStream_OpenFile("E:\\Stream.bin", false);
if(pStream == NULL)
{
printf("Failed to open existing file\n");
return -1;
}
for(int i = 0; i < 10; i++)
{
if(!FileStream_Read(pStream, NULL, szString2, dwLength))
{
printf("Failed to read from the stream\n");
return -1;
}
szString2[dwLength] = 0;
if(strcmp(szString1, szString2))
{
printf("Data read from file are different from data written\n");
return -1;
}
}
FileStream_Close(pStream);
//
// Test5: Open partial MPQ stream
//
// InitializeMpqCryptography();
pStream = FileStream_OpenFile("e:\\Multimedia\\MPQs\\PartialMPQs\\patch.MPQ.part", false);
if(pStream != NULL)
{
// Read the MPQ header
FileStream_Read(pStream, NULL, &MpqHeader, MPQ_HEADER_SIZE_V2);
// Read the hash table
pHash = STORM_ALLOC(TMPQHash, MpqHeader.dwHashTableSize);
FilePos.HighPart = 0;
FilePos.LowPart = MpqHeader.dwHashTablePos;
FileStream_Read(pStream, &FilePos, pHash, MpqHeader.dwHashTableSize * sizeof(TMPQHash));
//
// At this point, the encrypted hash table should be like this:
//
// 416c7175 5ddfb61b 84bb75c8 c0399515
// a9793eca 9ec773d7 ed8a54d6 74fb2adf
// 6acd4ae5 b13816b5 ffad2341 2f2b2a54
// 614339c7 5fd0adf0 62434e91 d62439e3
// 8f317aa5 f12706d6 bd83d2ca 97d7f108
// 7586d373 51d85b05 8540beca f37ef3d7
// d931d4d6 d592aadf 9044e960 c4592e92
// 47dc03f7 0982dea4 afb31943 7c3c7cec
// 0c28fd0d bcbfb7df 4d13b6e4 b5b0ef31
// e1a33b70 ec30e4b9 7aaa5e7a fb6d46ec
// 61732791 55fe757e 8ba18b5d d5f93246
// 6d275f38 a89b5781 c34189a9 654c6472
// 07e1d4e1 814bc8ee c72d2730 815afd43
// 40bd2a92 640a9391 d868f813 0f61b73d
// 6d202746 2c5124ca 65db3ad0 5b1c3e39
// b731013c 73776405 eac0c746 6e50c938
// a4a7fd00 56db3805 6d6dbab7 44fed28a
// 2383394b bf617bdd a3edfaa2 e7d3aaaf
//
// Decrypt the hash table
// DecryptMpqBlock(pHash, MpqHeader.dwHashTableSize * sizeof(TMPQHash), MPQ_KEY_HASH_TABLE);
//
// At this point, the hash table should be like this:
//
// c750beb9 72c2538a 00000000 00000466
// ffffffff ffffffff ffffffff ffffffff
// 898fdc7a 18963b5d 00000000 000005e1
// ffffffff ffffffff ffffffff ffffffff
// e3c6fc32 d8afff2b 00000000 000001ea
// ffffffff ffffffff ffffffff ffffffff
// ffffffff ffffffff ffffffff ffffffff
// ffffffff ffffffff ffffffff ffffffff
// ffffffff ffffffff ffffffff ffffffff
// ffffffff ffffffff ffffffff ffffffff
// ffffffff ffffffff ffffffff ffffffff
// 0fa4fd60 3fbe8626 00000000 0000076f
// 9ee5bccf 031b277b 00000000 0000095c
// f4e154c5 0aadd1c1 00000000 00000876
// 9e1ce9e7 e12d575d 00000000 0000071d
//
// Read the block table
pBlock = STORM_ALLOC(TMPQBlock, MpqHeader.dwBlockTableSize);
FilePos.HighPart = 0;
FilePos.LowPart = MpqHeader.dwBlockTablePos;
FileStream_Read(pStream, &FilePos, pBlock, MpqHeader.dwBlockTableSize * sizeof(TMPQBlock));
//
// At this point, the encrypted block table should be like this:
//
// 3d4867a7 ca0f533e f82c54d6 ed3c9dec
// d8d607dc d9ad13ab f4588b46 8d058704
// e8084fc8 63bc8064 b058c777 3683e9e3
// 6c0da998 7703be0d 91ce3607 c14e29b9
// 481b5c0d 42d902d2 8302acb7 e8f3e715
// c9cdfc91 7cc38c15 ea3dfd22 ad20c856
// b6450c7f 08522866 4cedb064 e03e3a86
// 4509c7cc ddffbfc3 82fc8c66 e82a4424
// afc4a982 23169037 5af6a3e2 34e1d24e
// 362c9e34 846cfc3d 4c611fcd d645fe8f
// f4061640 6d08d196 f330a975 66e30993
// fd96a033 2b16def6 62ff30af 3e190b0b
// 664a5b91 b8558235 fd631825 a7807be7
// ec906b9b 76d8b32e 36f3ea0b 1b0f5391
//
// Decrypt the block table
// DecryptMpqBlock(pBlock, MpqHeader.dwBlockTableSize * sizeof(TMPQBlock), MPQ_KEY_BLOCK_TABLE);
//
// At this point, the block table should be like this:
//
// 0000002c 00078093 00116824 84000200
// 000780bf 000002d5 00008044 84000200
// 00078394 00001516 0000874c 84000200
// 000798aa 00003797 0000af4e 84000200
// 0007d041 000001db 00008044 84000200
// 0007d21c 0000005e 0000005e 84000200
// 0007d27a 000022fb 00009674 84000200
// 0007f575 00002389 00009c64 84000200
// 000818fe 000023cb 00009d58 84000200
// 00083cc9 000024d9 0000a0d8 84000200
// 000861a2 00002356 00009c70 84000200
// 000884f8 000023d3 00009da4 84000200
// 0008a8cb 000022d6 00009cd4 84000200
// 0008cba1 00002339 00009714 84000200
// 0008eeda 000023dc 00009b24 84000200
// 000912b6 00002481 00009eac 84000200
// 00093737 00002444 0000a028 84000200
// 00095b7b 00002440 00009fc4 84000200
//
FileStream_Close(pStream);
}
return 0;
}
*/
|