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
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
|
/*****************************************************************************/
/* StormLibTest.cpp Copyright (c) Ladislav Zezula 2003 */
/*---------------------------------------------------------------------------*/
/* Test module for StormLib */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 25.03.03 1.00 Lad The first version of StormLibTest.cpp */
/*****************************************************************************/
#define _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_SECURE_NO_DEPRECATE
#define __INCLUDE_CRYPTOGRAPHY__
#define __STORMLIB_SELF__ // Don't use StormLib.lib
#include <stdio.h>
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
#include "../src/StormLib.h"
#include "../src/StormCommon.h"
#include "TLogHelper.cpp" // Helper class for showing test results
#ifdef _MSC_VER
#pragma warning(disable: 4505) // 'XXX' : unreferenced local function has been removed
#pragma comment(lib, "winmm.lib")
#endif
//------------------------------------------------------------------------------
// Defines
#ifdef PLATFORM_WINDOWS
#define WORK_PATH_ROOT "E:\\Multimedia\\MPQs"
#endif
#ifdef PLATFORM_LINUX
#define WORK_PATH_ROOT "/home/ladik/MPQs"
#endif
#ifdef PLATFORM_MAC
#define WORK_PATH_ROOT "/Users/sam/StormLib/test"
#endif
// Global for the work MPQ
static const char * szMpqSubDir = "1996 - Test MPQs";
static const char * szMpqPatchDir = "1996 - Test MPQs\\patches";
typedef int (*ARCHIVE_TEST)(const char * szMpqName);
//-----------------------------------------------------------------------------
// Testing data
static DWORD AddFlags[] =
{
// Compression Encryption Fixed key Single Unit Sector CRC
0 | 0 | 0 | 0 | 0,
0 | MPQ_FILE_ENCRYPTED | 0 | 0 | 0,
0 | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | 0 | 0,
0 | 0 | 0 | MPQ_FILE_SINGLE_UNIT | 0,
0 | MPQ_FILE_ENCRYPTED | 0 | MPQ_FILE_SINGLE_UNIT | 0,
0 | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_IMPLODE | 0 | 0 | 0 | 0,
MPQ_FILE_IMPLODE | MPQ_FILE_ENCRYPTED | 0 | 0 | 0,
MPQ_FILE_IMPLODE | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | 0 | 0,
MPQ_FILE_IMPLODE | 0 | 0 | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_IMPLODE | MPQ_FILE_ENCRYPTED | 0 | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_IMPLODE | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_IMPLODE | 0 | 0 | 0 | MPQ_FILE_SECTOR_CRC,
MPQ_FILE_IMPLODE | MPQ_FILE_ENCRYPTED | 0 | 0 | MPQ_FILE_SECTOR_CRC,
MPQ_FILE_IMPLODE | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | 0 | MPQ_FILE_SECTOR_CRC,
MPQ_FILE_COMPRESS | 0 | 0 | 0 | 0,
MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | 0 | 0 | 0,
MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | 0 | 0,
MPQ_FILE_COMPRESS | 0 | 0 | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | 0 | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | MPQ_FILE_SINGLE_UNIT | 0,
MPQ_FILE_COMPRESS | 0 | 0 | 0 | MPQ_FILE_SECTOR_CRC,
MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | 0 | 0 | MPQ_FILE_SECTOR_CRC,
MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | MPQ_FILE_FIX_KEY | 0 | MPQ_FILE_SECTOR_CRC,
0xFFFFFFFF
};
static DWORD Compressions[] =
{
MPQ_COMPRESSION_ADPCM_MONO | MPQ_COMPRESSION_HUFFMANN,
MPQ_COMPRESSION_ADPCM_STEREO | MPQ_COMPRESSION_HUFFMANN,
MPQ_COMPRESSION_PKWARE,
MPQ_COMPRESSION_ZLIB,
MPQ_COMPRESSION_BZIP2
};
static const wchar_t szUnicodeName1[] = { // Czech
0x010C, 0x0065, 0x0073, 0x006B, 0x00FD, _T('.'), _T('m'), _T('p'), _T('q'), 0
};
static const wchar_t szUnicodeName2[] = { // Russian
0x0420, 0x0443, 0x0441, 0x0441, 0x043A, 0x0438, 0x0439, _T('.'), _T('m'), _T('p'), _T('q'), 0
};
static const wchar_t szUnicodeName3[] = { // Greek
0x03B5, 0x03BB, 0x03BB, 0x03B7, 0x03BD, 0x03B9, 0x03BA, 0x03AC, _T('.'), _T('m'), _T('p'), _T('q'), 0
};
static const wchar_t szUnicodeName4[] = { // Chinese
0x65E5, 0x672C, 0x8A9E, _T('.'), _T('m'), _T('p'), _T('q'), 0
};
static const wchar_t szUnicodeName5[] = { // Japanese
0x7B80, 0x4F53, 0x4E2D, 0x6587, _T('.'), _T('m'), _T('p'), _T('q'), 0
};
static const wchar_t szUnicodeName6[] = { // Arabic
0x0627, 0x0644, 0x0639, 0x0639, 0x0631, 0x0628, 0x064A, 0x0629, _T('.'), _T('m'), _T('p'), _T('q'), 0
};
static const char * PatchList_WoW_OldWorld13286[] =
{
"MPQ_2012_v4_OldWorld.MPQ",
"wow-update-oldworld-13154.MPQ",
"wow-update-oldworld-13286.MPQ",
NULL
};
static const char * PatchList_WoW15050[] =
{
"MPQ_2013_v4_world.MPQ",
"wow-update-13164.MPQ",
"wow-update-13205.MPQ",
"wow-update-13287.MPQ",
"wow-update-13329.MPQ",
"wow-update-13596.MPQ",
"wow-update-13623.MPQ",
"wow-update-base-13914.MPQ",
"wow-update-base-14007.MPQ",
"wow-update-base-14333.MPQ",
"wow-update-base-14480.MPQ",
"wow-update-base-14545.MPQ",
"wow-update-base-14946.MPQ",
"wow-update-base-15005.MPQ",
"wow-update-base-15050.MPQ",
NULL
};
static const char * PatchList_WoW16965[] =
{
"MPQ_2013_v4_locale-enGB.MPQ",
"wow-update-enGB-16016.MPQ",
"wow-update-enGB-16048.MPQ",
"wow-update-enGB-16057.MPQ",
"wow-update-enGB-16309.MPQ",
"wow-update-enGB-16357.MPQ",
"wow-update-enGB-16516.MPQ",
"wow-update-enGB-16650.MPQ",
"wow-update-enGB-16844.MPQ",
"wow-update-enGB-16965.MPQ",
NULL
};
//-----------------------------------------------------------------------------
// Local file functions
// Definition of the path separator
#ifdef PLATFORM_WINDOWS
#define PATH_SEPARATOR '\\' // Path separator for Windows platforms
#else
#define PATH_SEPARATOR '/' // Path separator for Windows platforms
#endif
// This must be the directory where our test MPQs are stored.
// We also expect a subdirectory named
static char szMpqDirectory[MAX_PATH];
size_t cchMpqDirectory = 0;
static size_t ConvertSha1ToText(const unsigned char * sha1_digest, char * szSha1Text)
{
const char * szTable = "0123456789abcdef";
for(size_t i = 0; i < SHA1_DIGEST_SIZE; i++)
{
*szSha1Text++ = szTable[(sha1_digest[0] >> 0x04)];
*szSha1Text++ = szTable[(sha1_digest[0] & 0x0F)];
sha1_digest++;
}
*szSha1Text = 0;
return (SHA1_DIGEST_SIZE * 2);
}
#ifdef _UNICODE
static const TCHAR * GetShortPlainName(const TCHAR * szFileName)
{
const TCHAR * szPlainName = szFileName;
const TCHAR * szPlainEnd = szFileName + _tcslen(szFileName);
// If there is terminating slash or backslash, move to it
while(szFileName < szPlainEnd)
{
if(szFileName[0] == _T('\\') || szFileName[0] == _T('/'))
szPlainName = szFileName + 1;
szFileName++;
}
// If the name is still too long, cut it
if((szPlainEnd - szPlainName) > 50)
szPlainName = szPlainEnd - 50;
return szPlainName;
}
static void CreateFullPathName(TCHAR * szBuffer, const char * szSubDir, const char * szFileName)
{
size_t nLength;
// Copy the master MPQ directory
mbstowcs(szBuffer, szMpqDirectory, cchMpqDirectory);
szBuffer += cchMpqDirectory;
// Append the subdirectory, if any
if(szSubDir != NULL && (nLength = strlen(szSubDir)) != 0)
{
// No leading or trailing separators allowed
assert(szSubDir[0] != '/' && szSubDir[0] != '\\');
assert(szSubDir[nLength - 1] != '/' && szSubDir[nLength - 1] != '\\');
// Append file path separator
*szBuffer++ = PATH_SEPARATOR;
// Copy the subdirectory
mbstowcs(szBuffer, szSubDir, nLength);
// Fix the path separators
for(size_t i = 0; i < nLength; i++)
szBuffer[i] = (szBuffer[i] != '\\' && szBuffer[i] != '/') ? szBuffer[i] : PATH_SEPARATOR;
// Move the buffer pointer
szBuffer += nLength;
}
// Copy the file name, if any
if(szFileName != NULL && (nLength = strlen(szFileName)) != 0)
{
// No path separator can be there
assert(strchr(szFileName, '\\') == NULL);
assert(strchr(szFileName, '/') == NULL);
// Append file path separator
*szBuffer++ = PATH_SEPARATOR;
// Copy the file name
mbstowcs(szBuffer, szFileName, nLength);
szBuffer += nLength;
}
// Terminate the buffer with zero
*szBuffer = 0;
}
TFileStream * FileStream_OpenFile(const char * szFileName, DWORD dwStreamFlags)
{
TFileStream * pStream = NULL;
TCHAR * szFileNameT;
size_t nLength = strlen(szFileName);
// Allocate buffer for the UNICODE file name
szFileNameT = STORM_ALLOC(TCHAR, nLength + 1);
if(szFileNameT != NULL)
{
CopyFileName(szFileNameT, szFileName, nLength);
pStream = FileStream_OpenFile(szFileNameT, dwStreamFlags);
STORM_FREE(szFileNameT);
}
// Return what we got
return pStream;
}
#endif
static const char * GetShortPlainName(const char * szFileName)
{
const char * szPlainName = szFileName;
const char * szPlainEnd = szFileName + strlen(szFileName);
// If there is terminating slash or backslash, move to it
while(szFileName < szPlainEnd)
{
if(szFileName[0] == '\\' || szFileName[0] == '/')
szPlainName = szFileName + 1;
szFileName++;
}
// If the name is still too long, cut it
if((szPlainEnd - szPlainName) > 50)
szPlainName = szPlainEnd - 50;
return szPlainName;
}
static void CreateFullPathName(char * szBuffer, const char * szSubDir, const char * szFileName)
{
size_t nLength;
// Copy the master MPQ directory
memcpy(szBuffer, szMpqDirectory, cchMpqDirectory);
szBuffer += cchMpqDirectory;
// Append the subdirectory, if any
if(szSubDir != NULL && (nLength = strlen(szSubDir)) != 0)
{
// No leading or trailing separator must be there
assert(szSubDir[0] != '/' && szSubDir[0] != '\\');
assert(szSubDir[nLength - 1] != '/' && szSubDir[nLength - 1] != '\\');
// Append file path separator
*szBuffer++ = PATH_SEPARATOR;
// Copy the subdirectory
memcpy(szBuffer, szSubDir, nLength);
// Fix the path separators
for(size_t i = 0; i < nLength; i++)
szBuffer[i] = (szBuffer[i] != '\\' && szBuffer[i] != '/') ? szBuffer[i] : PATH_SEPARATOR;
// Move the buffer pointer
szBuffer += nLength;
}
// Copy the file name, if any
if(szFileName != NULL && (nLength = strlen(szFileName)) != 0)
{
// No path separator can be there
assert(strchr(szFileName, '\\') == NULL);
assert(strchr(szFileName, '/') == NULL);
// Append file path separator
*szBuffer++ = PATH_SEPARATOR;
// Copy file name
memcpy(szBuffer, szFileName, nLength);
szBuffer += nLength;
}
// Terminate the buffer with zero
*szBuffer = 0;
}
static int InitializeMpqDirectory(char * argv[], int argc)
{
TLogHelper Logger("InitWorkDir");
TFileStream * pStream;
const char * szWhereFrom = NULL;
const char * szDirName;
TCHAR szFileName[MAX_PATH];
#ifdef _MSC_VER
// Mix the random number generator
srand(GetTickCount());
#endif
// Retrieve the name of the MPQ directory
if(argc > 1 && argv[1] != NULL)
{
szWhereFrom = "entered at command line";
szDirName = argv[1];
}
else
{
szWhereFrom = "default";
szDirName = WORK_PATH_ROOT;
}
// Copy the name of the MPQ directory.
strcpy(szMpqDirectory, szDirName);
cchMpqDirectory = strlen(szMpqDirectory);
// Cut trailing slashes and/or backslashes
while(cchMpqDirectory > 0 && szMpqDirectory[cchMpqDirectory - 1] == '/' || szMpqDirectory[cchMpqDirectory - 1] == '\\')
cchMpqDirectory--;
szMpqDirectory[cchMpqDirectory] = 0;
// Print the work directory info
Logger.PrintMessage("Work directory %s (%s)", szMpqDirectory, szWhereFrom);
// Verify if the work MPQ directory is writable
CreateFullPathName(szFileName, NULL, "TestFile.bin");
pStream = FileStream_CreateFile(szFileName, 0);
if(pStream == NULL)
return Logger.PrintError("MPQ subdirectory is not writable");
// Close the stream
FileStream_Close(pStream);
// Verify if the working directory exists and if there is a subdirectory with the file name
CreateFullPathName(szFileName, szMpqSubDir, "ListFile_Blizzard.txt");
pStream = FileStream_OpenFile(szFileName, STREAM_FLAG_READ_ONLY);
if(pStream == NULL)
return Logger.PrintError(_T("The main listfile (%s) was not found. Check your paths"), szFileName);
// Close the stream
FileStream_Close(pStream);
return ERROR_SUCCESS;
}
static int GetFilePatchCount(TLogHelper * pLogger, HANDLE hMpq, const char * szFileName)
{
TCHAR * szPatchName;
HANDLE hFile;
TCHAR szPatchChain[0x400];
int nPatchCount = 0;
int nError = ERROR_SUCCESS;
// Open the MPQ file
if(SFileOpenFileEx(hMpq, szFileName, 0, &hFile))
{
// Notify the user
pLogger->PrintProgress("Verifying patch chain for %s ...", GetShortPlainName(szFileName));
// Query the patch chain
if(!SFileGetFileInfo(hFile, SFileInfoPatchChain, szPatchChain, sizeof(szPatchChain), NULL))
nError = pLogger->PrintError("Failed to retrieve the patch chain on %s", szFileName);
// Is there anything at all in the patch chain?
if(nError == ERROR_SUCCESS && szPatchChain[0] == 0)
{
pLogger->PrintError("The patch chain for %s is empty", szFileName);
nError = ERROR_FILE_CORRUPT;
}
// Now calculate the number of patches
if(nError == ERROR_SUCCESS)
{
// Get the pointer to the patch
szPatchName = szPatchChain;
// Skip the base name
for(;;)
{
// Skip the current name
szPatchName = szPatchName + _tcslen(szPatchName) + 1;
if(szPatchName[0] == 0)
break;
// Increment number of patches
nPatchCount++;
}
}
SFileCloseFile(hFile);
}
else
{
pLogger->PrintError("Failed to open file %s", szFileName);
}
return nPatchCount;
}
static int VerifyFilePatchCount(TLogHelper * pLogger, HANDLE hMpq, const char * szFileName, int nExpectedPatchCount)
{
int nPatchCount = 0;
// Retrieve the patch count
pLogger->PrintProgress("Verifying patch count for %s ...", szFileName);
nPatchCount = GetFilePatchCount(pLogger, hMpq, szFileName);
// Check if there are any patches at all
if(nExpectedPatchCount != 0 && nPatchCount == 0)
{
pLogger->PrintMessage("There are no patches beyond %s", szFileName);
return ERROR_FILE_CORRUPT;
}
// Check if the number of patches fits
if(nPatchCount != nExpectedPatchCount)
{
pLogger->PrintMessage("Unexpected number of patches for %s", szFileName);
return ERROR_FILE_CORRUPT;
}
return ERROR_SUCCESS;
}
static int CreateEmptyFile(TLogHelper * pLogger, const char * szPlainName, ULONGLONG FileSize, TCHAR * szBuffer)
{
TFileStream * pStream;
// Notify the user
pLogger->PrintProgress("Creating empty file %s ...", szPlainName);
// Construct the full path and crete the file
CreateFullPathName(szBuffer, NULL, szPlainName);
pStream = FileStream_CreateFile(szBuffer, STREAM_PROVIDER_LINEAR | BASE_PROVIDER_FILE);
if(pStream == NULL)
return pLogger->PrintError(_T("Failed to create file %s"), szBuffer);
// Write the required size
FileStream_SetSize(pStream, FileSize);
FileStream_Close(pStream);
return ERROR_SUCCESS;
}
static int WriteMpqUserDataHeader(
TLogHelper * pLogger,
TFileStream * pStream,
ULONGLONG ByteOffset,
DWORD dwByteCount)
{
TMPQUserData UserData;
int nError = ERROR_SUCCESS;
// Notify the user
pLogger->PrintProgress("Writing user data header...");
// Fill the user data header
UserData.dwID = ID_MPQ_USERDATA;
UserData.cbUserDataSize = dwByteCount;
UserData.dwHeaderOffs = (dwByteCount + sizeof(TMPQUserData));
UserData.cbUserDataHeader = dwByteCount / 2;
if(!FileStream_Write(pStream, &ByteOffset, &UserData, sizeof(TMPQUserData)))
nError = GetLastError();
return nError;
}
static int WriteFileData(
TLogHelper * pLogger,
TFileStream * pStream,
ULONGLONG ByteOffset,
ULONGLONG ByteCount)
{
ULONGLONG SaveByteCount = ByteCount;
ULONGLONG BytesWritten = 0;
LPBYTE pbDataBuffer;
DWORD cbDataBuffer = 0x10000;
int nError = ERROR_SUCCESS;
// Write some data
pbDataBuffer = new BYTE[cbDataBuffer];
if(pbDataBuffer != NULL)
{
memset(pbDataBuffer, 0, cbDataBuffer);
strcpy((char *)pbDataBuffer, "This is a test data written to a file.");
// Perform the write
while(ByteCount > 0)
{
DWORD cbToWrite = (ByteCount > cbDataBuffer) ? cbDataBuffer : (DWORD)ByteCount;
// Notify the user
pLogger->PrintProgress("Writing file data (%I64u of %I64u) ...", BytesWritten, SaveByteCount);
// Write the data
if(!FileStream_Write(pStream, &ByteOffset, pbDataBuffer, cbToWrite))
{
nError = GetLastError();
break;
}
BytesWritten += cbToWrite;
ByteOffset += cbToWrite;
ByteCount -= cbToWrite;
}
delete [] pbDataBuffer;
}
return nError;
}
static int CopyFileData(
TLogHelper * pLogger,
TFileStream * pStream1,
TFileStream * pStream2,
ULONGLONG ByteOffset,
ULONGLONG ByteCount)
{
ULONGLONG BytesCopied = 0;
ULONGLONG EndOffset = ByteOffset + ByteCount;
LPBYTE pbCopyBuffer;
DWORD BytesToRead;
DWORD BlockLength = 0x100000;
int nError = ERROR_SUCCESS;
// Allocate copy buffer
pbCopyBuffer = STORM_ALLOC(BYTE, BlockLength);
if(pbCopyBuffer != NULL)
{
while(ByteOffset < EndOffset)
{
// Notify the user
pLogger->PrintProgress("Copying %I64u of %I64u ...", BytesCopied, ByteCount);
// Read source
BytesToRead = ((EndOffset - ByteOffset) > BlockLength) ? BlockLength : (DWORD)(EndOffset - ByteOffset);
if(!FileStream_Read(pStream1, &ByteOffset, pbCopyBuffer, BytesToRead))
{
nError = GetLastError();
break;
}
// Write to the destination file
if(!FileStream_Write(pStream2, NULL, pbCopyBuffer, BytesToRead))
{
nError = GetLastError();
break;
}
BytesCopied += BytesToRead;
ByteOffset += BytesToRead;
}
STORM_FREE(pbCopyBuffer);
}
return nError;
}
// Support function for copying file
static int CreateMpqCopy(
TLogHelper * pLogger,
const char * szPlainName,
const char * szFileCopy,
TCHAR * szBuffer,
ULONGLONG PreMpqDataSize = 0,
ULONGLONG UserDataSize = 0)
{
TFileStream * pStream1; // Source file
TFileStream * pStream2; // Target file
ULONGLONG ByteOffset = 0;
ULONGLONG FileSize = 0;
TCHAR szFileName1[MAX_PATH];
TCHAR szFileName2[MAX_PATH];
int nError = ERROR_SUCCESS;
// Notify the user
pLogger->PrintProgress("Creating copy of %s ...", szPlainName);
// Construct both file names. Check if they are not the same
CreateFullPathName(szFileName1, szMpqSubDir, szPlainName);
CreateFullPathName(szFileName2, NULL, szFileCopy);
if(!_tcsicmp(szFileName1, szFileName2))
{
pLogger->PrintError("Failed to create copy of MPQ (the copy name is the same like the original name)");
return ERROR_CAN_NOT_COMPLETE;
}
// Open the source file
pStream1 = FileStream_OpenFile(szFileName1, STREAM_FLAG_READ_ONLY);
if(pStream1 == NULL)
{
pLogger->PrintError(_T("Failed to open the source file %s"), szFileName1);
return ERROR_CAN_NOT_COMPLETE;
}
// Create the destination file
pStream2 = FileStream_CreateFile(szFileName2, 0);
if(pStream2 != NULL)
{
// If we should write some pre-MPQ data to the target file, do it
if(PreMpqDataSize != 0)
{
nError = WriteFileData(pLogger, pStream2, ByteOffset, PreMpqDataSize);
ByteOffset += PreMpqDataSize;
}
// If we should write some MPQ user data, write the header first
if(UserDataSize != 0)
{
nError = WriteMpqUserDataHeader(pLogger, pStream2, ByteOffset, (DWORD)UserDataSize);
ByteOffset += sizeof(TMPQUserData);
nError = WriteFileData(pLogger, pStream2, ByteOffset, UserDataSize);
ByteOffset += UserDataSize;
}
// Copy the file data from the source file to the destination file
FileStream_GetSize(pStream1, &FileSize);
if(FileSize != 0)
{
nError = CopyFileData(pLogger, pStream1, pStream2, 0, FileSize);
ByteOffset += FileSize;
}
FileStream_Close(pStream2);
}
// Close the source file
FileStream_Close(pStream1);
if(szBuffer != NULL)
_tcscpy(szBuffer, szFileName2);
if(nError != ERROR_SUCCESS)
pLogger->PrintError("Failed to create copy of MPQ");
return nError;
}
static void WINAPI AddFileCallback(void * pvUserData, DWORD dwBytesWritten, DWORD dwTotalBytes, bool bFinalCall)
{
TLogHelper * pLogger = (TLogHelper *)pvUserData;
// Keep compiler happy
bFinalCall = bFinalCall;
pLogger->PrintProgress("Adding file (%s) (%u of %u) (%u of %u) ...", pLogger->UserString,
pLogger->UserCount,
pLogger->UserTotal,
dwBytesWritten,
dwTotalBytes);
}
static void WINAPI CompactCallback(void * pvUserData, DWORD dwWork, ULONGLONG BytesDone, ULONGLONG TotalBytes)
{
TLogHelper * pLogger = (TLogHelper *)pvUserData;
const char * szWork = NULL;
switch(dwWork)
{
case CCB_CHECKING_FILES:
szWork = "Checking files in archive";
break;
case CCB_CHECKING_HASH_TABLE:
szWork = "Checking hash table";
break;
case CCB_COPYING_NON_MPQ_DATA:
szWork = "Copying non-MPQ data";
break;
case CCB_COMPACTING_FILES:
szWork = "Compacting files";
break;
case CCB_CLOSING_ARCHIVE:
szWork = "Closing archive";
break;
}
if(szWork != NULL)
{
if(pLogger != NULL)
pLogger->PrintProgress("%s (%I64u of %I64u) ...", szWork, BytesDone, TotalBytes);
else
printf("%s (%I64u of %I64u) ... \r", szWork, (DWORD)BytesDone, (DWORD)TotalBytes);
}
}
//-----------------------------------------------------------------------------
// MPQ file utilities
#define TEST_FLAG_LOAD_FILES 0x00000001 // Test function should load all files in the MPQ
#define TEST_FLAG_HASH_FILES 0x00000002 // Test function should load all files in the MPQ
#define TEST_FLAG_PLAY_WAVES 0x00000004 // Play extracted WAVE files
#define TEST_FLAG_MOST_PATCHED 0x00000008 // Find the most patched file
struct TFileData
{
DWORD dwBlockIndex;
DWORD dwFileSize;
DWORD dwFlags;
DWORD dwReserved; // Alignment
BYTE FileData[1];
};
static bool CheckIfFileIsPresent(TLogHelper * pLogger, HANDLE hMpq, const char * szFileName, bool bShouldExist)
{
HANDLE hFile = NULL;
if(SFileOpenFileEx(hMpq, szFileName, 0, &hFile))
{
if(bShouldExist == false)
pLogger->PrintMessage("The file %s is present, but it should not be", szFileName);
SFileCloseFile(hFile);
return true;
}
else
{
if(bShouldExist)
pLogger->PrintMessage("The file %s is not present, but it should be", szFileName);
return false;
}
}
static TFileData * LoadLocalFile(TLogHelper * pLogger, const char * szFileName, bool bMustSucceed)
{
TFileStream * pStream;
TFileData * pFileData = NULL;
ULONGLONG FileSize = 0;
size_t nAllocateBytes;
// Notify the user
if(pLogger != NULL)
pLogger->PrintProgress("Loading local file ...");
// Attempt to open the file
pStream = FileStream_OpenFile(szFileName, STREAM_FLAG_READ_ONLY);
if(pStream == NULL)
{
if(pLogger != NULL && bMustSucceed == true)
pLogger->PrintError("Failed to open the file %s", szFileName);
return NULL;
}
// Verify the size
FileStream_GetSize(pStream, &FileSize);
if((FileSize >> 0x20) == 0)
{
// Allocate space for the file
nAllocateBytes = sizeof(TFileData) + (size_t)FileSize;
pFileData = (TFileData *)STORM_ALLOC(BYTE, nAllocateBytes);
if(pFileData != NULL)
{
// Make sure it;s properly zeroed
memset(pFileData, 0, nAllocateBytes);
pFileData->dwFileSize = (DWORD)FileSize;
// Load to memory
if(!FileStream_Read(pStream, NULL, pFileData->FileData, pFileData->dwFileSize))
{
STORM_FREE(pFileData);
pFileData = NULL;
}
}
}
FileStream_Close(pStream);
return pFileData;
}
static TFileData * LoadMpqFile(TLogHelper * pLogger, HANDLE hMpq, const char * szFileName)
{
TFileData * pFileData = NULL;
HANDLE hFile;
DWORD dwFileSizeHi = 0xCCCCCCCC;
DWORD dwFileSizeLo = 0;
DWORD dwBytesRead;
int nError = ERROR_SUCCESS;
// Notify the user that we are loading a file from MPQ
pLogger->PrintProgress("Loading file %s ...", GetShortPlainName(szFileName));
// Open the file from MPQ
if(!SFileOpenFileEx(hMpq, szFileName, 0, &hFile))
nError = pLogger->PrintError("Failed to open the file %s", szFileName);
// Get the size of the file
if(nError == ERROR_SUCCESS)
{
dwFileSizeLo = SFileGetFileSize(hFile, &dwFileSizeHi);
if(dwFileSizeLo == SFILE_INVALID_SIZE || dwFileSizeHi != 0)
nError = pLogger->PrintError("Failed to query the file size");
}
// Allocate buffer for the file content
if(nError == ERROR_SUCCESS)
{
pFileData = (TFileData *)STORM_ALLOC(BYTE, sizeof(TFileData) + dwFileSizeLo);
if(pFileData == NULL)
{
pLogger->PrintError("Failed to allocate buffer for the file content");
nError = ERROR_NOT_ENOUGH_MEMORY;
}
}
// get the file index of the MPQ file
if(nError == ERROR_SUCCESS)
{
// Store the file size
memset(pFileData, 0, sizeof(TFileData) + dwFileSizeLo);
pFileData->dwFileSize = dwFileSizeLo;
// Retrieve the block index and file flags
if(!SFileGetFileInfo(hFile, SFileInfoFileIndex, &pFileData->dwBlockIndex, sizeof(DWORD), NULL))
nError = pLogger->PrintError("Failed retrieve the file index of %s", szFileName);
if(!SFileGetFileInfo(hFile, SFileInfoFlags, &pFileData->dwFlags, sizeof(DWORD), NULL))
nError = pLogger->PrintError("Failed retrieve the file flags of %s", szFileName);
}
// Load the entire file
if(nError == ERROR_SUCCESS)
{
// Read the file data
SFileReadFile(hFile, pFileData->FileData, dwFileSizeLo, &dwBytesRead, NULL);
if(dwBytesRead != dwFileSizeLo)
nError = pLogger->PrintError("Failed to read the content of the file %s", szFileName);
}
// Close the file and return what we got
if(hFile != NULL)
SFileCloseFile(hFile);
if(nError != ERROR_SUCCESS)
SetLastError(nError);
return pFileData;
}
static bool CompareTwoFiles(TLogHelper * pLogger, TFileData * pFileData1, TFileData * pFileData2)
{
// Compare the file size
if(pFileData1->dwFileSize != pFileData2->dwFileSize)
{
pLogger->PrintErrorVa(_T("The files have different size (%u vs %u)"), pFileData1->dwFileSize, pFileData2->dwFileSize);
SetLastError(ERROR_FILE_CORRUPT);
return false;
}
// Compare the files
for(DWORD i = 0; i < pFileData1->dwFileSize; i++)
{
if(pFileData1->FileData[i] != pFileData2->FileData[i])
{
pLogger->PrintErrorVa(_T("Files are different at offset %08X"), i);
SetLastError(ERROR_FILE_CORRUPT);
return false;
}
}
// The files are identical
return true;
}
static int SearchArchive(
TLogHelper * pLogger,
HANDLE hMpq,
DWORD dwTestFlags = 0,
DWORD * pdwFileCount = NULL,
LPBYTE pbFileHash = NULL)
{
SFILE_FIND_DATA sf;
TFileData * pFileData;
HANDLE hFind;
DWORD dwFileCount = 0;
hash_state md5state;
char szMostPatched[MAX_PATH] = "";
char szListFile[MAX_PATH];
bool bFound = true;
int nMaxPatchCount = 0;
int nPatchCount = 0;
int nError = ERROR_SUCCESS;
// Construct the full name of the listfile
CreateFullPathName(szListFile, szMpqSubDir, "ListFile_Blizzard.txt");
// Prepare hashing
md5_init(&md5state);
// Initiate the MPQ search
pLogger->PrintProgress("Searching the archive ...");
hFind = SFileFindFirstFile(hMpq, "*", &sf, szListFile);
if(hFind == NULL)
{
nError = GetLastError();
nError = (nError == ERROR_NO_MORE_FILES) ? ERROR_SUCCESS : nError;
return nError;
}
// Perform the search
while(bFound == true)
{
// Increment number of files
dwFileCount++;
if(dwTestFlags & TEST_FLAG_MOST_PATCHED)
{
// Load the patch count
nPatchCount = GetFilePatchCount(pLogger, hMpq, sf.cFileName);
// Check if it's greater than maximum
if(nPatchCount > nMaxPatchCount)
{
strcpy(szMostPatched, sf.cFileName);
nMaxPatchCount = nPatchCount;
}
}
// Load the file to memory, if required
if(dwTestFlags & TEST_FLAG_LOAD_FILES)
{
// Load the entire file to the MPQ
pFileData = LoadMpqFile(pLogger, hMpq, sf.cFileName);
if(pFileData == NULL)
{
nError = pLogger->PrintError("Failed to load the file %s", sf.cFileName);
break;
}
// Hash the file data, if needed
if((dwTestFlags & TEST_FLAG_HASH_FILES) && !IsInternalMpqFileName(sf.cFileName))
md5_process(&md5state, pFileData->FileData, pFileData->dwFileSize);
// Play sound files, if required
if((dwTestFlags & TEST_FLAG_PLAY_WAVES) && strstr(sf.cFileName, ".wav") != NULL)
{
#ifdef _MSC_VER
pLogger->PrintProgress("Playing sound %s", sf.cFileName);
PlaySound((LPCTSTR)pFileData->FileData, NULL, SND_MEMORY);
#endif
}
STORM_FREE(pFileData);
}
bFound = SFileFindNextFile(hFind, &sf);
}
SFileFindClose(hFind);
// Give the file count, if required
if(pdwFileCount != NULL)
pdwFileCount[0] = dwFileCount;
// Give the hash, if required
if(pbFileHash != NULL && (dwTestFlags & TEST_FLAG_HASH_FILES))
md5_done(&md5state, pbFileHash);
return nError;
}
static int CreateNewArchive_FullPath(TLogHelper * pLogger, const TCHAR * szMpqName, DWORD dwCreateFlags, DWORD dwMaxFileCount, HANDLE * phMpq)
{
HANDLE hMpq = NULL;
// Make sure that the MPQ is deleted
_tremove(szMpqName);
// Fix the flags
dwCreateFlags |= (MPQ_CREATE_LISTFILE | MPQ_CREATE_ATTRIBUTES);
// Create the new MPQ
if(!SFileCreateArchive(szMpqName, dwCreateFlags, dwMaxFileCount, &hMpq))
return pLogger->PrintError(_T("Failed to create archive %s"), szMpqName);
// Shall we close it right away?
if(phMpq == NULL)
SFileCloseArchive(hMpq);
else
*phMpq = hMpq;
return ERROR_SUCCESS;
}
static int CreateNewArchive(TLogHelper * pLogger, const TCHAR * szPlainName, DWORD dwCreateFlags, DWORD dwMaxFileCount, HANDLE * phMpq)
{
TCHAR szMpqName[MAX_PATH];
CreateFullPathName(szMpqName, "StormLibTest_", NULL);
_tcscat(szMpqName, szPlainName);
return CreateNewArchive_FullPath(pLogger, szMpqName, dwCreateFlags, dwMaxFileCount, phMpq);
}
#ifdef _UNICODE
static int CreateNewArchive(TLogHelper * pLogger, const char * szPlainName, DWORD dwCreateFlags, DWORD dwMaxFileCount, HANDLE * phMpq)
{
TCHAR szMpqName[MAX_PATH];
CreateFullPathName(szMpqName, NULL, szPlainName);
return CreateNewArchive_FullPath(pLogger, szMpqName, dwCreateFlags, dwMaxFileCount, phMpq);
}
#endif
static int OpenExistingArchive(TLogHelper * pLogger, const char * szFileName, const char * szCopyName, HANDLE * phMpq)
{
TCHAR szMpqName[MAX_PATH];
HANDLE hMpq = NULL;
DWORD dwFlags = 0;
int nError = ERROR_SUCCESS;
// We expect MPQ directory to be already prepared by InitializeMpqDirectory
assert(szMpqDirectory[0] != 0);
// At least one name must be entered
assert(szFileName != NULL || szCopyName != NULL);
// If both names entered, create a copy
if(szFileName != NULL && szCopyName != NULL)
{
nError = CreateMpqCopy(pLogger, szFileName, szCopyName, szMpqName);
if(nError != ERROR_SUCCESS)
return nError;
}
// If only source name entered, open it for read-only access
else if(szFileName != NULL && szCopyName == NULL)
{
CreateFullPathName(szMpqName, szMpqSubDir, szFileName);
dwFlags |= MPQ_OPEN_READ_ONLY;
}
// If only target name entered, open it directly
else if(szFileName == NULL && szCopyName != NULL)
{
CreateFullPathName(szMpqName, NULL, szCopyName);
}
// Is it an encrypted MPQ ?
if(_tcsstr(szMpqName, _T(".MPQE")) != NULL)
dwFlags |= MPQ_OPEN_ENCRYPTED;
// Open the copied archive
pLogger->PrintProgress("Opening archive %s ...", (szCopyName != NULL) ? szCopyName : szFileName);
if(!SFileOpenArchive(szMpqName, 0, dwFlags, &hMpq))
return pLogger->PrintError(_T("Failed to open archive %s"), szMpqName);
// Store the archive handle or close the archive
if(phMpq == NULL)
SFileCloseArchive(hMpq);
else
*phMpq = hMpq;
return nError;
}
static int OpenPatchedArchive(TLogHelper * pLogger, HANDLE * phMpq, const char * PatchList[])
{
TCHAR szMpqName[MAX_PATH];
HANDLE hMpq = NULL;
int nError = ERROR_SUCCESS;
// The first file is expected to be valid
assert(PatchList[0] != NULL);
// Open the primary MPQ
CreateFullPathName(szMpqName, szMpqSubDir, PatchList[0]);
pLogger->PrintProgress("Opening base MPQ %s ...", PatchList[0]);
if(!SFileOpenArchive(szMpqName, 0, MPQ_OPEN_READ_ONLY, &hMpq))
nError = pLogger->PrintError(_T("Failed to open the archive %s"), szMpqName);
// Add all patches
if(nError == ERROR_SUCCESS)
{
for(size_t i = 1; PatchList[i] != NULL; i++)
{
CreateFullPathName(szMpqName, szMpqPatchDir, PatchList[i]);
pLogger->PrintProgress("Adding patch %s ...", PatchList[i]);
if(!SFileOpenPatchArchive(hMpq, szMpqName, NULL, 0))
{
nError = pLogger->PrintError(_T("Failed to add patch %s ..."), szMpqName);
break;
}
}
}
// Store the archive handle or close the archive
if(phMpq == NULL)
SFileCloseArchive(hMpq);
else
*phMpq = hMpq;
return nError;
}
static int AddFileToMpq(
TLogHelper * pLogger,
HANDLE hMpq,
const char * szFileName,
const char * szFileData,
DWORD dwFlags = 0,
DWORD dwCompression = 0,
bool bMustSucceed = false)
{
HANDLE hFile = NULL;
DWORD dwFileSize = (DWORD)strlen(szFileData);
int nError = ERROR_SUCCESS;
// Notify the user
pLogger->PrintProgress("Adding file %s ...", szFileName);
// Get the default flags
if(dwFlags == 0)
dwFlags = MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED;
if(dwCompression == 0)
dwCompression = MPQ_COMPRESSION_ZLIB;
// Create the file within the MPQ
if(!SFileCreateFile(hMpq, szFileName, 0, dwFileSize, 0, dwFlags, &hFile))
{
// If success is not expected, it is actually a good thing
if(bMustSucceed == true)
return pLogger->PrintError("Failed to create MPQ file %s", szFileName);
return GetLastError();
}
// Write the file
if(!SFileWriteFile(hFile, szFileData, dwFileSize, dwCompression))
nError = pLogger->PrintError("Failed to write data to the MPQ");
SFileCloseFile(hFile);
return nError;
}
static int AddLocalFileToMpq(
TLogHelper * pLogger,
HANDLE hMpq,
const char * szArchivedName,
const TCHAR * szFileName,
DWORD dwFlags = 0,
DWORD dwCompression = 0,
bool bMustSucceed = false)
{
DWORD dwVerifyResult;
// Notify the user
pLogger->PrintProgress("Adding file %s (%u of %u)...", szArchivedName, pLogger->UserCount, pLogger->UserTotal);
pLogger->UserString = szArchivedName;
// Get the default flags
if(dwFlags == 0)
dwFlags = MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED;
if(dwCompression == 0)
dwCompression = MPQ_COMPRESSION_ZLIB;
// Set the notification callback
SFileSetAddFileCallback(hMpq, AddFileCallback, pLogger);
// Add the file to the MPQ
if(!SFileAddFileEx(hMpq, szFileName, szArchivedName, dwFlags, dwCompression, MPQ_COMPRESSION_NEXT_SAME))
{
if(bMustSucceed)
return pLogger->PrintError("Failed to add the file %s", szArchivedName);
return GetLastError();
}
// Verify the file unless it was lossy compression
if((dwCompression & (MPQ_COMPRESSION_ADPCM_MONO | MPQ_COMPRESSION_ADPCM_STEREO)) == 0)
{
// Notify the user
pLogger->PrintProgress("Verifying file %s (%u of %u) ...", szArchivedName, pLogger->UserCount, pLogger->UserTotal);
// Perform the verification
dwVerifyResult = SFileVerifyFile(hMpq, szArchivedName, MPQ_ATTRIBUTE_CRC32 | MPQ_ATTRIBUTE_MD5);
if(dwVerifyResult & (VERIFY_OPEN_ERROR | VERIFY_READ_ERROR | VERIFY_FILE_SECTOR_CRC_ERROR | VERIFY_FILE_CHECKSUM_ERROR | VERIFY_FILE_MD5_ERROR))
return pLogger->PrintError("CRC error on %s", szArchivedName);
}
return ERROR_SUCCESS;
}
static int RenameMpqFile(TLogHelper * pLogger, HANDLE hMpq, const char * szOldFileName, const char * szNewFileName, bool bMustSucceed)
{
// Notify the user
pLogger->PrintProgress("Renaming %s to %s ...", szOldFileName, szNewFileName);
// Perform the deletion
if(!SFileRenameFile(hMpq, szOldFileName, szNewFileName))
{
if(bMustSucceed == true)
return pLogger->PrintErrorVa("Failed to rename %s to %s", szOldFileName, szNewFileName);
return GetLastError();
}
return ERROR_SUCCESS;
}
static int RemoveMpqFile(TLogHelper * pLogger, HANDLE hMpq, const char * szFileName, bool bMustSucceed)
{
// Notify the user
pLogger->PrintProgress("Removing file %s ...", szFileName);
// Perform the deletion
if(!SFileRemoveFile(hMpq, szFileName, 0))
{
if(bMustSucceed == true)
return pLogger->PrintError("Failed to remove the file %s from the archive", szFileName);
return GetLastError();
}
return ERROR_SUCCESS;
}
//-----------------------------------------------------------------------------
// Tests
static void TestGetFileInfo(
TLogHelper * pLogger,
HANDLE hMpqOrFile,
SFileInfoClass InfoClass,
void * pvFileInfo,
DWORD cbFileInfo,
DWORD * pcbLengthNeeded,
bool bExpectedResult,
int nExpectedError)
{
bool bResult;
int nError = ERROR_SUCCESS;
// Call the get file info
bResult = SFileGetFileInfo(hMpqOrFile, InfoClass, pvFileInfo, cbFileInfo, pcbLengthNeeded);
if(!bResult)
nError = GetLastError();
if(bResult != bExpectedResult)
pLogger->PrintMessage("Different result of SFileGetFileInfo.");
if(nError != nExpectedError)
pLogger->PrintMessage("Different error from SFileGetFileInfo (expected %u, returned %u)", nExpectedError, nError);
}
static int TestVerifyFileChecksum(const char * szFullPath)
{
const char * szShortPlainName = GetShortPlainName(szFullPath);
unsigned char sha1_digest[SHA1_DIGEST_SIZE];
TFileStream * pStream;
TFileData * pFileData;
hash_state sha1_state;
ULONGLONG ByteOffset = 0;
ULONGLONG FileSize = 0;
char * szExtension;
LPBYTE pbFileBlock;
char szShaFileName[MAX_PATH];
char Sha1Text[0x40];
DWORD cbBytesToRead;
DWORD cbFileBlock = 0x10000;
size_t nLength;
int nError = ERROR_SUCCESS;
// Try to load the file with the SHA extension
strcpy(szShaFileName, szFullPath);
szExtension = strrchr(szShaFileName, '.');
if(szExtension == NULL)
return ERROR_SUCCESS;
// Skip .SHA and .TXT files
if(!_stricmp(szExtension, ".sha") || !_stricmp(szExtension, ".txt"))
return ERROR_SUCCESS;
// Load the local file to memory
strcpy(szExtension, ".sha");
pFileData = LoadLocalFile(NULL, szShaFileName, false);
if(pFileData != NULL)
{
TLogHelper Logger("VerifyFileHash", szShortPlainName);
// Open the file to be verified
pStream = FileStream_OpenFile(szFullPath, STREAM_FLAG_READ_ONLY);
if(pStream != NULL)
{
// Notify the user
Logger.PrintProgress("Verifying file %s", szShortPlainName);
// Retrieve the size of the file
FileStream_GetSize(pStream, &FileSize);
// Allocate the buffer for loading file parts
pbFileBlock = STORM_ALLOC(BYTE, cbFileBlock);
if(pbFileBlock != NULL)
{
// Initialize SHA1 calculation
sha1_init(&sha1_state);
// Calculate the SHA1 of the file
while(ByteOffset < FileSize)
{
// Notify the user
Logger.PrintProgress("Verifying file %s (%I64u of %I64u)", szShortPlainName, ByteOffset, FileSize);
// Load the file block
cbBytesToRead = ((FileSize - ByteOffset) > cbFileBlock) ? cbFileBlock : (DWORD)(FileSize - ByteOffset);
if(!FileStream_Read(pStream, &ByteOffset, pbFileBlock, cbBytesToRead))
{
nError = GetLastError();
break;
}
// Add to SHA1
sha1_process(&sha1_state, pbFileBlock, cbBytesToRead);
ByteOffset += cbBytesToRead;
}
// Finalize SHA1
sha1_done(&sha1_state, sha1_digest);
STORM_FREE(pbFileBlock);
// Compare with what we loaded from the file
if(pFileData->dwFileSize >= (SHA1_DIGEST_SIZE * 2))
{
// Compare the Sha1
nLength = ConvertSha1ToText(sha1_digest, Sha1Text);
if(_strnicmp(Sha1Text, (char *)pFileData->FileData, nLength))
{
Logger.PrintError("File CRC check failed: %s", szFullPath);
nError = ERROR_FILE_CORRUPT;
}
}
}
// Close the file
FileStream_Close(pStream);
}
STORM_FREE(pFileData);
}
return nError;
}
// StormLib is able to open local files (as well as the original Storm.dll)
// I want to keep this for occasional use
static int TestOpenLocalFile(const char * szPlainName)
{
TLogHelper Logger("OpenLocalFile", szPlainName);
HANDLE hFile;
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = 0;
char szFileName1[MAX_PATH];
char szFileName2[MAX_PATH];
char szFileLine[0x40];
CreateFullPathName(szFileName1, szMpqSubDir, szPlainName);
if(SFileOpenFileEx(NULL, szFileName1, SFILE_OPEN_LOCAL_FILE, &hFile))
{
// Retrieve the file name. It must match the name under which the file was open
SFileGetFileName(hFile, szFileName2);
if(strcmp(szFileName2, szFileName1))
Logger.PrintMessage("The retrieved name does not match the open name");
// Retrieve the file size
dwFileSizeLo = SFileGetFileSize(hFile, &dwFileSizeHi);
if(dwFileSizeHi != 0 || dwFileSizeLo != 3904784)
Logger.PrintMessage("Local file size mismatch");
// Read the first line
memset(szFileLine, 0, sizeof(szFileLine));
SFileReadFile(hFile, szFileLine, 18, NULL, NULL);
if(strcmp(szFileLine, "(1)Enslavers01.scm"))
Logger.PrintMessage("Content of the listfile does not match");
SFileCloseFile(hFile);
}
return ERROR_SUCCESS;
}
//
static int TestPartFileRead(const char * szPlainName)
{
TLogHelper Logger("PartFileRead", szPlainName);
TMPQHeader Header;
ULONGLONG ByteOffset;
ULONGLONG FileSize = 0;
TFileStream * pStream;
TCHAR szFileName[MAX_PATH];
BYTE Buffer[0x100];
int nError = ERROR_SUCCESS;
// Open the partial file
CreateFullPathName(szFileName, szMpqSubDir, szPlainName);
pStream = FileStream_OpenFile(szFileName, STREAM_PROVIDER_PARTIAL | BASE_PROVIDER_FILE | STREAM_FLAG_READ_ONLY);
if(pStream == NULL)
nError = Logger.PrintError(_T("Failed to open %s"), szFileName);
// Get the size of the stream
if(nError == ERROR_SUCCESS)
{
if(!FileStream_GetSize(pStream, &FileSize))
nError = Logger.PrintError("Failed to retrieve virtual file size");
}
// Read the MPQ header
if(nError == ERROR_SUCCESS)
{
ByteOffset = 0;
if(!FileStream_Read(pStream, &ByteOffset, &Header, MPQ_HEADER_SIZE_V2))
nError = Logger.PrintError("Failed to read the MPQ header");
if(Header.dwID != ID_MPQ || Header.dwHeaderSize != MPQ_HEADER_SIZE_V2)
nError = Logger.PrintError("MPQ Header error");
}
// Read the last 0x100 bytes
if(nError == ERROR_SUCCESS)
{
ByteOffset = FileSize - sizeof(Buffer);
if(!FileStream_Read(pStream, &ByteOffset, Buffer, sizeof(Buffer)))
nError = Logger.PrintError("Failed to read from the file");
}
// Read 0x100 bytes from position (FileSize - 0xFF)
// This test must fail
if(nError == ERROR_SUCCESS)
{
ByteOffset = FileSize - sizeof(Buffer) + 1;
if(FileStream_Read(pStream, &ByteOffset, Buffer, sizeof(Buffer)))
nError = Logger.PrintError("Test Failed: Reading 0x100 bytes from (FileSize - 0xFF)");
}
FileStream_Close(pStream);
return nError;
}
static int TestOpenFile_OpenById(const char * szPlainName)
{
TLogHelper Logger("OpenFileById", szPlainName);
TFileData * pFileData1 = NULL;
TFileData * pFileData2 = NULL;
HANDLE hMpq;
int nError;
// Copy the archive so we won't fuck up the original one
nError = OpenExistingArchive(&Logger, szPlainName, NULL, &hMpq);
// Now try to open a file without knowing the file name
if(nError == ERROR_SUCCESS)
{
// File00000023.xxx = music\dintro.wav
pFileData1 = LoadMpqFile(&Logger, hMpq, "File00000023.xxx");
if(pFileData1 == NULL)
nError = Logger.PrintError("Failed to load the file %s", "File00000023.xxx");
}
// Now try to open the file again with its original name
if(nError == ERROR_SUCCESS)
{
// File00000023.xxx = music\dintro.wav
pFileData2 = LoadMpqFile(&Logger, hMpq, "music\\dintro.wav");
if(pFileData2 == NULL)
nError = Logger.PrintError("Failed to load the file %s", "music\\dintro.wav");
}
// Now compare both files
if(nError == ERROR_SUCCESS)
{
if(!CompareTwoFiles(&Logger, pFileData1, pFileData1))
nError = Logger.PrintError("The file has different size/content when open without name");
}
// Close the archive
if(pFileData2 != NULL)
STORM_FREE(pFileData2);
if(pFileData1 != NULL)
STORM_FREE(pFileData1);
if(hMpq != NULL)
SFileCloseArchive(hMpq);
return nError;
}
// Open an empty archive (found in WoW cache - it's just a header)
static int TestOpenArchive(const char * szPlainName, const char * szListFile = NULL)
{
TLogHelper Logger("OpenMpqTest", szPlainName);
TFileData * pFileData;
HANDLE hMpq;
DWORD dwFileCount = 0;
char szListFileBuff[MAX_PATH];
int nError;
// Copy the archive so we won't fuck up the original one
nError = OpenExistingArchive(&Logger, szPlainName, NULL, &hMpq);
if(nError == ERROR_SUCCESS)
{
// If the listfile was given, add it to the MPQ
if(szListFile != NULL)
{
Logger.PrintProgress("Adding listfile %s ...", szListFile);
CreateFullPathName(szListFileBuff, szMpqSubDir, szListFile);
nError = SFileAddListFile(hMpq, szListFileBuff);
if(nError != ERROR_SUCCESS)
Logger.PrintMessage("Failed to add the listfile to the MPQ");
}
// Attempt to open the listfile and attributes
if(SFileHasFile(hMpq, LISTFILE_NAME))
{
pFileData = LoadMpqFile(&Logger, hMpq, LISTFILE_NAME);
if(pFileData != NULL)
STORM_FREE(pFileData);
}
// Attempt to open the listfile and attributes
if(SFileHasFile(hMpq, ATTRIBUTES_NAME))
{
pFileData = LoadMpqFile(&Logger, hMpq, ATTRIBUTES_NAME);
if(pFileData != NULL)
STORM_FREE(pFileData);
}
// Search the archive and load every file
nError = SearchArchive(&Logger, hMpq, TEST_FLAG_LOAD_FILES, &dwFileCount);
SFileCloseArchive(hMpq);
}
return nError;
}
// Opens a patched MPQ archive
static int TestOpenArchive_Patched(const char * PatchList[], const char * szPatchedFile = NULL, int nExpectedPatchCount = 0)
{
TLogHelper Logger("OpenPatchedMpqTest", PatchList[0]);
HANDLE hMpq;
DWORD dwFileCount = 0;
int nError;
// Open a patched MPQ archive
nError = OpenPatchedArchive(&Logger, &hMpq, PatchList);
if(nError == ERROR_SUCCESS)
{
// Check patch count
if(szPatchedFile != NULL)
nError = VerifyFilePatchCount(&Logger, hMpq, szPatchedFile, nExpectedPatchCount);
// Search the archive and load every file
if(nError == ERROR_SUCCESS)
nError = SearchArchive(&Logger, hMpq, TEST_FLAG_LOAD_FILES, &dwFileCount);
// Close the archive
SFileCloseArchive(hMpq);
}
return nError;
}
// Open an archive for read-only access
static int TestOpenArchive_ReadOnly(const char * szPlainName, bool bReadOnly)
{
const char * szCopyName;
TLogHelper Logger("ReadOnlyTest", szPlainName);
HANDLE hMpq;
TCHAR szMpqName[MAX_PATH];
DWORD dwFlags = 0;
bool bMustSucceed;
int nError;
// Copy the fiel so we wont screw up something
szCopyName = bReadOnly ? "StormLibTest_ReadOnly.mpq" : "StormLibTest_ReadWrite.mpq";
nError = CreateMpqCopy(&Logger, szPlainName, szCopyName, szMpqName);
// Now open the archive for read-only access
if(nError == ERROR_SUCCESS)
{
Logger.PrintProgress("Opening archive %s ...", szCopyName);
dwFlags = bReadOnly ? MPQ_OPEN_READ_ONLY : 0;
if(!SFileOpenArchive(szMpqName, 0, dwFlags, &hMpq))
nError = Logger.PrintError("Failed to open the archive %s", szCopyName);
}
// Now try to add a file. This must fail if the MPQ is read only
if(nError == ERROR_SUCCESS)
{
bMustSucceed = (bReadOnly == false);
nError = AddFileToMpq(&Logger, hMpq, "AddedFile.txt", "This is an added file.", MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED, 0, bMustSucceed);
if(nError != ERROR_SUCCESS && bMustSucceed == false)
nError = ERROR_SUCCESS;
}
// Now try to rename a file in the MPQ. This must only succeed if the MPQ is not read only
if(nError == ERROR_SUCCESS)
{
bMustSucceed = (bReadOnly == false);
nError = RenameMpqFile(&Logger, hMpq, "spawn.mpq", "spawn-renamed.mpq", bMustSucceed);
if(nError != ERROR_SUCCESS && bMustSucceed == false)
nError = ERROR_SUCCESS;
}
// Now try to delete a file in the MPQ. This must only succeed if the MPQ is not read only
if(nError == ERROR_SUCCESS)
{
bMustSucceed = (bReadOnly == false);
nError = RemoveMpqFile(&Logger, hMpq, "spawn-renamed.mpq", bMustSucceed);
if(nError != ERROR_SUCCESS && bMustSucceed == false)
nError = ERROR_SUCCESS;
}
// Close the archive
if(hMpq != NULL)
SFileCloseArchive(hMpq);
return nError;
}
static int TestOpenArchive_GetFileInfo(const char * szPlainName1, const char * szPlainName4)
{
TLogHelper Logger("GetFileInfoTest");
HANDLE hFile;
HANDLE hMpq4;
HANDLE hMpq1;
DWORD cbLength;
BYTE DataBuff[0x400];
int nError1;
int nError4;
// Copy the archive so we won't fuck up the original one
nError1 = OpenExistingArchive(&Logger, szPlainName1, NULL, &hMpq1);
nError4 = OpenExistingArchive(&Logger, szPlainName4, NULL, &hMpq4);
if(nError1 == ERROR_SUCCESS && nError4 == ERROR_SUCCESS)
{
// Invalid handle - expected (false, ERROR_INVALID_HANDLE)
TestGetFileInfo(&Logger, NULL, SFileMpqBetHeader, NULL, 0, NULL, false, ERROR_INVALID_HANDLE);
// Valid handle but invalid value of file info class (false, ERROR_INVALID_PARAMETER)
TestGetFileInfo(&Logger, NULL, (SFileInfoClass)0xFFF, NULL, 0, NULL, false, ERROR_INVALID_PARAMETER);
// Valid archive handle but file info class is for file (false, ERROR_INVALID_HANDLE)
TestGetFileInfo(&Logger, NULL, SFileInfoNameHash1, NULL, 0, NULL, false, ERROR_INVALID_HANDLE);
// Valid handle and all parameters NULL
// Returns (true, ERROR_SUCCESS), if BET table is present, otherwise (false, ERROR_CAN_NOT_COMPLETE)
TestGetFileInfo(&Logger, hMpq1, SFileMpqBetHeader, NULL, 0, NULL, false, ERROR_FILE_NOT_FOUND);
TestGetFileInfo(&Logger, hMpq4, SFileMpqBetHeader, NULL, 0, NULL, true, ERROR_SUCCESS);
// Now try to retrieve the required size of the BET table header
TestGetFileInfo(&Logger, hMpq4, SFileMpqBetHeader, NULL, 0, &cbLength, true, ERROR_SUCCESS);
// When we call SFileInfo with buffer = NULL and nonzero buffer size, it is ignored
TestGetFileInfo(&Logger, hMpq4, SFileMpqBetHeader, NULL, 3, &cbLength, true, ERROR_SUCCESS);
// When we call SFileInfo with buffer != NULL and nonzero buffer size, it should return error
TestGetFileInfo(&Logger, hMpq4, SFileMpqBetHeader, DataBuff, 3, &cbLength, false, ERROR_INSUFFICIENT_BUFFER);
// Request for bet table header should also succeed if we want header only
TestGetFileInfo(&Logger, hMpq4, SFileMpqBetHeader, DataBuff, sizeof(TMPQBetHeader), &cbLength, true, ERROR_SUCCESS);
// Request for bet table header should also succeed if we want header+flag table only
TestGetFileInfo(&Logger, hMpq4, SFileMpqBetHeader, DataBuff, sizeof(DataBuff), &cbLength, true, ERROR_SUCCESS);
// Try to retrieve strong signature from the MPQ
TestGetFileInfo(&Logger, hMpq1, SFileMpqStrongSignature, NULL, 0, NULL, true, ERROR_SUCCESS);
TestGetFileInfo(&Logger, hMpq4, SFileMpqStrongSignature, NULL, 0, NULL, false, ERROR_FILE_NOT_FOUND);
// Strong signature is returned including the signature ID
TestGetFileInfo(&Logger, hMpq1, SFileMpqStrongSignature, NULL, 0, &cbLength, true, ERROR_SUCCESS);
assert(cbLength == MPQ_STRONG_SIGNATURE_SIZE + 4);
// Retrieve the signature
TestGetFileInfo(&Logger, hMpq1, SFileMpqStrongSignature, DataBuff, sizeof(DataBuff), &cbLength, true, ERROR_SUCCESS);
assert(memcmp(DataBuff, "NGIS", 4) == 0);
// Check SFileGetFileInfo on
if(SFileOpenFileEx(hMpq4, LISTFILE_NAME, 0, &hFile))
{
// Valid parameters but the handle should be file handle
TestGetFileInfo(&Logger, hMpq4, SFileInfoFileTime, DataBuff, sizeof(DataBuff), &cbLength, false, ERROR_INVALID_HANDLE);
// Valid parameters
TestGetFileInfo(&Logger, hFile, SFileInfoFileTime, DataBuff, sizeof(DataBuff), &cbLength, true, ERROR_SUCCESS);
SFileCloseFile(hFile);
}
}
if(hMpq4 != NULL)
SFileCloseArchive(hMpq4);
if(hMpq1 != NULL)
SFileCloseArchive(hMpq1);
return ERROR_SUCCESS;
}
static int TestOpenArchive_VerifySignature(const char * szPlainName, const char * szOriginalName)
{
TLogHelper Logger("VerifySignatureTest", szPlainName);
HANDLE hMpq;
DWORD dwSignatures = 0;
int nVerifyError;
int nError = ERROR_SUCCESS;
// We need original name for the signature check
nError = OpenExistingArchive(&Logger, szPlainName, szOriginalName, &hMpq);
if(nError == ERROR_SUCCESS)
{
// Query the signature types
Logger.PrintProgress("Retrieving signatures ...");
TestGetFileInfo(&Logger, hMpq, SFileMpqSignatures, &dwSignatures, sizeof(DWORD), NULL, true, ERROR_SUCCESS);
// Verify any of the present signatures
Logger.PrintProgress("Verifying archive signature ...");
nVerifyError = SFileVerifyArchive(hMpq);
// Verify the result
if((dwSignatures & SIGNATURE_TYPE_STRONG) && (nVerifyError != ERROR_STRONG_SIGNATURE_OK))
{
Logger.PrintMessage("Strong signature verification error");
nError = ERROR_FILE_CORRUPT;
}
// Verify the result
if((dwSignatures & SIGNATURE_TYPE_WEAK) && (nVerifyError != ERROR_WEAK_SIGNATURE_OK))
{
Logger.PrintMessage("Weak signature verification error");
nError = ERROR_FILE_CORRUPT;
}
SFileCloseArchive(hMpq);
}
return nError;
}
// Open an empty archive (found in WoW cache - it's just a header)
static int TestOpenArchive_CraftedUserData(const char * szPlainName, const char * szCopyName)
{
TLogHelper Logger("CraftedMpqTest", szPlainName);
HANDLE hMpq;
DWORD dwFileCount1 = 0;
DWORD dwFileCount2 = 0;
TCHAR szMpqName[MAX_PATH];
BYTE FileHash1[MD5_DIGEST_SIZE];
BYTE FileHash2[MD5_DIGEST_SIZE];
int nError;
// Create copy of the archive, with interleaving some user data
nError = CreateMpqCopy(&Logger, szPlainName, szCopyName, szMpqName, 0x400, 0x531);
// Open the archive and load some files
if(nError == ERROR_SUCCESS)
{
Logger.PrintProgress("Opening archive %s ...", szCopyName);
if(!SFileOpenArchive(szMpqName, 0, 0, &hMpq))
return Logger.PrintError(_T("Failed to open archive %s"), szMpqName);
// Verify presence of (listfile) and (attributes)
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, true);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, true);
// Search the archive and load every file
nError = SearchArchive(&Logger, hMpq, TEST_FLAG_LOAD_FILES | TEST_FLAG_HASH_FILES, &dwFileCount1, FileHash1);
SFileCloseArchive(hMpq);
}
// Try to compact the MPQ
if(nError == ERROR_SUCCESS)
{
// Open the archive again
Logger.PrintProgress("Reopening archive %s ...", szCopyName);
if(!SFileOpenArchive(szMpqName, 0, 0, &hMpq))
return Logger.PrintError(_T("Failed to open archive %s"), szMpqName);
// Compact the archive
Logger.PrintProgress("Compacting archive %s ...", szMpqName);
if(!SFileSetCompactCallback(hMpq, CompactCallback, &Logger))
nError = Logger.PrintError(_T("Failed to compact archive %s"), szMpqName);
SFileCompactArchive(hMpq, NULL, false);
SFileCloseArchive(hMpq);
}
// Open the archive and load some files
if(nError == ERROR_SUCCESS)
{
Logger.PrintProgress("Reopening archive %s ...", szCopyName);
if(!SFileOpenArchive(szMpqName, 0, 0, &hMpq))
return Logger.PrintError(_T("Failed to open archive %s"), szMpqName);
// Verify presence of (listfile) and (attributes)
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, true);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, true);
// Search the archive and load every file
nError = SearchArchive(&Logger, hMpq, TEST_FLAG_LOAD_FILES | TEST_FLAG_HASH_FILES, &dwFileCount2, FileHash2);
SFileCloseArchive(hMpq);
}
// Compare the file counts and their hashes
if(nError == ERROR_SUCCESS)
{
if(dwFileCount2 != dwFileCount1)
Logger.PrintMessage("Different file count after compacting archive: %u vs %u", dwFileCount2, dwFileCount1);
if(memcmp(FileHash2, FileHash1, MD5_DIGEST_SIZE))
Logger.PrintMessage("Different file hash after compacting archive");
}
return nError;
}
// Adding a file to MPQ that had no (listfile) and no (attributes).
// We expect that neither of these will be present after the archive is closed
static int TestAddFile_ListFileTest(const char * szSourceMpq, bool bShouldHaveListFile, bool bShouldHaveAttributes)
{
TLogHelper Logger("ListFileTest", szSourceMpq);
TFileData * pFileData = NULL;
const char * szBackupMpq = bShouldHaveListFile ? "StormLibTest_HasListFile.mpq" : "StormLibTest_NoListFile.mpq";
const char * szFileName = "AddedFile001.txt";
const char * szFileData = "0123456789ABCDEF";
HANDLE hMpq = NULL;
DWORD dwFileSize = (DWORD)strlen(szFileData);
int nError = ERROR_SUCCESS;
// Copy the archive so we won't fuck up the original one
nError = OpenExistingArchive(&Logger, szSourceMpq, szBackupMpq, &hMpq);
// Add a file
if(nError == ERROR_SUCCESS)
{
// Now add a file
nError = AddFileToMpq(&Logger, hMpq, szFileName, szFileData, MPQ_FILE_IMPLODE, MPQ_COMPRESSION_PKWARE);
SFileCloseArchive(hMpq);
}
// Now reopen the archive
if(nError == ERROR_SUCCESS)
nError = OpenExistingArchive(&Logger, NULL, szBackupMpq, &hMpq);
// Now the file has been written and the MPQ has been saved.
// We Reopen the MPQ and check if there is no (listfile) nor (attributes).
if(nError == ERROR_SUCCESS)
{
// Verify presence of (listfile) and (attributes)
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, bShouldHaveListFile);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, bShouldHaveAttributes);
// Try to open the file that we recently added
pFileData = LoadMpqFile(&Logger, hMpq, szFileName);
if(pFileData != NULL)
{
// Verify if the file size matches
if(pFileData->dwFileSize == dwFileSize)
{
// Verify if the file data match
if(memcmp(pFileData->FileData, szFileData, dwFileSize))
{
Logger.PrintError("The data of the added file does not match");
nError = ERROR_FILE_CORRUPT;
}
}
else
{
Logger.PrintError("The size of the added file does not match");
nError = ERROR_FILE_CORRUPT;
}
// Delete the file data
STORM_FREE(pFileData);
}
else
{
nError = Logger.PrintError("Failed to open the file previously added");
}
}
// Close the MPQ archive
if(hMpq != NULL)
SFileCloseArchive(hMpq);
return nError;
}
static int TestCreateArchive_EmptyMpq(const char * szPlainName, DWORD dwCreateFlags)
{
TLogHelper Logger("CreateEmptyMpq", szPlainName);
HANDLE hMpq = NULL;
DWORD dwFileCount = 0;
int nError;
// Create the full path name
nError = CreateNewArchive(&Logger, szPlainName, dwCreateFlags, 0, &hMpq);
if(nError == ERROR_SUCCESS)
{
SearchArchive(&Logger, hMpq);
SFileCloseArchive(hMpq);
}
// Reopen the empty MPQ
if(nError == ERROR_SUCCESS)
{
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
if(nError == ERROR_SUCCESS)
{
SFileGetFileInfo(hMpq, SFileMpqNumberOfFiles, &dwFileCount, sizeof(dwFileCount), NULL);
CheckIfFileIsPresent(&Logger, hMpq, "File00000000.xxx", false);
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, false);
SearchArchive(&Logger, hMpq);
SFileCloseArchive(hMpq);
}
}
return nError;
}
static int TestCreateArchive_FillArchive(const char * szPlainName)
{
TLogHelper Logger("CreateFullMpq", szPlainName);
const char * szFileData = "TestCreateArchive_FillArchive: Testing file data";
char szFileName[MAX_PATH];
HANDLE hMpq = NULL;
DWORD dwMaxFileCount = 6;
DWORD dwCompression = MPQ_COMPRESSION_ZLIB;
DWORD dwFlags = MPQ_FILE_ENCRYPTED | MPQ_FILE_COMPRESS;
int nError;
// Create the new MPQ
nError = CreateNewArchive(&Logger, szPlainName, 0, dwMaxFileCount, &hMpq);
// Now we should be able to add 6 files
if(nError == ERROR_SUCCESS)
{
for(DWORD i = 0; i < dwMaxFileCount; i++)
{
sprintf(szFileName, "AddedFile%03u.txt", i);
nError = AddFileToMpq(&Logger, hMpq, szFileName, szFileData, dwFlags, dwCompression);
if(nError != ERROR_SUCCESS)
break;
}
}
// Now the MPQ should be full. It must not be possible to add another file
if(nError == ERROR_SUCCESS)
{
nError = AddFileToMpq(&Logger, hMpq, "ShouldNotBeHere.txt", szFileData, MPQ_FILE_COMPRESS, MPQ_COMPRESSION_ZLIB, false);
assert(nError != ERROR_SUCCESS);
nError = ERROR_SUCCESS;
}
// Close the archive to enforce saving all tables
if(hMpq != NULL)
SFileCloseArchive(hMpq);
hMpq = NULL;
// Reopen the archive again
if(nError == ERROR_SUCCESS)
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
// The archive should still be full
if(nError == ERROR_SUCCESS)
{
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, true);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, true);
AddFileToMpq(&Logger, hMpq, "ShouldNotBeHere.txt", szFileData, MPQ_FILE_COMPRESS, MPQ_COMPRESSION_ZLIB, false);
}
// The (listfile) must be present
if(nError == ERROR_SUCCESS)
{
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, true);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, true);
nError = RemoveMpqFile(&Logger, hMpq, szFileName, true);
}
// Now add the file again. This time, it should be possible OK
if(nError == ERROR_SUCCESS)
{
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, false);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, false);
nError = AddFileToMpq(&Logger, hMpq, szFileName, szFileData, dwFlags, dwCompression, true);
}
// Now add the file again. This time, it should be fail
if(nError == ERROR_SUCCESS)
{
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, false);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, false);
AddFileToMpq(&Logger, hMpq, szFileName, szFileData, dwFlags, dwCompression, false);
}
// Close the archive and return
if(hMpq != NULL)
SFileCloseArchive(hMpq);
hMpq = NULL;
// Reopen the archive for the third time to verify that both internal files are there
if(nError == ERROR_SUCCESS)
{
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
if(nError == ERROR_SUCCESS)
{
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, true);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, true);
SFileCloseArchive(hMpq);
}
}
return nError;
}
static int TestCreateArchive_IncMaxFileCount(const char * szPlainName)
{
TLogHelper Logger("IncMaxFileCount", szPlainName);
const char * szFileData = "TestCreateArchive_IncMaxFileCount: Testing file data";
char szFileName[MAX_PATH];
HANDLE hMpq = NULL;
DWORD dwMaxFileCount = 1;
int nError;
// Create the new MPQ
nError = CreateNewArchive(&Logger, szPlainName, MPQ_CREATE_ARCHIVE_V4, dwMaxFileCount, &hMpq);
// Now add exactly one file
if(nError == ERROR_SUCCESS)
{
nError = AddFileToMpq(&Logger, hMpq, "AddFile_base.txt", szFileData);
SFileFlushArchive(hMpq);
SFileCloseArchive(hMpq);
}
// Now add 10 files. Each time we cannot add the file due to archive being full,
// we increment the max file count
if(nError == ERROR_SUCCESS)
{
for(DWORD i = 0; i < 10; i++)
{
// Open the archive again
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
if(nError != ERROR_SUCCESS)
break;
// Add one file
sprintf(szFileName, "AddFile_%04u.txt", i);
nError = AddFileToMpq(&Logger, hMpq, szFileName, szFileData);
if(nError != ERROR_SUCCESS)
{
// Increment the ma file count by one
dwMaxFileCount = SFileGetMaxFileCount(hMpq) + 1;
Logger.PrintProgress("Increasing max file count to %u ...", dwMaxFileCount);
SFileSetMaxFileCount(hMpq, dwMaxFileCount);
// Attempt to create the file again
nError = AddFileToMpq(&Logger, hMpq, szFileName, szFileData, 0, 0, true);
}
// Compact the archive and close it
SFileSetCompactCallback(hMpq, CompactCallback, &Logger);
SFileCompactArchive(hMpq, NULL, false);
SFileCloseArchive(hMpq);
if(nError != ERROR_SUCCESS)
break;
}
}
return nError;
}
static int TestCreateArchive_UnicodeNames()
{
TLogHelper Logger("MpqUnicodeName");
int nError = ERROR_SUCCESS;
#ifdef _UNICODE
nError = CreateNewArchive(&Logger, szUnicodeName1, MPQ_CREATE_ARCHIVE_V1, 15, NULL);
if(nError != ERROR_SUCCESS)
return nError;
nError = CreateNewArchive(&Logger, szUnicodeName2, MPQ_CREATE_ARCHIVE_V2, 58, NULL);
if(nError != ERROR_SUCCESS)
return nError;
nError = CreateNewArchive(&Logger, szUnicodeName3, MPQ_CREATE_ARCHIVE_V3, 15874, NULL);
if(nError != ERROR_SUCCESS)
return nError;
nError = CreateNewArchive(&Logger, szUnicodeName4, MPQ_CREATE_ARCHIVE_V4, 87541, NULL);
if(nError != ERROR_SUCCESS)
return nError;
nError = CreateNewArchive(&Logger, szUnicodeName5, MPQ_CREATE_ARCHIVE_V3, 87541, NULL);
if(nError != ERROR_SUCCESS)
return nError;
nError = CreateNewArchive(&Logger, szUnicodeName5, MPQ_CREATE_ARCHIVE_V2, 87541, NULL);
#endif // _UNICODE
return nError;
}
static int TestCreateArchive_FileFlagTest(const char * szPlainName)
{
TLogHelper Logger("FileFlagTest", szPlainName);
HANDLE hMpq = NULL; // Handle of created archive
TCHAR szFileName1[MAX_PATH];
TCHAR szFileName2[MAX_PATH];
TCHAR szMpqName[MAX_PATH];
const char * szMiddleFile = "FileTest_10.exe";
LCID LocaleIDs[] = {0x000, 0x405, 0x406, 0x407, 0xFFFF};
char szArchivedName[MAX_PATH];
DWORD dwMaxFileCount = 0;
DWORD dwFileCount = 0;
size_t i;
int nError;
// Create paths for local file to be added
CreateFullPathName(szFileName1, szMpqSubDir, "AddFile.exe");
CreateFullPathName(szFileName2, szMpqSubDir, "AddFile.bin");
// Create an empty file that will serve as holder for the MPQ
nError = CreateEmptyFile(&Logger, szPlainName, 0x100000, szMpqName);
// Create new MPQ archive over that file
if(nError == ERROR_SUCCESS)
nError = CreateNewArchive_FullPath(&Logger, szMpqName, MPQ_CREATE_ARCHIVE_V1, 17, &hMpq);
// Add the same file multiple times
if(nError == ERROR_SUCCESS)
{
dwMaxFileCount = SFileGetMaxFileCount(hMpq);
for(i = 0; AddFlags[i] != 0xFFFFFFFF; i++)
{
sprintf(szArchivedName, "FileTest_%02u.exe", i);
nError = AddLocalFileToMpq(&Logger, hMpq, szArchivedName, szFileName1, AddFlags[i], 0);
if(nError != ERROR_SUCCESS)
break;
dwFileCount++;
}
}
// Delete a file in the middle of the file table
if(nError == ERROR_SUCCESS)
{
Logger.PrintProgress("Removing file %s ...", szMiddleFile);
nError = RemoveMpqFile(&Logger, hMpq, szMiddleFile, true);
dwFileCount--;
}
// Add one more file
if(nError == ERROR_SUCCESS)
{
nError = AddLocalFileToMpq(&Logger, hMpq, "FileTest_xx.exe", szFileName1);
dwFileCount++;
}
// Try to decrement max file count. This must succeed
if(nError == ERROR_SUCCESS)
{
Logger.PrintProgress("Attempting to decrement max file count ...");
if(SFileSetMaxFileCount(hMpq, 5))
nError = Logger.PrintError("Max file count decremented, even if it should fail");
}
// Add ZeroSize.txt several times under a different locale
if(nError == ERROR_SUCCESS)
{
for(i = 0; LocaleIDs[i] != 0xFFFF; i++)
{
bool bMustSucceed = ((dwFileCount + 2) < dwMaxFileCount);
SFileSetLocale(LocaleIDs[i]);
nError = AddLocalFileToMpq(&Logger, hMpq, "ZeroSize_1.txt", szFileName2);
if(nError != ERROR_SUCCESS)
{
if(bMustSucceed == false)
nError = ERROR_SUCCESS;
break;
}
dwFileCount++;
}
}
// Add ZeroSize.txt again several times under a different locale
if(nError == ERROR_SUCCESS)
{
for(i = 0; LocaleIDs[i] != 0xFFFF; i++)
{
bool bMustSucceed = ((dwFileCount + 2) < dwMaxFileCount);
SFileSetLocale(LocaleIDs[i]);
nError = AddLocalFileToMpq(&Logger, hMpq, "ZeroSize_2.txt", szFileName2, 0, 0, bMustSucceed);
if(nError != ERROR_SUCCESS)
{
if(bMustSucceed == false)
nError = ERROR_SUCCESS;
break;
}
dwFileCount++;
}
}
// Verify how many files did we add to the MPQ
if(nError == ERROR_SUCCESS)
{
if(dwFileCount + 2 != dwMaxFileCount)
{
Logger.PrintErrorVa("Number of files added to MPQ was unexpected (expected %u, added %u)", dwFileCount, dwMaxFileCount - 2);
nError = ERROR_FILE_CORRUPT;
}
}
// Test rename function
if(nError == ERROR_SUCCESS)
{
Logger.PrintProgress("Testing rename files ...");
SFileSetLocale(LANG_NEUTRAL);
if(!SFileRenameFile(hMpq, "FileTest_08.exe", "FileTest_08a.exe"))
nError = Logger.PrintError("Failed to rename the file");
}
if(nError == ERROR_SUCCESS)
{
if(!SFileRenameFile(hMpq, "FileTest_08a.exe", "FileTest_08.exe"))
nError = Logger.PrintError("Failed to rename the file");
}
if(nError == ERROR_SUCCESS)
{
if(SFileRenameFile(hMpq, "FileTest_10.exe", "FileTest_10a.exe"))
{
Logger.PrintError("Rename test succeeded even if it shouldn't");
nError = ERROR_FILE_CORRUPT;
}
}
if(nError == ERROR_SUCCESS)
{
if(SFileRenameFile(hMpq, "FileTest_10a.exe", "FileTest_10.exe"))
{
Logger.PrintError("Rename test succeeded even if it shouldn't");
nError = ERROR_FILE_CORRUPT;
}
}
// Close the archive
if(hMpq != NULL)
SFileCloseArchive(hMpq);
hMpq = NULL;
// Try to reopen the archive
nError = OpenExistingArchive(&Logger, NULL, szPlainName, NULL);
return nError;
}
static int TestCreateArchive_CompressionsTest(const char * szPlainName)
{
TLogHelper Logger("CompressionsTest", szPlainName);
HANDLE hMpq = NULL; // Handle of created archive
TCHAR szFileName[MAX_PATH]; // Source file to be added
TCHAR szMpqName[MAX_PATH];
char szArchivedName[MAX_PATH];
DWORD dwCmprCount = sizeof(Compressions) / sizeof(DWORD);
DWORD dwAddedFiles = 0;
DWORD dwFoundFiles = 0;
size_t i;
int nError;
// Create paths for local file to be added
CreateFullPathName(szFileName, szMpqSubDir, "AddFile.wav");
CreateFullPathName(szMpqName, NULL, szPlainName);
// Create new archive
nError = CreateNewArchive_FullPath(&Logger, szMpqName, MPQ_CREATE_ARCHIVE_V4, 0x40, &hMpq);
// Add the same file multiple times
if(nError == ERROR_SUCCESS)
{
Logger.UserTotal = dwCmprCount;
for(i = 0; i < dwCmprCount; i++)
{
sprintf(szArchivedName, "WaveFile_%02u.wav", i + 1);
nError = AddLocalFileToMpq(&Logger, hMpq, szArchivedName, szFileName, MPQ_FILE_COMPRESS | MPQ_FILE_ENCRYPTED | MPQ_FILE_SECTOR_CRC, Compressions[i]);
if(nError != ERROR_SUCCESS)
break;
Logger.UserCount++;
dwAddedFiles++;
}
SFileCloseArchive(hMpq);
}
// Reopen the archive extract each WAVE file and try to play it
if(nError == ERROR_SUCCESS)
{
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
if(nError == ERROR_SUCCESS)
{
SearchArchive(&Logger, hMpq, TEST_FLAG_LOAD_FILES | TEST_FLAG_PLAY_WAVES, &dwFoundFiles, NULL);
SFileCloseArchive(hMpq);
}
// Check if the number of found files is the same like the number of added files
// DOn;t forget that there will be (listfile) and (attributes)
if(dwFoundFiles != (dwAddedFiles + 2))
{
Logger.PrintError("Number of found files does not match number of added files.");
nError = ERROR_FILE_CORRUPT;
}
}
return nError;
}
static int TestCreateArchive_ListFilePos(const char * szPlainName)
{
TFileData * pFileData;
const char * szReaddedFile = "AddedFile_##.txt";
const char * szFileMask = "AddedFile_%02u.txt";
TLogHelper Logger("ListFilePos", szPlainName);
HANDLE hMpq = NULL; // Handle of created archive
char szArchivedName[MAX_PATH];
DWORD dwMaxFileCount = 0x1E;
DWORD dwAddedCount = 0;
size_t i;
int nError;
// Create a new archive with the limit of 0x20 files
nError = CreateNewArchive(&Logger, szPlainName, MPQ_CREATE_ARCHIVE_V4, dwMaxFileCount, &hMpq);
// Add 0x1E files
if(nError == ERROR_SUCCESS)
{
for(i = 0; i < dwMaxFileCount; i++)
{
sprintf(szArchivedName, szFileMask, i);
nError = AddFileToMpq(&Logger, hMpq, szArchivedName, "This is a text data.", 0, 0, true);
if(nError != ERROR_SUCCESS)
break;
dwAddedCount++;
}
}
// Delete few middle files
if(nError == ERROR_SUCCESS)
{
for(i = 0; i < (dwMaxFileCount / 2); i++)
{
sprintf(szArchivedName, szFileMask, i);
nError = RemoveMpqFile(&Logger, hMpq, szArchivedName, true);
if(nError != ERROR_SUCCESS)
break;
}
}
// Close the archive
if(hMpq != NULL)
SFileCloseArchive(hMpq);
hMpq = NULL;
// Reopen the archive to catch any asserts
if(nError == ERROR_SUCCESS)
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
// Check that (listfile) is at the end
if(nError == ERROR_SUCCESS)
{
pFileData = LoadMpqFile(&Logger, hMpq, LISTFILE_NAME);
if(pFileData != NULL)
{
if(pFileData->dwBlockIndex < dwAddedCount)
Logger.PrintMessage("Unexpected file index of %s", LISTFILE_NAME);
STORM_FREE(pFileData);
}
pFileData = LoadMpqFile(&Logger, hMpq, ATTRIBUTES_NAME);
if(pFileData != NULL)
{
if(pFileData->dwBlockIndex <= dwAddedCount)
Logger.PrintMessage("Unexpected file index of %s", ATTRIBUTES_NAME);
STORM_FREE(pFileData);
}
// Add new file to the archive. It should be added to position 0
// (since position 0 should be free)
nError = AddFileToMpq(&Logger, hMpq, szReaddedFile, "This is a re-added file.", 0, 0, true);
if(nError == ERROR_SUCCESS)
{
pFileData = LoadMpqFile(&Logger, hMpq, szReaddedFile);
if(pFileData != NULL)
{
if(pFileData->dwBlockIndex != 0)
Logger.PrintMessage("Unexpected file index of %s", szReaddedFile);
STORM_FREE(pFileData);
}
}
SFileCloseArchive(hMpq);
}
return nError;
}
static int TestCreateArchive_BigArchive(const char * szPlainName)
{
const char * szFileMask = "AddedFile_%02u.txt";
TLogHelper Logger("BigMpqTest");
HANDLE hMpq = NULL; // Handle of created archive
TCHAR szFileName[MAX_PATH];
char szArchivedName[MAX_PATH];
DWORD dwMaxFileCount = 0x20;
DWORD dwAddedCount = 0;
size_t i;
int nError;
// Create a new archive with the limit of 0x20 files
nError = CreateNewArchive(&Logger, szPlainName, MPQ_CREATE_ARCHIVE_V3, dwMaxFileCount, &hMpq);
if(nError == ERROR_SUCCESS)
{
// Now add few really big files
CreateFullPathName(szFileName, szMpqSubDir, "MPQ_1997_v1_Diablo1_DIABDAT.MPQ");
Logger.UserTotal = (dwMaxFileCount / 2);
for(i = 0; i < dwMaxFileCount / 2; i++)
{
sprintf(szArchivedName, szFileMask, i + 1);
nError = AddLocalFileToMpq(&Logger, hMpq, szArchivedName, szFileName, 0, 0, true);
if(nError != ERROR_SUCCESS)
break;
Logger.UserCount++;
dwAddedCount++;
}
}
// Close the archive
if(hMpq != NULL)
SFileCloseArchive(hMpq);
hMpq = NULL;
// Reopen the archive to catch any asserts
if(nError == ERROR_SUCCESS)
nError = OpenExistingArchive(&Logger, NULL, szPlainName, &hMpq);
// Check that (listfile) is at the end
if(nError == ERROR_SUCCESS)
{
CheckIfFileIsPresent(&Logger, hMpq, LISTFILE_NAME, true);
CheckIfFileIsPresent(&Logger, hMpq, ATTRIBUTES_NAME, true);
SFileCloseArchive(hMpq);
}
return nError;
}
static int TestForEachArchive(ARCHIVE_TEST pfnTest, char * szSearchMask, char * szPlainName)
{
char * szPathBuff = NULL;
int nError = ERROR_SUCCESS;
// If the name was not entered, use new one
if(szSearchMask == NULL)
{
szPathBuff = STORM_ALLOC(char, MAX_PATH);
if(szPathBuff != NULL)
{
CreateFullPathName(szPathBuff, szMpqSubDir, "*");
szSearchMask = szPathBuff;
szPlainName = strrchr(szSearchMask, '*');
}
}
// At this point, both pointers must be valid
assert(szSearchMask != NULL && szPlainName != NULL);
// Now both must be entered
if(szSearchMask != NULL && szPlainName != NULL)
{
#ifdef PLATFORM_WINDOWS
WIN32_FIND_DATAA wf;
HANDLE hFind;
// Initiate search. Use ANSI function only
hFind = FindFirstFileA(szSearchMask, &wf);
if(hFind != INVALID_HANDLE_VALUE)
{
// Skip the first entry, since it's always "." or ".."
while(FindNextFileA(hFind, &wf) && nError == ERROR_SUCCESS)
{
// Found a directory?
if(wf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(wf.cFileName[0] != '.')
{
sprintf(szPlainName, "%s\\*", wf.cFileName);
nError = TestForEachArchive(pfnTest, szSearchMask, strrchr(szSearchMask, '*'));
}
}
else
{
if(pfnTest != NULL)
{
strcpy(szPlainName, wf.cFileName);
nError = pfnTest(szSearchMask);
}
}
}
FindClose(hFind);
}
#endif
}
// Free the path buffer, if any
if(szPathBuff != NULL)
STORM_FREE(szPathBuff);
szPathBuff = NULL;
return nError;
}
//-----------------------------------------------------------------------------
// Main
int main(int argc, char * argv[])
{
int nError = ERROR_SUCCESS;
#if defined(_MSC_VER) && defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif // defined(_MSC_VER) && defined(_DEBUG)
// Initialize storage and mix the random number generator
printf("==== Test Suite for StormLib version %s ====\n", STORMLIB_VERSION_STRING);
nError = InitializeMpqDirectory(argv, argc);
// Search all testing archives and verify their SHA1 hash
if(nError == ERROR_SUCCESS)
nError = TestForEachArchive(TestVerifyFileChecksum, NULL, NULL);
// Test opening local file with SFileOpenFileEx
if(nError == ERROR_SUCCESS)
nError = TestOpenLocalFile("ListFile_Blizzard.txt");
// Test reading partial file
if(nError == ERROR_SUCCESS)
nError = TestPartFileRead("MPQ_2009_v2_WoW_patch.MPQ.part");
// Test working with an archive that has no listfile
if(nError == ERROR_SUCCESS)
nError = TestOpenFile_OpenById("MPQ_1997_v1_Diablo1_DIABDAT.MPQ");
// Open an empty archive (found in WoW cache - it's just a header)
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2012_v2_EmptyMpq.MPQ");
// Open an empty archive (created artificially - it's just a header)
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2013_v4_EmptyMpq.MPQ");
// Open an empty archive (found in WoW cache - it's just a header)
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2013_v4_patch-base-16357.MPQ");
// Open an empty archive (found in WoW cache - it's just a header)
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2011_v4_InvalidHetEntryCount.MPQ");
// Open an truncated archive
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2002_v1_BlockTableCut.MPQ");
// Open an Warcraft III map locked by a protector
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2002_v1_ProtectedMap_HashTable_FakeValid.w3x");
// Open an Warcraft III map locked by a protector
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2002_v1_ProtectedMap_InvalidUserData.w3x");
// Open an Warcraft III map locked by a protector
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2002_v1_ProtectedMap_InvalidMpqFormat.w3x");
// Open a MPQ that actually has user data
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2010_v2_HasUserData.s2ma");
// Open a MPQ archive v 3.0
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2010_v3_expansion-locale-frFR.MPQ");
// Open an encrypted archive from Starcraft II installer
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2011_v2_EncryptedMpq.MPQE");
// Open a MPK archive from Longwu online
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPx_2013_v1_LongwuOnline.mpk");
// Open a SQP archive from War of the Immortals
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPx_2013_v1_WarOfTheImmortals.sqp", "ListFile_WarOfTheImmortals.txt");
// Open a patched archive
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_Patched(PatchList_WoW_OldWorld13286, "OldWorld\\World\\Model.blob", 2);
// Open a patched archive
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_Patched(PatchList_WoW15050, "World\\Model.blob", 8);
// Open a patched archive. The file is in each patch as full, so there is 0 patches in the chain
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_Patched(PatchList_WoW16965, "DBFilesClient\\BattlePetNPCTeamMember.db2", 0);
// Check the opening archive for read-only
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_ReadOnly("MPQ_1997_v1_Diablo1_DIABDAT.MPQ", true);
// Check the opening archive for read-only
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_ReadOnly("MPQ_1997_v1_Diablo1_DIABDAT.MPQ", false);
// Check the SFileGetFileInfo function
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_GetFileInfo("MPQ_2002_v1_StrongSignature.w3m", "MPQ_2013_v4_SC2_EmptyMap.SC2Map");
// Check archive signature
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_VerifySignature("MPQ_1999_v1_WeakSignature.exe", "War2Patch_202.exe");
// Check archive signature
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_VerifySignature("MPQ_2002_v1_StrongSignature.w3m", "(10)DustwallowKeys.w3m");
// Compact the archive
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_CraftedUserData("MPQ_2010_v3_expansion-locale-frFR.MPQ", "StormLibTest_CraftedMpq1_v3.mpq");
// Open a MPQ (add custom user data to it
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_CraftedUserData("MPQ_2013_v4_SC2_EmptyMap.SC2Map", "StormLibTest_CraftedMpq2_v4.mpq");
// Open a MPQ (add custom user data to it)
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_CraftedUserData("MPQ_2013_v4_expansion1.MPQ", "StormLibTest_CraftedMpq3_v4.mpq");
// Test modifying file with no (listfile) and no (attributes)
if(nError == ERROR_SUCCESS)
nError = TestAddFile_ListFileTest("MPQ_1997_v1_Diablo1_DIABDAT.MPQ", false, false);
// Test modifying an archive that contains (listfile) and (attributes)
if(nError == ERROR_SUCCESS)
nError = TestAddFile_ListFileTest("MPQ_2013_v4_SC2_EmptyMap.SC2Map", true, true);
// Create an empty archive v2
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_EmptyMpq("StormLibTest_EmptyMpq_v2.mpq", MPQ_CREATE_ARCHIVE_V2);
// Create an empty archive v4
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_EmptyMpq("StormLibTest_EmptyMpq_v4.mpq", MPQ_CREATE_ARCHIVE_V4);
// Create an archive and fill it with files up to the max file count
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_FillArchive("StormLibTest_FileTableFull.mpq");
// Create an archive, and increment max file count several times
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_IncMaxFileCount("StormLibTest_IncMaxFileCount.mpq");
// Create a MPQ archive with UNICODE names
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_UnicodeNames();
// Create a MPQ file, add files with various flags
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_FileFlagTest("StormLibTest_FileFlagTest.mpq");
// Create a MPQ file, add files with various compressions
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_CompressionsTest("StormLibTest_CompressionTest.mpq");
// Check if the listfile is always created at the end of the file table in the archive
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_ListFilePos("StormLibTest_ListFilePos.mpq");
// Open a MPQ (add custom user data to it)
if(nError == ERROR_SUCCESS)
nError = TestCreateArchive_BigArchive("StormLibTest_BigArchive_v4.mpq");
return nError;
}
|