aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/CascRootFile_Mndx.cpp
blob: 0f7e3c42c64873438ce4087942ae981a1e62f137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
/*****************************************************************************/
/* CascMndxRoot.cpp                       Copyright (c) Ladislav Zezula 2014 */
/*---------------------------------------------------------------------------*/
/* Common functions for CascLib                                              */
/* Note: "HOTS" refers to Play.exe, v2.5.0.29049 (Heroes of the Storm Alpha) */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 18.05.14  1.00  Lad  The first version of CascMndxRoot.cpp                */
/*****************************************************************************/

#define __CASCLIB_SELF__
#include "CascLib.h"
#include "CascCommon.h"
#include "CascMndx.h"

//-----------------------------------------------------------------------------
// Local defines

#define CASC_MAR_SIGNATURE 0x0052414d               //  'MAR\0'

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

typedef struct _FILE_MNDX_HEADER
{
    DWORD Signature;                                // 'MNDX'
    DWORD HeaderVersion;                            // Must be <= 2
    DWORD FormatVersion;

} FILE_MNDX_HEADER, *PFILE_MNDX_HEADER;

typedef struct _FILE_MAR_INFO
{
    DWORD MarIndex;
    DWORD MarDataSize;
    DWORD MarDataSizeHi;
    DWORD MarDataOffset;
    DWORD MarDataOffsetHi;
} FILE_MAR_INFO, *PFILE_MAR_INFO;

typedef struct _CASC_MNDX_INFO
{
    BYTE  RootFileName[MD5_HASH_SIZE];              // Name (aka MD5) of the root file
    DWORD HeaderVersion;                            // Must be <= 2
    DWORD FormatVersion;
    DWORD field_1C;
    DWORD field_20;
    DWORD MarInfoOffset;                            // Offset of the first MAR entry info
    DWORD MarInfoCount;                             // Number of the MAR info entries
    DWORD MarInfoSize;                              // Size of the MAR info entry
    DWORD MndxEntriesOffset;
    DWORD MndxEntriesTotal;                         // Total number of MNDX root entries
    DWORD MndxEntriesValid;                         // Number of valid MNDX root entries
    DWORD MndxEntrySize;                            // Size of one MNDX root entry
    struct _MAR_FILE * pMarFile1;                   // File name list for the packages
    struct _MAR_FILE * pMarFile2;                   // File name list for names stripped of package names
    struct _MAR_FILE * pMarFile3;                   // File name list for complete names
//  PCASC_ROOT_ENTRY_MNDX pMndxEntries;
//  PCASC_ROOT_ENTRY_MNDX * ppValidEntries;
    bool bRootFileLoaded;                           // true if the root info file was properly loaded

} CASC_MNDX_INFO, *PCASC_MNDX_INFO;

typedef struct _CASC_MNDX_PACKAGE
{
    char * szFileName;                              // Pointer to file name
    size_t nLength;                                 // Length of the file name

} CASC_MNDX_PACKAGE, *PCASC_MNDX_PACKAGE;

typedef struct _CASC_MNDX_PACKAGES
{
    char * szNameBuffer;                            // Pointer to the buffer for file names
    size_t NameEntries;                             // Number of name entries in Names
    size_t NameBufferUsed;                          // Number of bytes used in the name buffer
    size_t NameBufferMax;                           // Total size of the name buffer

    CASC_MNDX_PACKAGE Packages[1];                  // List of packages

} CASC_MNDX_PACKAGES, *PCASC_MNDX_PACKAGES;

// Root file entry for CASC storages with MNDX root file (Heroes of the Storm)
// Corresponds to the in-file structure
typedef struct _CASC_ROOT_ENTRY_MNDX
{
    DWORD Flags;                                    // High 8 bits: Flags, low 24 bits: package index
    BYTE  EncodingKey[MD5_HASH_SIZE];               // Encoding key for the file
    DWORD FileSize;                                 // Uncompressed file size, in bytes

} CASC_ROOT_ENTRY_MNDX, *PCASC_ROOT_ENTRY_MNDX;

//-----------------------------------------------------------------------------
// Testing functions prototypes

#if defined(_DEBUG) && defined(_X86_) && defined(CASCLIB_TEST)
extern "C" bool _cdecl sub_1958B00_x86(TFileNameDatabase * pDB, TMndxFindResult * pStruct1C);
void TestMndxRootFile(PCASC_MNDX_INFO pMndxInfo);
#endif

//-----------------------------------------------------------------------------
// Local variables

unsigned char table_1BA1818[0x800] =
{
    0x07, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x07, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x05, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
    0x07, 0x07, 0x07, 0x01, 0x07, 0x02, 0x02, 0x01, 0x07, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x05, 0x05, 0x01, 0x05, 0x02, 0x02, 0x01, 0x05, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x05, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x06, 0x06, 0x01, 0x06, 0x02, 0x02, 0x01, 0x06, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x06, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x06, 0x05, 0x05, 0x01, 0x05, 0x02, 0x02, 0x01, 0x05, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x05, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x07, 0x07, 0x01, 0x07, 0x02, 0x02, 0x01, 0x07, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x05, 0x05, 0x01, 0x05, 0x02, 0x02, 0x01, 0x05, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x05, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x06, 0x06, 0x01, 0x06, 0x02, 0x02, 0x01, 0x06, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x06, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x06, 0x05, 0x05, 0x01, 0x05, 0x02, 0x02, 0x01, 0x05, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x05, 0x04, 0x04, 0x01, 0x04, 0x02, 0x02, 0x01, 0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x02, 0x01,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x02, 0x07, 0x07, 0x07, 0x03, 0x07, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x04, 0x07, 0x04, 0x04, 0x02, 0x07, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x02, 0x07, 0x05, 0x05, 0x03, 0x05, 0x03, 0x03, 0x02,
    0x07, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x02, 0x07, 0x06, 0x06, 0x03, 0x06, 0x03, 0x03, 0x02,
    0x07, 0x06, 0x06, 0x04, 0x06, 0x04, 0x04, 0x02, 0x06, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x02, 0x06, 0x05, 0x05, 0x03, 0x05, 0x03, 0x03, 0x02,
    0x06, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x02, 0x07, 0x07, 0x07, 0x03, 0x07, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x04, 0x07, 0x04, 0x04, 0x02, 0x07, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x02, 0x07, 0x05, 0x05, 0x03, 0x05, 0x03, 0x03, 0x02,
    0x07, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x02, 0x07, 0x06, 0x06, 0x03, 0x06, 0x03, 0x03, 0x02,
    0x07, 0x06, 0x06, 0x04, 0x06, 0x04, 0x04, 0x02, 0x06, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x02, 0x06, 0x05, 0x05, 0x03, 0x05, 0x03, 0x03, 0x02,
    0x06, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x02, 0x05, 0x04, 0x04, 0x03, 0x04, 0x03, 0x03, 0x02,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x04, 0x07, 0x07, 0x07, 0x04, 0x07, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x03,
    0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x04, 0x07, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x03,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x04, 0x07, 0x06, 0x06, 0x04, 0x06, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05, 0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x03,
    0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x04, 0x06, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x04, 0x07, 0x07, 0x07, 0x04, 0x07, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x03,
    0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x04, 0x07, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x03,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x04, 0x07, 0x06, 0x06, 0x04, 0x06, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05, 0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x03,
    0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x04, 0x06, 0x05, 0x05, 0x04, 0x05, 0x04, 0x04, 0x03,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05, 0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x07, 0x07, 0x07, 0x05, 0x07, 0x05, 0x05, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05,
    0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05, 0x07, 0x06, 0x06, 0x05, 0x06, 0x05, 0x05, 0x04,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x05,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x07, 0x07, 0x07, 0x06, 0x07, 0x06, 0x06, 0x05,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07
};

//-----------------------------------------------------------------------------
// Local functions - Number of set bits in an integer

// HOTS: inlined
DWORD GetNumberOfSetBits(DWORD Value32)
{
    Value32 = ((Value32 >> 1) & 0x55555555) + (Value32 & 0x55555555);
    Value32 = ((Value32 >> 2) & 0x33333333) + (Value32 & 0x33333333);
    Value32 = ((Value32 >> 4) & 0x0F0F0F0F) + (Value32 & 0x0F0F0F0F);

    return (Value32 * 0x01010101);
}

#define GetNumbrOfSetBits32(x)  (GetNumberOfSetBits(x) >> 0x18)

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

static bool RootFileRead(LPBYTE pbFilePointer, LPBYTE pbFileEnd, void * pvBuffer, size_t dwBytesToRead)
{
    // False if the file pointer is beyond the end
    if(pbFilePointer > pbFileEnd)
        return false;

    // False if there is not enough bytes available
    if((size_t)(pbFileEnd - pbFilePointer) < dwBytesToRead)
        return false;

    memcpy(pvBuffer, pbFilePointer, dwBytesToRead);
    return true;
}

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

// HOTS: 01956EE0
TMndxFindResult::TMndxFindResult()
{
    szSearchMask = NULL;
    cchSearchMask = 0;
    field_8 = 0;
    szFoundPath = NULL;
    cchFoundPath = 0;
    FileNameIndex = 0;
    pStruct40 = NULL;
}

// HOTS: 01956F00
TMndxFindResult::~TMndxFindResult()
{
    FreeStruct40();
}

// HOTS: 01956F30
int TMndxFindResult::CreateStruct40()
{
    if(pStruct40 != NULL)
        return ERROR_INVALID_PARAMETER;

    pStruct40 = new TStruct40();
    return (pStruct40 != NULL) ? ERROR_SUCCESS : ERROR_NOT_ENOUGH_MEMORY;
}

void TMndxFindResult::FreeStruct40()
{
    if(pStruct40 != NULL)
        delete pStruct40;
    pStruct40 = NULL;
}

// HOTS: 01956E70
int TMndxFindResult::SetSearchPath(
    const char * szNewSearchMask,
    size_t cchNewSearchMask)
{
    if(szSearchMask == NULL && cchSearchMask != 0)
        return ERROR_INVALID_PARAMETER;

    if(pStruct40 != NULL)
        pStruct40->SearchPhase = CASC_SEARCH_INITIALIZING;

    szSearchMask = szNewSearchMask;
    cchSearchMask = cchNewSearchMask;
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TByteStream functions

// HOTS: 01959990
TByteStream::TByteStream()
{
    pbByteData = NULL;
    pvMappedFile = NULL;
    cbByteData = 0;
    field_C  = 0;
    hFile    = 0;
    hMap     = 0;
}

// HOTS: 19599F0
void TByteStream::ExchangeWith(TByteStream & Target)
{
    TByteStream WorkBuff;

    WorkBuff = *this;
    *this    = Target;
    Target   = WorkBuff;
}

// HOTS: 19599F0
int TByteStream::GetBytes(DWORD cbByteCount, PARRAY_POINTER PtrArray)
{
    if(cbByteData < cbByteCount)
        return ERROR_BAD_FORMAT;

    // Give the buffer to the caller
    PtrArray->Bytes = pbByteData;

    // Move pointers
    pbByteData += cbByteCount;
    cbByteData -= cbByteCount;
    return ERROR_SUCCESS;
}

// HOTS: 1957190
int TByteStream::GetArray_DWORDs(PARRAY_POINTER PtrArray, DWORD ItemCount)
{
    if(PtrArray == NULL && ItemCount != 0)
        return ERROR_INVALID_PARAMETER;
    if(ItemCount > CASC_MAX_ENTRIES(DWORD))
        return ERROR_NOT_ENOUGH_MEMORY;

    return GetBytes(ItemCount * 4, PtrArray);
}

// HOTS: 19571E0
int TByteStream::GetArray_Triplets(PARRAY_POINTER PtrArray, DWORD ItemCount)
{
    if(PtrArray == NULL && ItemCount != 0)
        return ERROR_INVALID_PARAMETER;
    if(ItemCount > CASC_MAX_ENTRIES(TRIPLET))
        return ERROR_NOT_ENOUGH_MEMORY;

    return GetBytes(ItemCount * sizeof(TRIPLET), PtrArray);
}

// HOTS: 1957230
int TByteStream::GetArray_BYTES(PARRAY_POINTER PtrArray, DWORD ItemCount)
{
    if(PtrArray == NULL && ItemCount != 0)
        return ERROR_INVALID_PARAMETER;
    if(ItemCount > CASC_MAX_ENTRIES(BYTE))
        return ERROR_NOT_ENOUGH_MEMORY;

    return GetBytes(ItemCount, PtrArray);
}

// HOTS: 1957280
int TByteStream::GetArray_NameTable(PARRAY_POINTER PtrArray, DWORD ItemCount)
{
    if(PtrArray == NULL && ItemCount != 0)
        return ERROR_INVALID_PARAMETER;
    if(ItemCount > CASC_MAX_ENTRIES(NAME_FRAG))
        return ERROR_NOT_ENOUGH_MEMORY;

    return GetBytes(ItemCount * sizeof(NAME_FRAG), PtrArray);
}

// HOTS: 1959A60
int TByteStream::SkipBytes(DWORD cbByteCount)
{
    ARRAY_POINTER Dummy;

    return GetBytes(cbByteCount, &Dummy);
}

// HOTS: 1959AF0
int TByteStream::SetByteBuffer(LPBYTE pbNewByteData, DWORD cbNewByteData)
{
    if(pbNewByteData != NULL || cbNewByteData == 0)
    {
        pbByteData = pbNewByteData;
        cbByteData = cbNewByteData;
        return ERROR_SUCCESS;
    }

    return ERROR_INVALID_PARAMETER;
}


// HOTS: 1957160
int TByteStream::GetValue_DWORD(DWORD & Value)
{
    ARRAY_POINTER Pointer;
    int nError;

    nError = GetBytes(sizeof(DWORD), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;

    Value = Pointer.Uint32s[0];
    return ERROR_SUCCESS;
}

int TByteStream::GetValue_ItemCount(DWORD & NumberOfBytes, DWORD & ItemCount, DWORD ItemSize)
{
    ARRAY_POINTER Pointer;
    ULONGLONG ByteCount;
    int nError;

    // Verify if there is at least - 8 bytes
    nError = GetBytes(sizeof(ULONGLONG), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;

    // Extract the number of bytes
    ByteCount = Pointer.Int64Ptr[0];
    if(ByteCount > 0xFFFFFFFF || (ByteCount % ItemSize) != 0)
        return ERROR_BAD_FORMAT;

    // Give the result to the caller
    NumberOfBytes = (DWORD)ByteCount;
    ItemCount = (DWORD)(ByteCount / ItemSize);
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TGenericArray functions

TGenericArray::TGenericArray()
{
    DataBuffer.Bytes = NULL;
    FirstValid.Bytes = NULL;
    ArrayPointer.Bytes = NULL;
    ItemCount = 0;
    MaxItemCount = 0;
    bIsValidArray = false;
}

TGenericArray::~TGenericArray()
{
    if(DataBuffer.Bytes != NULL)
        CASC_FREE(DataBuffer.Bytes);
}

// HOTS: inlined
void TGenericArray::ExchangeWith(TGenericArray & Target)
{
    TGenericArray WorkBuff;

    WorkBuff = *this;
    *this    = Target;
    Target   = WorkBuff;
}

// HOTS: Inlined
void TGenericArray::CopyFrom(TGenericArray & Source)
{
    if(DataBuffer.Bytes != NULL)
        CASC_FREE(DataBuffer.Bytes);
    *this = Source;
}

// HOTS: 1957090 (SetDwordsValid)
// HOTS: 19570B0 (SetTripletsValid)
// HOTS: 19570D0 (? SetBitsValid ?)
// HOTS: 19570F0 (SetNameFragmentsValid)
int TGenericArray::SetArrayValid()
{
    if(bIsValidArray != 0)
        return 1;

    bIsValidArray = true;
    return ERROR_SUCCESS;
}


// HOTS: 19575A0
void TGenericArray::SetMaxItems_CHARS(DWORD NewMaxItemCount)
{
    ARRAY_POINTER OldDataBuffer = DataBuffer;
    ARRAY_POINTER NewDataBuffer;

    // Allocate new data buffer
    NewDataBuffer.Chars = CASC_ALLOC(char, NewMaxItemCount);
    if(NewDataBuffer.Chars != NULL)
    {
        // Copy the old items to the buffer
        for(DWORD i = 0; i < ItemCount; i++)
        {
            NewDataBuffer.Chars[i] = FirstValid.Chars[i];
        }
    }

    DataBuffer = NewDataBuffer;
    FirstValid = NewDataBuffer;
    ArrayPointer = NewDataBuffer;
    MaxItemCount = NewMaxItemCount;
    CASC_FREE(OldDataBuffer.Chars);
}

// HOTS: 1957600
void TGenericArray::SetMaxItems_PATH_STOP(DWORD NewMaxItemCount)
{
    ARRAY_POINTER OldDataBuffer = DataBuffer;
    ARRAY_POINTER NewDataBuffer;

    // Allocate new data buffer
    NewDataBuffer.PathStopPtr = CASC_ALLOC(PATH_STOP, NewMaxItemCount);
    if(NewDataBuffer.PathStopPtr != NULL)
    {
        // Copy the old items to the buffer
        for(DWORD i = 0; i < ItemCount; i++)
        {
            NewDataBuffer.PathStopPtr[i] = FirstValid.PathStopPtr[i];
        }
    }

    DataBuffer = NewDataBuffer;
    FirstValid = NewDataBuffer;
    ArrayPointer = NewDataBuffer;
    MaxItemCount = NewMaxItemCount;
    CASC_FREE(OldDataBuffer.PathStopPtr);
}

// HOTS: inline
void TGenericArray::InsertOneItem_CHAR(char NewItem)
{
    DWORD NewMaxItemCount;
    DWORD NewItemCount;

    NewItemCount = ItemCount + 1;
    if(NewItemCount > MaxItemCount)
    {
        NewMaxItemCount = NewItemCount;

        if(MaxItemCount > (NewItemCount / 2))
        {
            if(MaxItemCount <= (CASC_MAX_ENTRIES(BYTE) / 2))
                NewMaxItemCount = MaxItemCount + MaxItemCount;
            else
                NewMaxItemCount = CASC_MAX_ENTRIES(BYTE);
        }

        SetMaxItems_CHARS(NewMaxItemCount);
    }

    // Put the character to the slot that has been reserved
    FirstValid.Chars[ItemCount++] = NewItem;
}

// HOTS: 1958330, inline
void TGenericArray::InsertOneItem_PATH_STOP(PATH_STOP & NewItem)
{
    DWORD NewMaxItemCount;
    DWORD NewItemCount;

    NewItemCount = ItemCount + 1;
    if(NewItemCount > MaxItemCount)
    {
        NewMaxItemCount = NewItemCount;

        if(MaxItemCount > (NewItemCount / 2))
        {
            if(MaxItemCount <= (CASC_MAX_ENTRIES(PATH_STOP) / 2))
                NewMaxItemCount = MaxItemCount + MaxItemCount;
            else
                NewMaxItemCount = CASC_MAX_ENTRIES(PATH_STOP);
        }

        SetMaxItems_PATH_STOP(NewMaxItemCount);
    }

    // Put the structure to the slot that has been reserved
    FirstValid.PathStopPtr[ItemCount++] = NewItem;
}

// HOTS: 19583A0
void TGenericArray::sub_19583A0(DWORD NewItemCount)
{
    DWORD OldMaxItemCount = MaxItemCount;

    if(NewItemCount > MaxItemCount)
    {
        DWORD NewMaxItemCount = NewItemCount;

        if(MaxItemCount > (NewItemCount / 2))
        {
            if(MaxItemCount <= (CASC_MAX_ENTRIES(PATH_STOP) / 2))
                NewMaxItemCount = MaxItemCount + MaxItemCount;
            else
                NewMaxItemCount = CASC_MAX_ENTRIES(PATH_STOP);
        }

        SetMaxItems_PATH_STOP(NewMaxItemCount);
    }

    // Initialize the newly inserted items
    for(DWORD i = OldMaxItemCount; i < NewItemCount; i++)
    {
        FirstValid.PathStopPtr[i].ItemIndex = 0;
        FirstValid.PathStopPtr[i].field_4   = 0;
        FirstValid.PathStopPtr[i].field_8   = 0;
        FirstValid.PathStopPtr[i].field_C   = 0xFFFFFFFF;
        FirstValid.PathStopPtr[i].field_10  = 0xFFFFFFFF;
    }

    ItemCount = NewItemCount;
}

// HOTS: 1957440
int TGenericArray::LoadDwordsArray(TByteStream & InStream)
{
    DWORD NumberOfBytes;
    int nError;

    // Get and verify the number of items
    nError = InStream.GetValue_ItemCount(NumberOfBytes, ItemCount, sizeof(DWORD));
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetArray_DWORDs(&ArrayPointer, ItemCount);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.SkipBytes((0 - (DWORD)NumberOfBytes) & 0x07);
    if(nError != ERROR_SUCCESS)
        return nError;

    return SetArrayValid();
}

// HOTS: 19574E0
int TGenericArray::LoadTripletsArray(TByteStream & InStream)
{
    DWORD NumberOfBytes;
    int nError;

    // Get and verify the number of items
    nError = InStream.GetValue_ItemCount(NumberOfBytes, ItemCount, sizeof(TRIPLET));
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetArray_Triplets(&ArrayPointer, ItemCount);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.SkipBytes((0 - (DWORD)NumberOfBytes) & 0x07);
    if(nError != ERROR_SUCCESS)
        return nError;

    return SetArrayValid();
}

// HOTS: 1957690
int TGenericArray::LoadByteArray(TByteStream & InStream)
{
    DWORD NumberOfBytes;
    int nError;

    // Get and verify the number of items
    nError = InStream.GetValue_ItemCount(NumberOfBytes, ItemCount, sizeof(BYTE));
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetArray_BYTES(&ArrayPointer, ItemCount);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.SkipBytes((0 - (DWORD)NumberOfBytes) & 0x07);
    if(nError != ERROR_SUCCESS)
        return nError;

    return SetArrayValid();
}

// HOTS: 1957700
int TGenericArray::LoadFragmentInfos(TByteStream & InStream)
{
    DWORD NumberOfBytes;
    int nError;

    // Get and verify the number of items
    nError = InStream.GetValue_ItemCount(NumberOfBytes, ItemCount, sizeof(NAME_FRAG));
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetArray_NameTable(&ArrayPointer, ItemCount);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.SkipBytes((0 - (DWORD)NumberOfBytes) & 0x07);
    if(nError != ERROR_SUCCESS)
        return nError;

    return SetArrayValid();
}

// HOTS: 195A220
int TGenericArray::LoadStrings(TByteStream & InStream)
{
    DWORD NumberOfBytes;
    int nError;

    // Get and verify the number of items
    nError = InStream.GetValue_ItemCount(NumberOfBytes, ItemCount, sizeof(char));
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetArray_BYTES(&ArrayPointer, ItemCount);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.SkipBytes((0 - (DWORD)NumberOfBytes) & 0x07);
    if(nError != ERROR_SUCCESS)
        return nError;

    return SetArrayValid();
}

// HOTS: 19581C0
int TGenericArray::LoadDwordsArray_Copy(TByteStream & InStream)
{
    TGenericArray TempArray;
    int nError;

    nError = TempArray.LoadDwordsArray(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    CopyFrom(TempArray);
    return ERROR_SUCCESS;
}

// HOTS: 1958250
int TGenericArray::LoadTripletsArray_Copy(TByteStream & InStream)
{
    TGenericArray TempArray;
    int nError;

    nError = TempArray.LoadTripletsArray(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    CopyFrom(TempArray);
    return ERROR_SUCCESS;
}

// HOTS: 1958420
int TGenericArray::LoadBytes_Copy(TByteStream & InStream)
{
    TGenericArray TempArray;
    int nError;

    nError = TempArray.LoadByteArray(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    CopyFrom(TempArray);
    return 0;
}

// HOTS: 19584F0
int TGenericArray::LoadFragmentInfos_Copy(TByteStream & InStream)
{
    TGenericArray TempArray;
    int nError;

    nError = TempArray.LoadFragmentInfos(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    CopyFrom(TempArray);
    return ERROR_SUCCESS;
}

// HOTS: 195A360
int TGenericArray::LoadStringsWithCopy(TByteStream & InStream)
{
    TGenericArray TempArray;
    int nError;

    nError = TempArray.LoadStrings(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    CopyFrom(TempArray);
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TBitEntryArray functions

TBitEntryArray::TBitEntryArray()
{
    BitsPerEntry = 0;
    EntryBitMask = 0;
    TotalEntries = 0;
}

TBitEntryArray::~TBitEntryArray()
{}

// HOTS: 01957D20
void TBitEntryArray::ExchangeWith(TBitEntryArray & Target)
{
    TBitEntryArray WorkBuff;

    WorkBuff = *this;
    *this    = Target;
    Target   = WorkBuff;
}

// HOTS: 1958580
int TBitEntryArray::LoadFromStream(TByteStream & InStream)
{
    ARRAY_POINTER Pointer;
    ULONGLONG Value = 0;
    int nError;

    nError = LoadDwordsArray_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetBytes(sizeof(DWORD), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;

    BitsPerEntry = Pointer.Uint32s[0];
    if(BitsPerEntry > 0x20)
        return ERROR_BAD_FORMAT;

    nError = InStream.GetBytes(sizeof(DWORD), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;
    EntryBitMask = Pointer.Uint32s[0];

    nError = InStream.GetBytes(sizeof(ULONGLONG), &Pointer);
    if(nError == ERROR_SUCCESS)
        Value = Pointer.Int64Ptr[0];
    if(Value > 0xFFFFFFFF)
        return ERROR_BAD_FORMAT;
    TotalEntries = (DWORD)Value;

    assert((BitsPerEntry * TotalEntries) / 32 <= ItemCount);
    return ERROR_SUCCESS;
}

// HOTS: 1959300
int TBitEntryArray::LoadFromStream_Exchange(TByteStream & InStream)
{
    TBitEntryArray TempArray;
    int nError;

    nError = TempArray.LoadFromStream(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    ExchangeWith(TempArray);
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TStruct40 functions

TStruct40::TStruct40()
{
    ItemIndex   = 0;
    CharIndex   = 0;
    ItemCount   = 0;
    SearchPhase = CASC_SEARCH_INITIALIZING;
}

// HOTS: 19586B0
void TStruct40::InitSearchBuffers()
{
    DWORD NewMaxItemCount;

    array_00.ItemCount = 0;

    // HOTS: 19586BD
    if(array_00.MaxItemCount < 0x40)
    {
        // HOTS: 19586C2
        NewMaxItemCount = 0x40;

        if(array_00.MaxItemCount > 0x20)
        {
            if(array_00.MaxItemCount <= 0x7FFFFFFF)
                NewMaxItemCount = array_00.MaxItemCount + array_00.MaxItemCount;
            else
                NewMaxItemCount = CASC_MAX_ENTRIES(BYTE);
        }

        array_00.SetMaxItems_CHARS(NewMaxItemCount);
    }

    // HOTS: 19586E1
    // Set the new item count
    PathStops.sub_19583A0(0);

    if(PathStops.MaxItemCount < 4)
    {
        // HOTS: 19586F2
        NewMaxItemCount = 4;

        // HOTS: 19586EA
        if(PathStops.MaxItemCount > 2)
        {
            if(PathStops.MaxItemCount <= 0x6666666)
                NewMaxItemCount = PathStops.MaxItemCount + PathStops.MaxItemCount;
            else
                NewMaxItemCount = CASC_MAX_ENTRIES(PATH_STOP);
        }

        // HOTS: 195870B
        PathStops.SetMaxItems_PATH_STOP(NewMaxItemCount);
    }

    ItemIndex = 0;
    CharIndex = 0;
    ItemCount = 0;
    SearchPhase = CASC_SEARCH_SEARCHING;
}

//-----------------------------------------------------------------------------
// TSparseArray functions

TSparseArray::TSparseArray()
{
    TotalItemCount = 0;
    ValidItemCount = 0;
}

// HOTS: 1957DA0
void TSparseArray::ExchangeWith(TSparseArray & Target)
{
    TSparseArray WorkBuff;

    WorkBuff = *this;
    *this    = Target;
    Target   = WorkBuff;
}

// HOTS: 1958630
int TSparseArray::LoadFromStream(TByteStream & InStream)
{
    ARRAY_POINTER Pointer;
    int nError;

    nError = ItemBits.LoadDwordsArray_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetBytes(sizeof(DWORD), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;
    TotalItemCount = Pointer.Uint32s[0];

    nError = InStream.GetBytes(sizeof(DWORD), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;
    ValidItemCount = Pointer.Uint32s[0];

    if(ValidItemCount > TotalItemCount)
        return ERROR_FILE_CORRUPT;

    nError = BaseValues.LoadTripletsArray_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = ArrayDwords_38.LoadDwordsArray_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = ArrayDwords_50.LoadDwordsArray_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    return ERROR_SUCCESS;
}

// HOTS: 1959380
int TSparseArray::LoadFromStream_Exchange(TByteStream & InStream)
{
    TSparseArray NewStruct68;
    int nError;

    nError = NewStruct68.LoadFromStream(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    ExchangeWith(NewStruct68);
    return ERROR_SUCCESS;
}

// HOTS: 1959B60
DWORD TSparseArray::GetItemValue(DWORD ItemIndex)
{
    PTRIPLET pTriplet;
    DWORD DwordIndex;
    DWORD BaseValue;
    DWORD BitMask;

    //
    // Divide the low-8-bits index to four parts:
    //
    // |-----------------------|---|------------|
    // |       A (23 bits)     | B |      C     |
    // |-----------------------|---|------------|
    //
    // A (23-bits): Index to the table (60 bits per entry)
    //
    //    Layout of the table entry:
    //   |--------------------------------|-------|--------|--------|---------|---------|---------|---------|-----|
    //   |  Base Value                    | val[0]| val[1] | val[2] | val[3]  | val[4]  | val[5]  | val[6]  |  -  |
    //   |  32 bits                       | 7 bits| 8 bits | 8 bits | 9 bits  | 9 bits  | 9 bits  | 9 bits  |5bits|
    //   |--------------------------------|-------|--------|--------|---------|---------|---------|---------|-----|
    //
    // B (3 bits) : Index of the variable-bit value in the array (val[#], see above)
    //
    // C (32 bits): Number of bits to be checked (up to 0x3F bits).
    //              Number of set bits is then added to the values obtained from A and B

    // Upper 23 bits contain index to the table
    pTriplet = BaseValues.TripletArray + (ItemIndex >> 0x09);
    BaseValue = pTriplet->BaseValue;

    // Next 3 bits contain the index to the VBR
    switch(((ItemIndex >> 0x06) & 0x07) - 1)
    {
        case 0:     // Add the 1st value (7 bits)
            BaseValue += (pTriplet->Value2 & 0x7F);
            break;

        case 1:     // Add the 2nd value (8 bits)
            BaseValue += (pTriplet->Value2 >> 0x07) & 0xFF;
            break;

        case 2:     // Add the 3rd value (8 bits)
            BaseValue += (pTriplet->Value2 >> 0x0F) & 0xFF;
            break;

        case 3:     // Add the 4th value (9 bits)
            BaseValue += (pTriplet->Value2 >> 0x17);
            break;

        case 4:     // Add the 5th value (9 bits)
            BaseValue += (pTriplet->Value3 & 0x1FF);
            break;

        case 5:     // Add the 6th value (9 bits)
            BaseValue += (pTriplet->Value3 >> 0x09) & 0x1FF;
            break;

        case 6:     // Add the 7th value (9 bits)
            BaseValue += (pTriplet->Value3 >> 0x12) & 0x1FF;
            break;
    }

    //
    // Take the upper 27 bits as an index to DWORD array, take lower 5 bits
    // as number of bits to mask. Then calculate number of set bits in the value
    // masked value.
    //

    // Get the index into the array of DWORDs
    DwordIndex = (ItemIndex >> 0x05);

    // Add number of set bits in the masked value up to 0x3F bits
    if(ItemIndex & 0x20)
        BaseValue += GetNumbrOfSetBits32(ItemBits.Uint32Array[DwordIndex - 1]);

    BitMask = (1 << (ItemIndex & 0x1F)) - 1;
    return BaseValue + GetNumbrOfSetBits32(ItemBits.Uint32Array[DwordIndex] & BitMask);
}

//-----------------------------------------------------------------------------
// TNameIndexStruct functions

// HOTS: 0195A290
TNameIndexStruct::TNameIndexStruct()
{}

// HOTS: inlined
TNameIndexStruct::~TNameIndexStruct()
{}

// HOTS: 195A180
bool TNameIndexStruct::CheckNameFragment(TMndxFindResult * pStruct1C, DWORD dwFragOffs)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    const char * szPathFragment;
    const char * szSearchMask;

    if(!Struct68.TotalItemCount)
    {
        // Get the offset of the fragment to compare. For convenience with pStruct40->CharIndex,
        // subtract the CharIndex from the fragment offset
        szPathFragment = (NameFragments.CharArray + dwFragOffs - pStruct40->CharIndex);
        szSearchMask = pStruct1C->szSearchMask;

        // Keep searching as long as the name matches with the fragment
        while(szPathFragment[pStruct40->CharIndex] == szSearchMask[pStruct40->CharIndex])
        {
            // Move to the next character
            pStruct40->CharIndex++;

            // Is it the end of the fragment or end of the path?
            if(szPathFragment[pStruct40->CharIndex] == 0)
                return true;
            if(pStruct40->CharIndex >= pStruct1C->cchSearchMask)
                return false;
        }

        return false;
    }
    else
    {
        // Get the offset of the fragment to compare.
        szPathFragment = (const char *)(NameFragments.CharArray);
        szSearchMask = pStruct1C->szSearchMask;

        // Keep searching as long as the name matches with the fragment
        while(szPathFragment[dwFragOffs] == szSearchMask[pStruct40->CharIndex])
        {
            // Move to the next character
            pStruct40->CharIndex++;

            // Is it the end of the fragment or end of the path?
            if(Struct68.IsItemPresent(dwFragOffs++))
                return true;
            if(dwFragOffs >= pStruct1C->cchSearchMask)
                return false;
        }

        return false;
    }
}

// HOTS: 195A570
bool TNameIndexStruct::CheckAndCopyNameFragment(TMndxFindResult * pStruct1C, DWORD dwFragOffs)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    const char * szPathFragment;
    const char * szSearchMask;

    if(!Struct68.TotalItemCount)
    {
        // Get the offset of the fragment to compare. For convenience with pStruct40->CharIndex,
        // subtract the CharIndex from the fragment offset
        szPathFragment = (const char *)(NameFragments.CharArray + dwFragOffs - pStruct40->CharIndex);
        szSearchMask = pStruct1C->szSearchMask;

        // Keep copying as long as we don't reach the end of the search mask
        while(pStruct40->CharIndex < pStruct1C->cchSearchMask)
        {
            // HOTS: 195A5A0
            if(szPathFragment[pStruct40->CharIndex] != szSearchMask[pStruct40->CharIndex])
                return false;

            // HOTS: 195A5B7
            pStruct40->array_00.InsertOneItem_CHAR(szPathFragment[pStruct40->CharIndex]);
            pStruct40->CharIndex++;

            if(szPathFragment[pStruct40->CharIndex] == 0)
                return true;
        }

        // Fixup the address of the fragment
        szPathFragment += pStruct40->CharIndex;

        // HOTS: 195A660
        // Now we need to copy the rest of the fragment
        while(szPathFragment[0] != 0)
        {
            pStruct40->array_00.InsertOneItem_CHAR(szPathFragment[0]);
            szPathFragment++;
        }
    }
    else
    {
        // Get the offset of the fragment to compare
        // HOTS: 195A6B7
        szPathFragment = NameFragments.CharArray;
        szSearchMask = pStruct1C->szSearchMask;

        // Keep copying as long as we don't reach the end of the search mask
        while(dwFragOffs < pStruct1C->cchSearchMask)
        {
            if(szPathFragment[dwFragOffs] != szSearchMask[pStruct40->CharIndex])
                return false;

            pStruct40->array_00.InsertOneItem_CHAR(szPathFragment[dwFragOffs]);
            pStruct40->CharIndex++;

            // Keep going as long as the given bit is not set
            if(Struct68.IsItemPresent(dwFragOffs++))
                return true;
        }

        // Fixup the address of the fragment
        szPathFragment += dwFragOffs;

        // Now we need to copy the rest of the fragment
        while(Struct68.IsItemPresent(dwFragOffs++) == 0)
        {
            // HOTS: 195A7A6
            pStruct40->array_00.InsertOneItem_CHAR(szPathFragment[0]);
            szPathFragment++;
        }
    }

    return true;
}

// HOTS: 195A3F0
void TNameIndexStruct::CopyNameFragment(TMndxFindResult * pStruct1C, DWORD dwFragOffs)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    const char * szPathFragment;

    // HOTS: 195A3FA
    if(!Struct68.TotalItemCount)
    {
        // HOTS: 195A40C
        szPathFragment = NameFragments.CharArray + dwFragOffs;
        while(szPathFragment[0] != 0)
        {
            // Insert the character to the path being built
            pStruct40->array_00.InsertOneItem_CHAR(*szPathFragment++);
        }
    }
    else
    {
        // HOTS: 195A4B3
        for(;;)
        {
            // Insert the character to the path being built
            pStruct40->array_00.InsertOneItem_CHAR(NameFragments.CharArray[dwFragOffs]);

            // Keep going as long as the given bit is not set
            if(Struct68.IsItemPresent(dwFragOffs++))
                break;
        }
    }
}

// HOTS: 0195A300
void TNameIndexStruct::ExchangeWith(TNameIndexStruct & Target)
{
    TNameIndexStruct WorkBuff;

    WorkBuff = *this;
    *this    = Target;
    Target   = WorkBuff;
}

// HOTS: 0195A820
int TNameIndexStruct::LoadFromStream(TByteStream & InStream)
{
    int nError;

    nError = NameFragments.LoadStringsWithCopy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    return Struct68.LoadFromStream_Exchange(InStream);
}

// HOTS: 195A850
int TNameIndexStruct::LoadFromStream_Exchange(TByteStream & InStream)
{
    TNameIndexStruct TempIndexStruct;
    int nError;

    nError = TempIndexStruct.LoadFromStream(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    ExchangeWith(TempIndexStruct);
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TStruct10 functions

TStruct10::TStruct10()
{
    field_0 = 0x03;
    field_4 = 0x200;
    field_8 = 0x1000;
    field_C = 0x20000;
}

// HOTS: inline
void TStruct10::CopyFrom(TStruct10 & Target)
{
    field_0 = Target.field_0;
    field_4 = Target.field_4;
    field_8 = Target.field_8;
    field_C = Target.field_C;
}

// HOTS: 1956FD0
int TStruct10::sub_1956FD0(DWORD dwBitMask)
{
    switch(dwBitMask & 0xF80)
    {
        case 0x00:
            field_4 = 0x200;
            return ERROR_SUCCESS;

        case 0x80:
            field_4 = 0x80;
            return ERROR_SUCCESS;

        case 0x100:
            field_4 = 0x100;
            return ERROR_SUCCESS;

        case 0x200:
            field_4 = 0x200;
            return ERROR_SUCCESS;

        case 0x400:
            field_4 = 0x400;
            return ERROR_SUCCESS;

        case 0x800:
            field_4 = 0x800;
            return ERROR_SUCCESS;
    }

    return ERROR_INVALID_PARAMETER;
}

// HOTS: 1957050
int TStruct10::sub_1957050(DWORD dwBitMask)
{
    switch(dwBitMask & 0xF0000)
    {
        case 0x00:
            field_C = 0x20000;
            return ERROR_SUCCESS;

        case 0x10000:
            field_C = 0x10000;
            return ERROR_SUCCESS;

        case 0x20000:
            field_C = 0x20000;
            return ERROR_SUCCESS;
    }

    return ERROR_INVALID_PARAMETER;
}

// HOTS: 19572E0
int TStruct10::sub_19572E0(DWORD dwBitMask)
{
    DWORD dwSubMask;
    int nError;

    if(dwBitMask & 0xFFF00000)
        return ERROR_INVALID_PARAMETER;

    dwSubMask = dwBitMask & 0x7F;
    if(dwSubMask)
        field_0 = dwSubMask;

    nError = sub_1956FD0(dwBitMask);
    if(nError != ERROR_SUCCESS)
        return nError;

    dwSubMask = dwBitMask & 0xF000;
    if(dwSubMask == 0 || dwSubMask == 0x1000)
    {
        field_8 = 0x1000;
        return sub_1957050(dwBitMask);
    }

    if(dwSubMask == 0x2000)
    {
        field_8 = 0x2000;
        return sub_1957050(dwBitMask);
    }

    return ERROR_INVALID_PARAMETER;
}

// HOTS: 1957800
int TStruct10::sub_1957800(DWORD dwBitMask)
{
    TStruct10 TempStruct;
    int nError;

    nError = TempStruct.sub_19572E0(dwBitMask);
    if(nError != ERROR_SUCCESS)
        return nError;

    CopyFrom(TempStruct);
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TFileNameDatabase functions

// HOTS: 01958730
TFileNameDatabase::TFileNameDatabase()
{
    NameFragIndexMask = 0;
    field_214 = 0;
}

// HOTS: inlined
void TFileNameDatabase::ExchangeWith(TFileNameDatabase & Target)
{
    TFileNameDatabasePtr TempPtr;
    DWORD dwTemp;

    Struct68_00.ExchangeWith(Target.Struct68_00);
    FileNameIndexes.ExchangeWith(Target.FileNameIndexes);
    Struct68_D0.ExchangeWith(Target.Struct68_D0);

    FrgmDist_LoBits.ExchangeWith(Target.FrgmDist_LoBits);
    FrgmDist_HiBits.ExchangeWith(Target.FrgmDist_HiBits);

    IndexStruct_174.ExchangeWith(Target.IndexStruct_174);

    TempPtr = NextDB;
    NextDB = Target.NextDB;
    Target.NextDB = TempPtr;

    NameFragTable.ExchangeWith(Target.NameFragTable);

    dwTemp = NameFragIndexMask;
    NameFragIndexMask = Target.NameFragIndexMask;
    Target.NameFragIndexMask = dwTemp;

    dwTemp = field_214;
    field_214 = Target.field_214;
    Target.field_214 = dwTemp;

    Struct10.CopyFrom(Target.Struct10);
}

// HOTS: 1959CB0
DWORD TFileNameDatabase::sub_1959CB0(DWORD dwItemIndex)
{
    PTRIPLET pTriplet;
    DWORD dwKeyShifted = (dwItemIndex >> 9);
    DWORD eax, ebx, ecx, edx, esi, edi;

    // If lower 9 is zero
    edx = dwItemIndex;
    if((edx & 0x1FF) == 0)
        return Struct68_00.ArrayDwords_38.Uint32Array[dwKeyShifted];

    eax = Struct68_00.ArrayDwords_38.Uint32Array[dwKeyShifted] >> 9;
    esi = (Struct68_00.ArrayDwords_38.Uint32Array[dwKeyShifted + 1] + 0x1FF) >> 9;
    dwItemIndex = esi;

    if((eax + 0x0A) >= esi)
    {
        // HOTS: 1959CF7
        pTriplet = Struct68_00.BaseValues.TripletArray + eax + 1;
        edi = (eax << 0x09);
        ebx = edi - pTriplet->BaseValue + 0x200;
        while(edx >= ebx)
        {
            // HOTS: 1959D14
            edi += 0x200;
            pTriplet++;

            ebx = edi - pTriplet->BaseValue + 0x200;
            eax++;
        }
    }
    else
    {
        // HOTS: 1959D2E
        while((eax + 1) < esi)
        {
            // HOTS: 1959D38
            // ecx = Struct68_00.BaseValues.TripletArray;
            esi = (esi + eax) >> 1;
            ebx = (esi << 0x09) - Struct68_00.BaseValues.TripletArray[esi].BaseValue;
            if(edx < ebx)
            {
                // HOTS: 01959D4B
                dwItemIndex = esi;
            }
            else
            {
                // HOTS: 1959D50
                eax = esi;
                esi = dwItemIndex;
            }
        }
    }

    // HOTS: 1959D5F
    pTriplet = Struct68_00.BaseValues.TripletArray + eax;
    edx += pTriplet->BaseValue - (eax << 0x09);
    edi = (eax << 4);

    eax = pTriplet->Value2;
    ecx = (eax >> 0x17);
    ebx = 0x100 - ecx;
    if(edx < ebx)
    {
        // HOTS: 1959D8C
        ecx = ((eax >> 0x07) & 0xFF);
        esi = 0x80 - ecx;
        if(edx < esi)
        {
            // HOTS: 01959DA2
            eax = eax & 0x7F;
            ecx = 0x40 - eax;
            if(edx >= ecx)
            {
                // HOTS: 01959DB7
                edi += 2;
                edx = edx + eax - 0x40;
            }
        }
        else
        {
            // HOTS: 1959DC0
            eax = (eax >> 0x0F) & 0xFF;
            esi = 0xC0 - eax;
            if(edx < esi)
            {
                // HOTS: 1959DD3
                edi += 4;
                edx = edx + ecx - 0x80;
            }
            else
            {
                // HOTS: 1959DD3
                edi += 6;
                edx = edx + eax - 0xC0;
            }
        }
    }
    else
    {
        // HOTS: 1959DE8
        esi = pTriplet->Value3;
        eax = ((esi >> 0x09) & 0x1FF);
        ebx = 0x180 - eax;
        if(edx < ebx)
        {
            // HOTS: 01959E00
            esi = esi & 0x1FF;
            eax = (0x140 - esi);
            if(edx < eax)
            {
                // HOTS: 1959E11
                edi = edi + 8;
                edx = edx + ecx - 0x100;
            }
            else
            {
                // HOTS: 1959E1D
                edi = edi + 0x0A;
                edx = edx + esi - 0x140;
            }
        }
        else
        {
            // HOTS: 1959E29
            esi = (esi >> 0x12) & 0x1FF;
            ecx = (0x1C0 - esi);
            if(edx < ecx)
            {
                // HOTS: 1959E3D
                edi = edi + 0x0C;
                edx = edx + eax - 0x180;
            }
            else
            {
                // HOTS: 1959E49
                edi = edi + 0x0E;
                edx = edx + esi - 0x1C0;
            }
        }
    }

    // HOTS: 1959E53:
    // Calculate the number of bits set in the value of "ecx"
    ecx = ~Struct68_00.ItemBits.Uint32Array[edi];
    eax = GetNumberOfSetBits(ecx);
    esi = eax >> 0x18;

    if(edx >= esi)
    {
        // HOTS: 1959ea4
        ecx = ~Struct68_00.ItemBits.Uint32Array[++edi];
        edx = edx - esi;
        eax = GetNumberOfSetBits(ecx);
    }

    // HOTS: 1959eea
    // ESI gets the number of set bits in the lower 16 bits of ECX
    esi = (eax >> 0x08) & 0xFF;
    edi = edi << 0x05;
    if(edx < esi)
    {
        // HOTS: 1959EFC
        eax = eax & 0xFF;
        if(edx >= eax)
        {
            // HOTS: 1959F05
            ecx >>= 0x08;
            edi += 0x08;
            edx -= eax;
        }
    }
    else
    {
        // HOTS: 1959F0D
        eax = (eax >> 0x10) & 0xFF;
        if(edx < eax)
        {
            // HOTS: 1959F19
            ecx >>= 0x10;
            edi += 0x10;
            edx -= esi;
        }
        else
        {
            // HOTS: 1959F23
            ecx >>= 0x18;
            edi += 0x18;
            edx -= eax;
        }
    }

    // HOTS: 1959f2b
    edx = edx << 0x08;
    ecx = ecx & 0xFF;

    // BUGBUG: Possible buffer overflow here. Happens when dwItemIndex >= 0x9C.
    // The same happens in Heroes of the Storm (build 29049), so I am not sure
    // if this is a bug or a case that never happens
    assert((ecx + edx) < sizeof(table_1BA1818));
    return table_1BA1818[ecx + edx] + edi;
}

DWORD TFileNameDatabase::sub_1959F50(DWORD arg_0)
{
    PTRIPLET pTriplet;
    PDWORD ItemArray;
    DWORD eax, ebx, ecx, edx, esi, edi;

    edx = arg_0;
    eax = arg_0 >> 0x09;
    if((arg_0 & 0x1FF) == 0)
        return Struct68_00.ArrayDwords_50.Uint32Array[eax];

    ItemArray = Struct68_00.ArrayDwords_50.Uint32Array + eax;
    eax = (ItemArray[0] >> 0x09);
    edi = (ItemArray[1] + 0x1FF) >> 0x09;

    if((eax + 0x0A) > edi)
    {
        // HOTS: 01959F94
        pTriplet = Struct68_00.BaseValues.TripletArray + eax + 1;
        while(edx >= pTriplet->BaseValue)
        {
            // HOTS: 1959FA3
            pTriplet++;
            eax++;
        }
    }
    else
    {
        // Binary search
        // HOTS: 1959FAD
        if((eax + 1) < edi)
        {
            // HOTS: 1959FB4
            esi = (edi + eax) >> 1;
            if(edx < Struct68_00.BaseValues.TripletArray[esi].BaseValue)
            {
                // HOTS: 1959FC4
                edi = esi;
            }
            else
            {
                // HOTS: 1959FC8
                eax = esi;
            }
        }
    }

    // HOTS: 1959FD4
    pTriplet = Struct68_00.BaseValues.TripletArray + eax;
    edx = edx - pTriplet->BaseValue;
    edi = eax << 0x04;
    eax = pTriplet->Value2;
    ebx = (eax >> 0x17);
    if(edx < ebx)
    {
        // HOTS: 1959FF1
        esi = (eax >> 0x07) & 0xFF;
        if(edx < esi)
        {
            // HOTS: 0195A000
            eax = eax & 0x7F;
            if(edx >= eax)
            {
                // HOTS: 195A007
                edi = edi + 2;
                edx = edx - eax;
            }
        }
        else
        {
            // HOTS: 195A00E
            eax = (eax >> 0x0F) & 0xFF;
            if(edx < eax)
            {
                // HOTS: 195A01A
                edi += 4;
                edx = edx - esi;
            }
            else
            {
                // HOTS: 195A01F
                edi += 6;
                edx = edx - eax;
            }
        }
    }
    else
    {
        // HOTS: 195A026
        esi = pTriplet->Value3;
        eax = (pTriplet->Value3 >> 0x09) & 0x1FF;
        if(edx < eax)
        {
            // HOTS: 195A037
            esi = esi & 0x1FF;
            if(edx < esi)
            {
                // HOTS: 195A041
                edi = edi + 8;
                edx = edx - ebx;
            }
            else
            {
                // HOTS: 195A048
                edi = edi + 0x0A;
                edx = edx - esi;
            }
        }
        else
        {
            // HOTS: 195A04D
            esi = (esi >> 0x12) & 0x1FF;
            if(edx < esi)
            {
                // HOTS: 195A05A
                edi = edi + 0x0C;
                edx = edx - eax;
            }
            else
            {
                // HOTS: 195A061
                edi = edi + 0x0E;
                edx = edx - esi;
            }
        }
    }

    // HOTS: 195A066
    esi = Struct68_00.ItemBits.Uint32Array[edi];
    eax = GetNumberOfSetBits(esi);
    ecx = eax >> 0x18;

    if(edx >= ecx)
    {
        // HOTS: 195A0B2
        esi = Struct68_00.ItemBits.Uint32Array[++edi];
        edx = edx - ecx;
        eax = GetNumberOfSetBits(esi);
    }

    // HOTS: 195A0F6
    ecx = (eax >> 0x08) & 0xFF;

    edi = (edi << 0x05);
    if(edx < ecx)
    {
        // HOTS: 195A111
        eax = eax & 0xFF;
        if(edx >= eax)
        {
            // HOTS: 195A111
            edi = edi + 0x08;
            esi = esi >> 0x08;
            edx = edx - eax;
        }
    }
    else
    {
        // HOTS: 195A119
        eax = (eax >> 0x10) & 0xFF;
        if(edx < eax)
        {
            // HOTS: 195A125
            esi = esi >> 0x10;
            edi = edi + 0x10;
            edx = edx - ecx;
        }
        else
        {
            // HOTS: 195A12F
            esi = esi >> 0x18;
            edi = edi + 0x18;
            edx = edx - eax;
        }
    }

    esi = esi & 0xFF;
    edx = edx << 0x08;

    // BUGBUG: Potential buffer overflow
    // Happens in Heroes of the Storm when arg_0 == 0x5B
    assert((esi + edx) < sizeof(table_1BA1818));
    return table_1BA1818[esi + edx] + edi;
}

// HOTS: 1957970
bool TFileNameDatabase::CheckNextPathFragment(TMndxFindResult * pStruct1C)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    LPBYTE pbPathName = (LPBYTE)pStruct1C->szSearchMask;
    DWORD CollisionIndex;
    DWORD NameFragIndex;
    DWORD SaveCharIndex;
    DWORD HiBitsIndex;
    DWORD FragOffs;

    // Calculate index of the next name fragment in the name fragment table
    NameFragIndex = ((pStruct40->ItemIndex << 0x05) ^ pStruct40->ItemIndex ^ pbPathName[pStruct40->CharIndex]) & NameFragIndexMask;

    // Does the hash value match?
    if(NameFragTable.NameFragArray[NameFragIndex].ItemIndex == pStruct40->ItemIndex)
    {
        // Check if there is single character match
        if(IS_SINGLE_CHAR_MATCH(NameFragTable, NameFragIndex))
        {
            pStruct40->ItemIndex = NameFragTable.NameFragArray[NameFragIndex].NextIndex;
            pStruct40->CharIndex++;
            return true;
        }

        // Check if there is a name fragment match
        if(NextDB.pDB != NULL)
        {
            if(!NextDB.pDB->sub_1957B80(pStruct1C, NameFragTable.NameFragArray[NameFragIndex].FragOffs))
                return false;
        }
        else
        {
            if(!IndexStruct_174.CheckNameFragment(pStruct1C, NameFragTable.NameFragArray[NameFragIndex].FragOffs))
                return false;
        }

        pStruct40->ItemIndex = NameFragTable.NameFragArray[NameFragIndex].NextIndex;
        return true;
    }

    //
    // Conflict: Multiple hashes give the same table index
    //

    // HOTS: 1957A0E
    CollisionIndex = sub_1959CB0(pStruct40->ItemIndex) + 1;
    if(!Struct68_00.IsItemPresent(CollisionIndex))
        return false;

    pStruct40->ItemIndex = (CollisionIndex - pStruct40->ItemIndex - 1);
    HiBitsIndex = 0xFFFFFFFF;

//  CascDumpSparseArray("E:\\casc-array-68.txt", &FileNameIndexes);
//  CascDumpSparseArray("E:\\casc-array-D0.txt", &Struct68_D0);

    // HOTS: 1957A41:
    do
    {
        // HOTS: 1957A41
        // Check if the low 8 bits if the fragment offset contain a single character
        // or an offset to a name fragment
        if(Struct68_D0.IsItemPresent(pStruct40->ItemIndex))
        {
            if(HiBitsIndex == 0xFFFFFFFF)
            {
                // HOTS: 1957A6C
                HiBitsIndex = Struct68_D0.GetItemValue(pStruct40->ItemIndex);
            }
            else
            {
                // HOTS: 1957A7F
                HiBitsIndex++;
            }

            // HOTS: 1957A83
            SaveCharIndex = pStruct40->CharIndex;

            // Get the name fragment offset as combined value from lower 8 bits and upper bits
            FragOffs = GetNameFragmentOffsetEx(pStruct40->ItemIndex, HiBitsIndex);

            // Compare the string with the fragment name database
            if(NextDB.pDB != NULL)
            {
                // HOTS: 1957AEC
                if(NextDB.pDB->sub_1957B80(pStruct1C, FragOffs))
                    return true;
            }
            else
            {
                // HOTS: 1957AF7
                if(IndexStruct_174.CheckNameFragment(pStruct1C, FragOffs))
                    return true;
            }

            // HOTS: 1957B0E
            // If there was partial match with the fragment, end the search
            if(pStruct40->CharIndex != SaveCharIndex)
                return false;
        }
        else
        {
            // HOTS: 1957B1C
            if(FrgmDist_LoBits.ByteArray[pStruct40->ItemIndex] == pStruct1C->szSearchMask[pStruct40->CharIndex])
            {
                pStruct40->CharIndex++;
                return true;
            }
        }

        // HOTS: 1957B32
        pStruct40->ItemIndex++;
        CollisionIndex++;
    }
    while(Struct68_00.IsItemPresent(CollisionIndex));
    return false;
}

// HOTS: 1957B80
bool TFileNameDatabase::sub_1957B80(TMndxFindResult * pStruct1C, DWORD arg_4)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    PNAME_FRAG pNameEntry;
    DWORD FragOffs;
    DWORD eax, edi;

    edi = arg_4;

    // HOTS: 1957B95
    for(;;)
    {
        pNameEntry = NameFragTable.NameFragArray + (edi & NameFragIndexMask);
        if(edi == pNameEntry->NextIndex)
        {
            // HOTS: 01957BB4
            if((pNameEntry->FragOffs & 0xFFFFFF00) != 0xFFFFFF00)
            {
                // HOTS: 1957BC7
                if(NextDB.pDB != NULL)
                {
                    // HOTS: 1957BD3
                    if(!NextDB.pDB->sub_1957B80(pStruct1C, pNameEntry->FragOffs))
                        return false;
                }
                else
                {
                    // HOTS: 1957BE0
                    if(!IndexStruct_174.CheckNameFragment(pStruct1C, pNameEntry->FragOffs))
                        return false;
                }
            }
            else
            {
                // HOTS: 1957BEE
                if(pStruct1C->szSearchMask[pStruct40->CharIndex] != (char)pNameEntry->FragOffs)
                    return false;
                pStruct40->CharIndex++;
            }

            // HOTS: 1957C05
            edi = pNameEntry->ItemIndex;
            if(edi == 0)
                return true;

            if(pStruct40->CharIndex >= pStruct1C->cchSearchMask)
                return false;
        }
        else
        {
            // HOTS: 1957C30
            if(Struct68_D0.IsItemPresent(edi))
            {
                // HOTS: 1957C4C
                if(NextDB.pDB != NULL)
                {
                    // HOTS: 1957C58
                    FragOffs = GetNameFragmentOffset(edi);
                    if(!NextDB.pDB->sub_1957B80(pStruct1C, FragOffs))
                        return false;
                }
                else
                {
                    // HOTS: 1957350
                    FragOffs = GetNameFragmentOffset(edi);
                    if(!IndexStruct_174.CheckNameFragment(pStruct1C, FragOffs))
                        return false;
                }
            }
            else
            {
                // HOTS: 1957C8E
                if(FrgmDist_LoBits.ByteArray[edi] != pStruct1C->szSearchMask[pStruct40->CharIndex])
                    return false;

                pStruct40->CharIndex++;
            }

            // HOTS: 1957CB2
            if(edi <= field_214)
                return true;

            if(pStruct40->CharIndex >= pStruct1C->cchSearchMask)
                return false;

            eax = sub_1959F50(edi);
            edi = (eax - edi - 1);
        }
    }
}

// HOTS: 1958D70
void TFileNameDatabase::sub_1958D70(TMndxFindResult * pStruct1C, DWORD arg_4)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    PNAME_FRAG pNameEntry;

    // HOTS: 1958D84
    for(;;)
    {
        pNameEntry = NameFragTable.NameFragArray + (arg_4 & NameFragIndexMask);
        if(arg_4 == pNameEntry->NextIndex)
        {
            // HOTS: 1958DA6
            if((pNameEntry->FragOffs & 0xFFFFFF00) != 0xFFFFFF00)
            {
                // HOTS: 1958DBA
                if(NextDB.pDB != NULL)
                {
                    NextDB.pDB->sub_1958D70(pStruct1C, pNameEntry->FragOffs);
                }
                else
                {
                    IndexStruct_174.CopyNameFragment(pStruct1C, pNameEntry->FragOffs);
                }
            }
            else
            {
                // HOTS: 1958DE7
                // Insert the low 8 bits to the path being built
                pStruct40->array_00.InsertOneItem_CHAR((char)(pNameEntry->FragOffs & 0xFF));
            }

            // HOTS: 1958E71
            arg_4 = pNameEntry->ItemIndex;
            if(arg_4 == 0)
                return;
        }
        else
        {
            // HOTS: 1958E8E
            if(Struct68_D0.IsItemPresent(arg_4))
            {
                DWORD FragOffs;

                // HOTS: 1958EAF
                FragOffs = GetNameFragmentOffset(arg_4);
                if(NextDB.pDB != NULL)
                {
                    NextDB.pDB->sub_1958D70(pStruct1C, FragOffs);
                }
                else
                {
                    IndexStruct_174.CopyNameFragment(pStruct1C, FragOffs);
                }
            }
            else
            {
                // HOTS: 1958F50
                // Insert one character to the path being built
                pStruct40->array_00.InsertOneItem_CHAR(FrgmDist_LoBits.CharArray[arg_4]);
            }

            // HOTS: 1958FDE
            if(arg_4 <= field_214)
                return;

            arg_4 = 0xFFFFFFFF - arg_4 + sub_1959F50(arg_4);
        }
    }
}

// HOTS: 1959010
bool TFileNameDatabase::sub_1959010(TMndxFindResult * pStruct1C, DWORD arg_4)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    PNAME_FRAG pNameEntry;

    // HOTS: 1959024
    for(;;)
    {
        pNameEntry = NameFragTable.NameFragArray + (arg_4 & NameFragIndexMask);
        if(arg_4 == pNameEntry->NextIndex)
        {
            // HOTS: 1959047
            if((pNameEntry->FragOffs & 0xFFFFFF00) != 0xFFFFFF00)
            {
                // HOTS: 195905A
                if(NextDB.pDB != NULL)
                {
                    if(!NextDB.pDB->sub_1959010(pStruct1C, pNameEntry->FragOffs))
                        return false;
                }
                else
                {
                    if(!IndexStruct_174.CheckAndCopyNameFragment(pStruct1C, pNameEntry->FragOffs))
                        return false;
                }
            }
            else
            {
                // HOTS: 1959092
                if((char)(pNameEntry->FragOffs & 0xFF) != pStruct1C->szSearchMask[pStruct40->CharIndex])
                    return false;

                // Insert the low 8 bits to the path being built
                pStruct40->array_00.InsertOneItem_CHAR((char)(pNameEntry->FragOffs & 0xFF));
                pStruct40->CharIndex++;
            }

            // HOTS: 195912E
            arg_4 = pNameEntry->ItemIndex;
            if(arg_4 == 0)
                return true;
        }
        else
        {
            // HOTS: 1959147
            if(Struct68_D0.IsItemPresent(arg_4))
            {
                DWORD FragOffs;

                // HOTS: 195917C
                FragOffs = GetNameFragmentOffset(arg_4);
                if(NextDB.pDB != NULL)
                {
                    if(!NextDB.pDB->sub_1959010(pStruct1C, FragOffs))
                        return false;
                }
                else
                {
                    if(!IndexStruct_174.CheckAndCopyNameFragment(pStruct1C, FragOffs))
                        return false;
                }
            }
            else
            {
                // HOTS: 195920E
                if(FrgmDist_LoBits.CharArray[arg_4] != pStruct1C->szSearchMask[pStruct40->CharIndex])
                    return false;

                // Insert one character to the path being built
                pStruct40->array_00.InsertOneItem_CHAR(FrgmDist_LoBits.CharArray[arg_4]);
                pStruct40->CharIndex++;
            }

            // HOTS: 19592B6
            if(arg_4 <= field_214)
                return true;

            arg_4 = 0xFFFFFFFF - arg_4 + sub_1959F50(arg_4);
        }

        // HOTS: 19592D5
        if(pStruct40->CharIndex >= pStruct1C->cchSearchMask)
            break;
    }

    sub_1958D70(pStruct1C, arg_4);
    return true;
}

// HOTS: 1959460
bool TFileNameDatabase::sub_1959460(TMndxFindResult * pStruct1C)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    PPATH_STOP pPathStop;
    PATH_STOP PathStop;
    DWORD NewMaxItemCount;
    DWORD FragOffs;
    DWORD edi;

    if(pStruct40->SearchPhase == CASC_SEARCH_FINISHED)
        return false;

    if(pStruct40->SearchPhase != CASC_SEARCH_SEARCHING)
    {
        // HOTS: 1959489
        pStruct40->InitSearchBuffers();

        // If the caller passed a part of the search path, we need to find that one
        while(pStruct40->CharIndex < pStruct1C->cchSearchMask)
        {
            if(!sub_1958B00(pStruct1C))
            {
                pStruct40->SearchPhase = CASC_SEARCH_FINISHED;
                return false;
            }
        }

        // HOTS: 19594b0
        PathStop.ItemIndex = pStruct40->ItemIndex;
        PathStop.field_4   = 0;
        PathStop.field_8   = pStruct40->array_00.ItemCount;
        PathStop.field_C   = 0xFFFFFFFF;
        PathStop.field_10  = 0xFFFFFFFF;
        pStruct40->PathStops.InsertOneItem_PATH_STOP(PathStop);
        pStruct40->ItemCount = 1;

        if(FileNameIndexes.IsItemPresent(pStruct40->ItemIndex))
        {
            pStruct1C->szFoundPath   = pStruct40->array_00.FirstValid.Chars;
            pStruct1C->cchFoundPath  = pStruct40->array_00.ItemCount;
            pStruct1C->FileNameIndex = FileNameIndexes.GetItemValue(pStruct40->ItemIndex);
            return true;
        }
    }

    // HOTS: 1959522
    for(;;)
    {
        // HOTS: 1959530
        if(pStruct40->ItemCount == pStruct40->PathStops.ItemCount)
        {
            PPATH_STOP pLastStop;
            DWORD CollisionIndex;

            pLastStop = pStruct40->PathStops.FirstValid.PathStopPtr + pStruct40->PathStops.ItemCount - 1;
            CollisionIndex = sub_1959CB0(pLastStop->ItemIndex) + 1;

            // Insert a new structure
            PathStop.ItemIndex = CollisionIndex - pLastStop->ItemIndex - 1;;
            PathStop.field_4   = CollisionIndex;
            PathStop.field_8   = 0;
            PathStop.field_C   = 0xFFFFFFFF;
            PathStop.field_10  = 0xFFFFFFFF;
            pStruct40->PathStops.InsertOneItem_PATH_STOP(PathStop);
        }

        // HOTS: 19595BD
        pPathStop = pStruct40->PathStops.FirstValid.PathStopPtr + pStruct40->ItemCount;

        // HOTS: 19595CC
        if(Struct68_00.IsItemPresent(pPathStop->field_4++))
        {
            // HOTS: 19595F2
            pStruct40->ItemCount++;

            if(Struct68_D0.IsItemPresent(pPathStop->ItemIndex))
            {
                // HOTS: 1959617
                if(pPathStop->field_C == 0xFFFFFFFF)
                    pPathStop->field_C = Struct68_D0.GetItemValue(pPathStop->ItemIndex);
                else
                    pPathStop->field_C++;

                // HOTS: 1959630
                FragOffs = GetNameFragmentOffsetEx(pPathStop->ItemIndex, pPathStop->field_C);
                if(NextDB.pDB != NULL)
                {
                    // HOTS: 1959649
                    NextDB.pDB->sub_1958D70(pStruct1C, FragOffs);
                }
                else
                {
                    // HOTS: 1959654
                    IndexStruct_174.CopyNameFragment(pStruct1C, FragOffs);
                }
            }
            else
            {
                // HOTS: 1959665
                // Insert one character to the path being built
                pStruct40->array_00.InsertOneItem_CHAR(FrgmDist_LoBits.CharArray[pPathStop->ItemIndex]);
            }

            // HOTS: 19596AE
            pPathStop->field_8 = pStruct40->array_00.ItemCount;

            // HOTS: 19596b6
            if(FileNameIndexes.IsItemPresent(pPathStop->ItemIndex))
            {
                // HOTS: 19596D1
                if(pPathStop->field_10 == 0xFFFFFFFF)
                {
                    // HOTS: 19596D9
                    pPathStop->field_10 = FileNameIndexes.GetItemValue(pPathStop->ItemIndex);
                }
                else
                {
                    pPathStop->field_10++;
                }

                // HOTS: 1959755
                pStruct1C->szFoundPath = pStruct40->array_00.FirstValid.Chars;
                pStruct1C->cchFoundPath = pStruct40->array_00.ItemCount;
                pStruct1C->FileNameIndex = pPathStop->field_10;
                return true;
            }
        }
        else
        {
            // HOTS: 19596E9
            if(pStruct40->ItemCount == 1)
            {
                pStruct40->SearchPhase = CASC_SEARCH_FINISHED;
                return false;
            }

            // HOTS: 19596F5
            pPathStop = pStruct40->PathStops.FirstValid.PathStopPtr + pStruct40->ItemCount - 1;
            pPathStop->ItemIndex++;

            pPathStop = pStruct40->PathStops.FirstValid.PathStopPtr + pStruct40->ItemCount - 2;
            edi = pPathStop->field_8;

            if(edi > pStruct40->array_00.MaxItemCount)
            {
                // HOTS: 1959717
                NewMaxItemCount = edi;

                if(pStruct40->array_00.MaxItemCount > (edi / 2))
                {
                    if(pStruct40->array_00.MaxItemCount > 0x7FFFFFFF)
                    {
                        NewMaxItemCount = 0xFFFFFFFF;
                    }
                    else
                    {
                        NewMaxItemCount = pStruct40->array_00.MaxItemCount + pStruct40->array_00.MaxItemCount;
                    }
                }

                pStruct40->array_00.SetMaxItems_CHARS(NewMaxItemCount);
            }

            // HOTS: 1959749
            pStruct40->array_00.ItemCount = edi;
            pStruct40->ItemCount--;
        }
    }
}

// HOTS: 1958B00
bool TFileNameDatabase::sub_1958B00(TMndxFindResult * pStruct1C)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;
    LPBYTE pbPathName = (LPBYTE)pStruct1C->szSearchMask;
    DWORD CollisionIndex;
    DWORD FragmentOffset;
    DWORD SaveCharIndex;
    DWORD ItemIndex;
    DWORD FragOffs;
    DWORD var_4;

    ItemIndex = pbPathName[pStruct40->CharIndex] ^ (pStruct40->ItemIndex << 0x05) ^ pStruct40->ItemIndex;
    ItemIndex = ItemIndex & NameFragIndexMask;
    if(pStruct40->ItemIndex == NameFragTable.NameFragArray[ItemIndex].ItemIndex)
    {
        // HOTS: 1958B45
        FragmentOffset = NameFragTable.NameFragArray[ItemIndex].FragOffs;
        if((FragmentOffset & 0xFFFFFF00) == 0xFFFFFF00)
        {
            // HOTS: 1958B88
            pStruct40->array_00.InsertOneItem_CHAR((char)FragmentOffset);
            pStruct40->ItemIndex = NameFragTable.NameFragArray[ItemIndex].NextIndex;
            pStruct40->CharIndex++;
            return true;
        }

        // HOTS: 1958B59
        if(NextDB.pDB != NULL)
        {
            if(!NextDB.pDB->sub_1959010(pStruct1C, FragmentOffset))
                return false;
        }
        else
        {
            if(!IndexStruct_174.CheckAndCopyNameFragment(pStruct1C, FragmentOffset))
                return false;
        }

        // HOTS: 1958BCA
        pStruct40->ItemIndex = NameFragTable.NameFragArray[ItemIndex].NextIndex;
        return true;
    }

    // HOTS: 1958BE5
    CollisionIndex = sub_1959CB0(pStruct40->ItemIndex) + 1;
    if(!Struct68_00.IsItemPresent(CollisionIndex))
        return false;

    pStruct40->ItemIndex = (CollisionIndex - pStruct40->ItemIndex - 1);
    var_4 = 0xFFFFFFFF;

    // HOTS: 1958C20
    for(;;)
    {
        if(Struct68_D0.IsItemPresent(pStruct40->ItemIndex))
        {
            // HOTS: 1958C0E
            if(var_4 == 0xFFFFFFFF)
            {
                // HOTS: 1958C4B
                var_4 = Struct68_D0.GetItemValue(pStruct40->ItemIndex);
            }
            else
            {
                var_4++;
            }

            // HOTS: 1958C62
            SaveCharIndex = pStruct40->CharIndex;

            FragOffs = GetNameFragmentOffsetEx(pStruct40->ItemIndex, var_4);
            if(NextDB.pDB != NULL)
            {
                // HOTS: 1958CCB
                if(NextDB.pDB->sub_1959010(pStruct1C, FragOffs))
                    return true;
            }
            else
            {
                // HOTS: 1958CD6
                if(IndexStruct_174.CheckAndCopyNameFragment(pStruct1C, FragOffs))
                    return true;
            }

            // HOTS: 1958CED
            if(SaveCharIndex != pStruct40->CharIndex)
                return false;
        }
        else
        {
            // HOTS: 1958CFB
            if(FrgmDist_LoBits.ByteArray[pStruct40->ItemIndex] == pStruct1C->szSearchMask[pStruct40->CharIndex])
            {
                // HOTS: 1958D11
                pStruct40->array_00.InsertOneItem_CHAR(FrgmDist_LoBits.ByteArray[pStruct40->ItemIndex]);
                pStruct40->CharIndex++;
                return true;
            }
        }

        // HOTS: 1958D11
        pStruct40->ItemIndex++;
        CollisionIndex++;

        if(!Struct68_00.IsItemPresent(CollisionIndex))
            break;
    }

    return false;
}

// HOTS: 1957EF0
bool TFileNameDatabase::FindFileInDatabase(TMndxFindResult * pStruct1C)
{
    TStruct40 * pStruct40 = pStruct1C->pStruct40;

    pStruct40->ItemIndex = 0;
    pStruct40->CharIndex = 0;
    pStruct40->SearchPhase = CASC_SEARCH_INITIALIZING;

    if(pStruct1C->cchSearchMask > 0)
    {
        while(pStruct40->CharIndex < pStruct1C->cchSearchMask)
        {
            // HOTS: 01957F12
            if(!CheckNextPathFragment(pStruct1C))
                return false;
        }
    }

    // HOTS: 1957F26
    if(!FileNameIndexes.IsItemPresent(pStruct40->ItemIndex))
        return false;

    pStruct1C->szFoundPath   = pStruct1C->szSearchMask;
    pStruct1C->cchFoundPath  = pStruct1C->cchSearchMask;
    pStruct1C->FileNameIndex = FileNameIndexes.GetItemValue(pStruct40->ItemIndex);
    return true;
}

// HOTS: 1959790
int TFileNameDatabase::LoadFromStream(TByteStream & InStream)
{
    DWORD dwBitMask;
    int nError;

    nError = Struct68_00.LoadFromStream_Exchange(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = FileNameIndexes.LoadFromStream_Exchange(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = Struct68_D0.LoadFromStream_Exchange(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    // HOTS: 019597CD
    nError = FrgmDist_LoBits.LoadBytes_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = FrgmDist_HiBits.LoadFromStream_Exchange(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    // HOTS: 019597F5
    nError = IndexStruct_174.LoadFromStream_Exchange(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    // HOTS: 0195980A
    if(Struct68_D0.ValidItemCount != 0 && IndexStruct_174.NameFragments.ItemCount == 0)
    {
        TFileNameDatabase * pNextDB = new TFileNameDatabase;

        nError = NextDB.SetDatabase(pNextDB);
        if(nError != ERROR_SUCCESS)
            return nError;

        if(NextDB.pDB == NULL)
            return ERROR_NOT_ENOUGH_MEMORY;

        nError = NextDB.pDB->LoadFromStream(InStream);
        if(nError != ERROR_SUCCESS)
            return nError;
    }

    // HOTS: 0195986B
    nError = NameFragTable.LoadFragmentInfos_Copy(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    NameFragIndexMask = NameFragTable.ItemCount - 1;

    nError = InStream.GetValue_DWORD(field_214);
    if(nError != ERROR_SUCCESS)
        return nError;

    nError = InStream.GetValue_DWORD(dwBitMask);
    if(nError != ERROR_SUCCESS)
        return nError;

    return Struct10.sub_1957800(dwBitMask);
}

// HOTS: 19598D0
int TFileNameDatabase::LoadFromStream_Exchange(TByteStream & InStream)
{
    TFileNameDatabase TempDatabase;
    ARRAY_POINTER Pointer;
    DWORD dwSignature;
    int nError;

    // Get pointer to MAR signature
    nError = InStream.GetBytes(sizeof(DWORD), &Pointer);
    if(nError != ERROR_SUCCESS)
        return nError;

    // Verify the signature
    dwSignature = Pointer.Uint32s[0];
    if(dwSignature != CASC_MAR_SIGNATURE)
        return ERROR_BAD_FORMAT;

    nError = TempDatabase.LoadFromStream(InStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    MarStream.ExchangeWith(InStream);
    ExchangeWith(TempDatabase);
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// TFileNameDatabasePtr functions

// HOTS: 01956D70
TFileNameDatabasePtr::TFileNameDatabasePtr()
{
    pDB = NULL;
}

TFileNameDatabasePtr::~TFileNameDatabasePtr()
{
    delete pDB;
}

// HOTS: 1956C60
int TFileNameDatabasePtr::FindFileInDatabase(TMndxFindResult * pStruct1C)
{
    int nError = ERROR_SUCCESS;

    if(pDB == NULL)
        return ERROR_INVALID_PARAMETER;

    nError = pStruct1C->CreateStruct40();
    if(nError != ERROR_SUCCESS)
        return nError;

    if(!pDB->FindFileInDatabase(pStruct1C))
        nError = ERROR_FILE_NOT_FOUND;

    pStruct1C->FreeStruct40();
    return nError;
}

// HOTS: 1956CE0
int TFileNameDatabasePtr::sub_1956CE0(TMndxFindResult * pStruct1C, bool * pbFindResult)
{
    int nError = ERROR_SUCCESS;

    if(pDB == NULL)
        return ERROR_INVALID_PARAMETER;

    // Create the pStruct40, if not initialized yet
    if(pStruct1C->pStruct40 == NULL)
    {
        nError = pStruct1C->CreateStruct40();
        if(nError != ERROR_SUCCESS)
            return nError;
    }

    *pbFindResult = pDB->sub_1959460(pStruct1C);
    return nError;
}

// HOTS: 1956D20
int TFileNameDatabasePtr::GetFileNameCount(PDWORD PtrFileNameCount)
{
    if(pDB == NULL)
        return ERROR_INVALID_PARAMETER;

    PtrFileNameCount[0] = pDB->FileNameIndexes.ValidItemCount;
    return ERROR_SUCCESS;
}

// HOTS: 1956DA0
int TFileNameDatabasePtr::CreateDatabase(LPBYTE pbMarData, DWORD cbMarData)
{
    TFileNameDatabase * pDatabase;
    TByteStream ByteStream;
    int nError;

    if(pbMarData == NULL && cbMarData != 0)
        return ERROR_INVALID_PARAMETER;

    pDatabase = new TFileNameDatabase;
    if(pDatabase == NULL)
        return ERROR_NOT_ENOUGH_MEMORY;

    nError = ByteStream.SetByteBuffer(pbMarData, cbMarData);
    if(nError != ERROR_SUCCESS)
        return nError;

    // HOTS: 1956E11
    nError = pDatabase->LoadFromStream_Exchange(ByteStream);
    if(nError != ERROR_SUCCESS)
        return nError;

    pDB = pDatabase;
    return ERROR_SUCCESS;
}

// HOTS: 19584B0
int TFileNameDatabasePtr::SetDatabase(TFileNameDatabase * pNewDB)
{
    if(pNewDB != NULL && pDB == pNewDB)
        return ERROR_INVALID_PARAMETER;

    if(pDB != NULL)
        delete pDB;
    pDB = pNewDB;
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// Local functions - MAR file

// HOTS: 00E94180
static void MAR_FILE_CreateDatabase(PMAR_FILE pMarFile)
{
    pMarFile->pDatabasePtr = new TFileNameDatabasePtr;
    if(pMarFile->pDatabasePtr != NULL)
        pMarFile->pDatabasePtr->CreateDatabase(pMarFile->pbMarData, pMarFile->cbMarData);
}

static int MAR_FILE_SearchFile(PMAR_FILE pMarFile, TMndxFindResult * pStruct1C)
{
    return pMarFile->pDatabasePtr->FindFileInDatabase(pStruct1C);
}

static void MAR_FILE_Destructor(PMAR_FILE pMarFile)
{
    if(pMarFile != NULL)
    {
        if(pMarFile->pDatabasePtr != NULL)
            delete pMarFile->pDatabasePtr;
        if(pMarFile->pbMarData != NULL)
            CASC_FREE(pMarFile->pbMarData);

        CASC_FREE(pMarFile);
    }
}

//-----------------------------------------------------------------------------
// Package functions

// TODO: When working, increment these values to lower number of (re)allocations
#define CASC_PACKAGES_INIT  0x10
#define CASC_PACKAGES_DELTA 0x10

static PCASC_MNDX_PACKAGES AllocatePackages(size_t nNameEntries, size_t nNameBufferMax)
{
    PCASC_MNDX_PACKAGES pPackages;
    size_t cbToAllocate;

    // Allocate space
    cbToAllocate = sizeof(CASC_MNDX_PACKAGES) + (nNameEntries * sizeof(CASC_MNDX_PACKAGE)) + nNameBufferMax;
    pPackages = (PCASC_MNDX_PACKAGES)CASC_ALLOC(BYTE, cbToAllocate);
    if(pPackages != NULL)
    {
        // Fill the structure
        memset(pPackages, 0, cbToAllocate);

        // Init the list entries
        pPackages->szNameBuffer = (char *)(&pPackages->Packages[nNameEntries]);
        pPackages->NameEntries = nNameEntries;
        pPackages->NameBufferUsed = 0;
        pPackages->NameBufferMax = nNameBufferMax;
    }

    return pPackages;
}

static PCASC_MNDX_PACKAGES InsertToPackageList(
    PCASC_MNDX_PACKAGES pPackages,
    const char * szFileName,
    size_t cchFileName,
    size_t nPackageIndex)
{
    size_t nNewNameEntries = pPackages->NameEntries;
    size_t nNewNameBufferMax = pPackages->NameBufferMax;
    size_t cbToAllocate;
    char * szNameBuffer;

    // Need to reallocate?
    while(nPackageIndex >= nNewNameEntries)
        nNewNameEntries = nNewNameEntries + CASC_PACKAGES_DELTA;
    if((pPackages->NameBufferUsed + cchFileName + 1) > nNewNameBufferMax)
        nNewNameBufferMax = nNewNameBufferMax + 0x1000;

    // If any of the two variables overflowed, we need to reallocate the name list
    if(nNewNameEntries > pPackages->NameEntries || nNewNameBufferMax > pPackages->NameBufferMax)
    {
        PCASC_MNDX_PACKAGES pOldPackages = pPackages;

        // Allocate new name list
        cbToAllocate = sizeof(CASC_MNDX_PACKAGES) + (nNewNameEntries * sizeof(CASC_MNDX_PACKAGE)) + nNewNameBufferMax;
        pPackages = (PCASC_MNDX_PACKAGES)CASC_ALLOC(BYTE, cbToAllocate);
        if(pPackages == NULL)
            return NULL;

        // Copy the old entries
        memset(pPackages, 0, cbToAllocate);
        pPackages->szNameBuffer = szNameBuffer = (char *)(&pPackages->Packages[nNewNameEntries]);
        memcpy(pPackages->szNameBuffer, pOldPackages->szNameBuffer, pOldPackages->NameBufferUsed);

        // Copy the old entries
        for(size_t i = 0; i < pOldPackages->NameEntries; i++)
        {
            if(pOldPackages->Packages[i].szFileName != NULL)
            {
                pPackages->Packages[i].szFileName = pPackages->szNameBuffer + (pOldPackages->Packages[i].szFileName - pOldPackages->szNameBuffer);
                pPackages->Packages[i].nLength = pOldPackages->Packages[i].nLength;
            }
        }

        // Fill the limits
        pPackages->NameEntries = nNewNameEntries;
        pPackages->NameBufferUsed = pOldPackages->NameBufferUsed;
        pPackages->NameBufferMax = nNewNameBufferMax;

        // Switch the name lists
        CASC_FREE(pOldPackages);
    }

    // The slot is expected to be empty at the moment
    assert(pPackages->Packages[nPackageIndex].szFileName == NULL);
    assert(pPackages->Packages[nPackageIndex].nLength == 0);

    // Set the file name entry
    szNameBuffer = pPackages->szNameBuffer + pPackages->NameBufferUsed;
    pPackages->Packages[nPackageIndex].szFileName = szNameBuffer;
    pPackages->Packages[nPackageIndex].nLength = cchFileName;
    memcpy(szNameBuffer, szFileName, cchFileName);
    pPackages->NameBufferUsed += (cchFileName + 1);
    return pPackages;
}

static int LoadPackageNames(PCASC_MNDX_INFO pMndxInfo, PCASC_MNDX_PACKAGES * ppPackages)
{
    TMndxFindResult Struct1C;
    PCASC_MNDX_PACKAGES pPackages = NULL;
    PMAR_FILE pMarFile;

    // Sanity checks
    assert(pMndxInfo != NULL);

    // Prepare the file name search in the top level directory
    pMarFile = pMndxInfo->pMarFile1;
    Struct1C.SetSearchPath("", 0);

    // Allocate initial name list structure
    pPackages = AllocatePackages(CASC_PACKAGES_INIT, 0x1000);
    if(pPackages == NULL)
        return ERROR_NOT_ENOUGH_MEMORY;

    // Keep searching as long as we find something
    for(;;)
    {
        bool bFindResult = false;

        // Search the next file name
        pMarFile->pDatabasePtr->sub_1956CE0(&Struct1C, &bFindResult);
        if(bFindResult == false)
            break;

        // Insert the found name to the top level directory list
        pPackages = InsertToPackageList(pPackages, Struct1C.szFoundPath, Struct1C.cchFoundPath, Struct1C.FileNameIndex);
        if(pPackages == NULL)
            return ERROR_NOT_ENOUGH_MEMORY;
    }

    // Give the packages to the caller
    if(ppPackages != NULL)
        ppPackages[0] = pPackages;
    return ERROR_SUCCESS;
}

//-----------------------------------------------------------------------------
// Implementation of root file functions

struct TRootHandler_MNDX : public TRootHandler
{
    CASC_MNDX_INFO MndxInfo;

    PCASC_ROOT_ENTRY_MNDX * ppValidEntries;
    PCASC_ROOT_ENTRY_MNDX pMndxEntries;
    PCASC_MNDX_PACKAGES pPackages;              // Linear list of present packages
};

PCASC_MNDX_PACKAGE FindMndxPackage(TRootHandler_MNDX * pRootHandler, const char * szFileName)
{
    PCASC_MNDX_PACKAGE pMatching = NULL;
    PCASC_MNDX_PACKAGE pPackage;
    size_t nMaxLength = 0;
    size_t nLength = strlen(szFileName);

    // Packages must be loaded
    assert(pRootHandler->pPackages != NULL);
    pPackage = pRootHandler->pPackages->Packages;

    //FILE * fp = fopen("E:\\packages.txt", "wt");
    //for(size_t i = 0; i < hs->pPackages->NameEntries; i++, pPackage++)
    //{
    //    if(pPackage->szFileName != NULL)
    //        fprintf(fp, "%s\n", pPackage->szFileName);
    //}
    //fclose(fp);

    // Find the longest matching name
    for(size_t i = 0; i < pRootHandler->pPackages->NameEntries; i++, pPackage++)
    {
        if(pPackage->szFileName != NULL && pPackage->nLength < nLength && pPackage->nLength > nMaxLength)
        {
            // Compare the package name
            if(!strncmp(szFileName, pPackage->szFileName, pPackage->nLength))
            {
                pMatching = pPackage;
                nMaxLength = pPackage->nLength;
            }
        }
    }

    // Give the package pointer or NULL if not found
    return pMatching;
}

int SearchMndxInfo(TRootHandler_MNDX * pRootHandler, const char * szFileName, DWORD dwPackage, PCASC_ROOT_ENTRY_MNDX * ppRootEntry)
{
    PCASC_ROOT_ENTRY_MNDX pRootEntry;
    PCASC_MNDX_INFO pMndxInfo = &pRootHandler->MndxInfo;
    TMndxFindResult Struct1C;

    // Search the database for the file name
    if(pMndxInfo->bRootFileLoaded)
    {
        Struct1C.SetSearchPath(szFileName, strlen(szFileName));

        // Search the file name in the second MAR info (the one with stripped package names)
        if(MAR_FILE_SearchFile(pMndxInfo->pMarFile2, &Struct1C) != ERROR_SUCCESS)
            return ERROR_FILE_NOT_FOUND;

        // The found MNDX index must fall into range of valid MNDX entries
        if(Struct1C.FileNameIndex < pMndxInfo->MndxEntriesValid)
        {
            // HOTS: E945F4
            pRootEntry = pRootHandler->ppValidEntries[Struct1C.FileNameIndex];
            while((pRootEntry->Flags & 0x00FFFFFF) != dwPackage)
            {
                // The highest bit serves as a terminator if set
                if(pRootEntry->Flags & 0x80000000)
                    return ERROR_FILE_NOT_FOUND;

                pRootEntry++;
            }

            // Give the root entry pointer to the caller
            if(ppRootEntry != NULL)
                ppRootEntry[0] = pRootEntry;
            return ERROR_SUCCESS;
        }
    }

    return ERROR_FILE_NOT_FOUND;
}

static LPBYTE FillFindData(TRootHandler_MNDX * pRootHandler, TCascSearch * pSearch, TMndxFindResult * pStruct1C, PDWORD PtrFileSize)
{
    PCASC_ROOT_ENTRY_MNDX pRootEntry = NULL;
    PCASC_MNDX_PACKAGE pPackage;
    char * szStrippedPtr;
    char szStrippedName[MAX_PATH+1];
    int nError;

    // Sanity check
    assert(pStruct1C->cchFoundPath < MAX_PATH);

    // Fill the file name
    memcpy(pSearch->szFileName, pStruct1C->szFoundPath, pStruct1C->cchFoundPath);
    pSearch->szFileName[pStruct1C->cchFoundPath] = 0;

    // Fill the file size
    pPackage = FindMndxPackage(pRootHandler, pSearch->szFileName);
    if(pPackage == NULL)
        return NULL;

    // Cut the package name off the full path
    szStrippedPtr = pSearch->szFileName + pPackage->nLength;
    while(szStrippedPtr[0] == '/')
        szStrippedPtr++;

    // We need to convert the stripped name to lowercase, replacing backslashes with slashes
    NormalizeFileName_LowerSlash(szStrippedName, szStrippedPtr, MAX_PATH);

    // Search the package
    nError = SearchMndxInfo(pRootHandler, szStrippedName, (DWORD)(pPackage - pRootHandler->pPackages->Packages), &pRootEntry);
    if(nError != ERROR_SUCCESS)
        return NULL;

    // Give the file size
    if(PtrFileSize != NULL)
        PtrFileSize[0] = pRootEntry->FileSize;
    return pRootEntry->EncodingKey;
}

static int MndxHandler_Insert(TRootHandler_MNDX *, const char *, LPBYTE)
{
    return ERROR_NOT_SUPPORTED;
}

static LPBYTE MndxHandler_Search(TRootHandler_MNDX * pRootHandler, TCascSearch * pSearch, PDWORD PtrFileSize, PDWORD /* PtrLocaleFlags */)
{
    TMndxFindResult * pStruct1C = NULL;
    PCASC_MNDX_INFO pMndxInfo = &pRootHandler->MndxInfo;
    PMAR_FILE pMarFile = pMndxInfo->pMarFile3;
    bool bFindResult = false;

    // If the first time, allocate the structure for the search result
    if(pSearch->pRootContext == NULL)
    {
        // Create the new search structure
        pStruct1C = new TMndxFindResult;
        if(pStruct1C == NULL)
            return NULL;

        // Setup the search mask
        pStruct1C->SetSearchPath("", 0);
        pSearch->pRootContext = pStruct1C;
    }

    // Make shortcut for the search structure
    assert(pSearch->pRootContext != NULL);
    pStruct1C = (TMndxFindResult *)pSearch->pRootContext;

    // Search the next file name (our code)
    pMarFile->pDatabasePtr->sub_1956CE0(pStruct1C, &bFindResult);
    if(bFindResult == false)
        return NULL;

    // Give the file size and encoding key
    return FillFindData(pRootHandler, pSearch, pStruct1C, PtrFileSize);
}

static void MndxHandler_EndSearch(TRootHandler_MNDX * /* pRootHandler */, TCascSearch * pSearch)
{
    if(pSearch != NULL)
        delete (TMndxFindResult *)pSearch->pRootContext;
    pSearch->pRootContext = NULL;
}

static LPBYTE MndxHandler_GetKey(TRootHandler_MNDX * pRootHandler, const char * szFileName)
{
    PCASC_ROOT_ENTRY_MNDX pRootEntry = NULL;
    PCASC_MNDX_PACKAGE pPackage;
    char * szStrippedName;
    char szNormName[MAX_PATH+1];
    int nError;

    // Convert the file name to lowercase + slashes
    NormalizeFileName_LowerSlash(szNormName, szFileName, MAX_PATH);

    // Find the package number
    pPackage = FindMndxPackage(pRootHandler, szNormName);
    if(pPackage == NULL)
        return NULL;

    // Cut the package name off the full path
    szStrippedName = szNormName + pPackage->nLength;
    while(szStrippedName[0] == '/')
        szStrippedName++;

    // Find the root entry
    nError = SearchMndxInfo(pRootHandler, szStrippedName, (DWORD)(pPackage - pRootHandler->pPackages->Packages), &pRootEntry);
    if(nError != ERROR_SUCCESS || pRootEntry == NULL)
        return NULL;

    // Return the encoding key
    return pRootEntry->EncodingKey;
}

static void MndxHandler_Close(TRootHandler_MNDX * pRootHandler)
{
    if(pRootHandler->MndxInfo.pMarFile1 != NULL)
        MAR_FILE_Destructor(pRootHandler->MndxInfo.pMarFile1);
    if(pRootHandler->MndxInfo.pMarFile2 != NULL)
        MAR_FILE_Destructor(pRootHandler->MndxInfo.pMarFile2);
    if(pRootHandler->MndxInfo.pMarFile3 != NULL)
        MAR_FILE_Destructor(pRootHandler->MndxInfo.pMarFile3);
    if(pRootHandler->ppValidEntries != NULL)
        CASC_FREE(pRootHandler->ppValidEntries);
    if(pRootHandler->pMndxEntries != NULL)
        CASC_FREE(pRootHandler->pMndxEntries);
    if(pRootHandler->pPackages != NULL)
        CASC_FREE(pRootHandler->pPackages);

    CASC_FREE(pRootHandler);
}

//-----------------------------------------------------------------------------
// Public functions - MNDX info

int RootHandler_CreateMNDX(TCascStorage * hs, LPBYTE pbRootFile, DWORD cbRootFile)
{
    PFILE_MNDX_HEADER pMndxHeader = (PFILE_MNDX_HEADER)pbRootFile;
    PCASC_MNDX_INFO pMndxInfo;
    TRootHandler_MNDX * pRootHandler;
    FILE_MAR_INFO MarInfo;
    PMAR_FILE pMarFile;
    LPBYTE pbRootFileEnd = pbRootFile + cbRootFile;
    DWORD cbToAllocate;
    DWORD dwFilePointer = 0;
    DWORD i;
    int nError = ERROR_SUCCESS;

    // Check signature and the other variables
    if(pMndxHeader->Signature != CASC_MNDX_SIGNATURE || pMndxHeader->FormatVersion > 2 || pMndxHeader->FormatVersion < 1)
        return ERROR_BAD_FORMAT;

    // Allocate the structure for the MNDX root file
    hs->pRootHandler = pRootHandler = CASC_ALLOC(TRootHandler_MNDX, 1);
    if(pRootHandler == NULL)
        return ERROR_NOT_ENOUGH_MEMORY;

    // Fill-in the handler functions
    memset(pRootHandler, 0, sizeof(TRootHandler_MNDX));
    pRootHandler->Insert      = (ROOT_INSERT)MndxHandler_Insert;
    pRootHandler->Search      = (ROOT_SEARCH)MndxHandler_Search;
    pRootHandler->EndSearch   = (ROOT_ENDSEARCH)MndxHandler_EndSearch;
    pRootHandler->GetKey      = (ROOT_GETKEY)MndxHandler_GetKey;
    pRootHandler->Close       = (ROOT_CLOSE) MndxHandler_Close;
    pMndxInfo = &pRootHandler->MndxInfo;

    // Fill-in the flags
    pRootHandler->dwRootFlags |= ROOT_FLAG_HAS_NAMES;

    // Copy the header into the MNDX info
    pMndxInfo->HeaderVersion = pMndxHeader->HeaderVersion;
    pMndxInfo->FormatVersion = pMndxHeader->FormatVersion;
    dwFilePointer += sizeof(FILE_MNDX_HEADER);

    // Header version 2 has 2 extra fields that we need to load
    if(pMndxInfo->HeaderVersion == 2)
    {
        if(!RootFileRead(pbRootFile + dwFilePointer, pbRootFileEnd, &pMndxInfo->field_1C, sizeof(DWORD) + sizeof(DWORD)))
            return ERROR_FILE_CORRUPT;
        dwFilePointer += sizeof(DWORD) + sizeof(DWORD);
    }

    // Load the rest of the file header
    if(!RootFileRead(pbRootFile + dwFilePointer, pbRootFileEnd, &pMndxInfo->MarInfoOffset, 0x1C))
        return ERROR_FILE_CORRUPT;

    // Verify the structure
    if(pMndxInfo->MarInfoCount > CASC_MAX_MAR_FILES || pMndxInfo->MarInfoSize != sizeof(FILE_MAR_INFO))
        return ERROR_FILE_CORRUPT;

    // Load all MAR infos
    for(i = 0; i < pMndxInfo->MarInfoCount; i++)
    {
        // Load the n-th MAR info
        dwFilePointer = pMndxInfo->MarInfoOffset + (pMndxInfo->MarInfoSize * i);
        if(!RootFileRead(pbRootFile + dwFilePointer, pbRootFileEnd, &MarInfo, sizeof(FILE_MAR_INFO)))
            return ERROR_FILE_CORRUPT;

        // Allocate MAR_FILE structure
        pMarFile = CASC_ALLOC(MAR_FILE, 1);
        if(pMarFile == NULL)
        {
            nError = ERROR_NOT_ENOUGH_MEMORY;
            break;
        }

        // Allocate space for the MAR data
        pMarFile->pDatabasePtr = NULL;
        pMarFile->pbMarData = CASC_ALLOC(BYTE, MarInfo.MarDataSize);
        pMarFile->cbMarData = MarInfo.MarDataSize;
        if(pMarFile->pbMarData == NULL)
        {
            nError = ERROR_NOT_ENOUGH_MEMORY;
            break;
        }

        // Read the MAR data
        if(!RootFileRead(pbRootFile + MarInfo.MarDataOffset, pbRootFileEnd, pMarFile->pbMarData, pMarFile->cbMarData))
        {
            nError = ERROR_FILE_CORRUPT;
            break;
        }

        // HOTS: 00E94FF1
        MAR_FILE_CreateDatabase(pMarFile);
        if(i == 0)
            pMndxInfo->pMarFile1 = pMarFile;
        if(i == 1)
            pMndxInfo->pMarFile2 = pMarFile;
        if(i == 2)
            pMndxInfo->pMarFile3 = pMarFile;
    }

    // All three MAR files must be loaded
    // HOTS: 00E9503B
    if(nError == ERROR_SUCCESS)
    {
        if(pMndxInfo->pMarFile1 == NULL || pMndxInfo->pMarFile2 == NULL || pMndxInfo->pMarFile3 == NULL)
            nError = ERROR_BAD_FORMAT;
        if(pMndxInfo->MndxEntrySize != sizeof(CASC_ROOT_ENTRY_MNDX))
            nError = ERROR_BAD_FORMAT;
    }

    // Load the complete array of MNDX entries
    if(nError == ERROR_SUCCESS)
    {
        TFileNameDatabasePtr * pDbPtr = pMndxInfo->pMarFile2->pDatabasePtr;
        DWORD FileNameCount;

        nError = pDbPtr->GetFileNameCount(&FileNameCount);
        if(nError == ERROR_SUCCESS && FileNameCount == pMndxInfo->MndxEntriesValid)
        {
            cbToAllocate = pMndxInfo->MndxEntriesTotal * pMndxInfo->MndxEntrySize;
            pRootHandler->pMndxEntries = (PCASC_ROOT_ENTRY_MNDX)CASC_ALLOC(BYTE, cbToAllocate);
            if(pRootHandler->pMndxEntries != NULL)
            {
                if(!RootFileRead(pbRootFile + pMndxInfo->MndxEntriesOffset, pbRootFileEnd, pRootHandler->pMndxEntries, cbToAllocate))
                    nError = ERROR_FILE_CORRUPT;
            }
            else
                nError = ERROR_NOT_ENOUGH_MEMORY;
        }
        else
            nError = ERROR_FILE_CORRUPT;
    }

    // Pick the valid MNDX entries and put them to a separate array
    if(nError == ERROR_SUCCESS)
    {
        assert(pMndxInfo->MndxEntriesValid <= pMndxInfo->MndxEntriesTotal);
        pRootHandler->ppValidEntries = CASC_ALLOC(PCASC_ROOT_ENTRY_MNDX, pMndxInfo->MndxEntriesValid + 1);
        if(pRootHandler->ppValidEntries != NULL)
        {
            PCASC_ROOT_ENTRY_MNDX pRootEntry = pRootHandler->pMndxEntries;
            DWORD ValidEntryCount = 1; // edx
            DWORD nIndex1 = 0;

            // The first entry is always valid
            pRootHandler->ppValidEntries[nIndex1++] = pRootHandler->pMndxEntries;

            // Put the remaining entries
            for(i = 0; i < pMndxInfo->MndxEntriesTotal; i++, pRootEntry++)
            {
                if(ValidEntryCount > pMndxInfo->MndxEntriesValid)
                    break;

                if(pRootEntry->Flags & 0x80000000)
                {
                    pRootHandler->ppValidEntries[nIndex1++] = pRootEntry + 1;
                    ValidEntryCount++;
                }
            }

            // Verify the final number of valid entries
            if((ValidEntryCount - 1) != pMndxInfo->MndxEntriesValid)
                nError = ERROR_BAD_FORMAT;
        }
        else
            nError = ERROR_NOT_ENOUGH_MEMORY;
    }

    // Load the MNDX packages
    if(nError == ERROR_SUCCESS)
    {
        nError = LoadPackageNames(pMndxInfo, &pRootHandler->pPackages);
        pMndxInfo->bRootFileLoaded = (nError == ERROR_SUCCESS);
    }

#if defined(_DEBUG) && defined(_X86_) && defined(CASCLIB_TEST)
//  CascDumpNameFragTable("E:\\casc-name-fragment-table.txt", pMndxInfo->pMarFile1);
//  CascDumpFileNames("E:\\casc-listfile.txt", pMndxInfo->pMarFile1);
//  TestMndxRootFile(pRootHandler);
#endif

    // Return the result
    return nError;
}

//----------------------------------------------------------------------------
// Unit tests

#if defined(_DEBUG) && defined(_X86_) && defined(CASCLIB_TEST)

extern "C" {
    DWORD _cdecl sub_19573D0_x86(TFileNameDatabase * pDB, DWORD arg_0, DWORD arg_4);
    DWORD _cdecl sub_1957EF0_x86(TFileNameDatabase * pDB, TMndxFindResult * pStruct1C);
    bool  _cdecl sub_1959460_x86(TFileNameDatabase * pDB, TMndxFindResult * pStruct1C);
    DWORD _cdecl GetItemValue_x86(TSparseArray * pStruct, DWORD dwKey);
    DWORD _cdecl sub_1959CB0_x86(TFileNameDatabase * pDB, DWORD dwKey);
    DWORD _cdecl sub_1959F50_x86(TFileNameDatabase * pDB, DWORD arg_0);
}

extern "C" void * allocate_zeroed_memory_x86(size_t bytes)
{
    void * ptr = CASC_ALLOC(BYTE, bytes);

    if(ptr != NULL)
        memset(ptr, 0, bytes);
    return ptr;
}

extern "C" void free_memory_x86(void * ptr)
{
    if(ptr != NULL)
    {
        CASC_FREE(ptr);
    }
}

static int sub_1956CE0_x86(TFileNameDatabasePtr * pDatabasePtr, TMndxFindResult * pStruct1C, bool * pbFindResult)
{
    int nError = ERROR_SUCCESS;

    if(pDatabasePtr->pDB == NULL)
        return ERROR_INVALID_PARAMETER;

    // Create the pStruct40, if not initialized yet
    if(pStruct1C->pStruct40 == NULL)
    {
        nError = pStruct1C->CreateStruct40();
        if(nError != ERROR_SUCCESS)
            return nError;
    }

    *pbFindResult = sub_1959460_x86(pDatabasePtr->pDB, pStruct1C);
    return nError;
}
/*
static void TestFileSearch_SubStrings(PMAR_FILE pMarFile, char * szFileName, size_t nLength)
{
    TMndxFindResult Struct1C_1;
    TMndxFindResult Struct1C_2;

//  if(strcmp(szFileName, "mods/heroes.stormmod/base.stormassets/assets/textures/storm_temp_war3_btnstatup.dds"))
//      return;

    // Perform search on anything, that is longer than 4 chars
    while(nLength >= 4)
    {
        // Set a substring as search name
        Struct1C_1.SetSearchPath(szFileName, nLength);
        Struct1C_2.SetSearchPath(szFileName, nLength);
        szFileName[nLength] = 0;

        // Keep searching
        for(;;)
        {
            bool bFindResult1 = false;
            bool bFindResult2 = false;

            // Search the next file name (orig HOTS code)
            sub_1956CE0_x86(pMarFile->pDatabasePtr, &Struct1C_1, &bFindResult1);

            // Search the next file name (our code)
            pMarFile->pDatabasePtr->sub_1956CE0(&Struct1C_2, &bFindResult2);

            // Check the result
            assert(bFindResult1 == bFindResult2);
            assert(Struct1C_1.cchFoundPath == Struct1C_1.cchFoundPath);
            assert(Struct1C_1.FileNameIndex == Struct1C_2.FileNameIndex);
            assert(strncmp(Struct1C_1.szFoundPath, Struct1C_2.szFoundPath, Struct1C_1.cchFoundPath) == 0);
            assert(Struct1C_1.cchFoundPath < MAX_PATH);

            // Stop the search in case of failure
            if(bFindResult1 == false || bFindResult2 == false)
                break;
        }

        // Free the search structures
        Struct1C_1.FreeStruct40();
        Struct1C_2.FreeStruct40();
        nLength--;
    }
}
*/

static void TestFindPackage(PMAR_FILE pMarFile, const char * szPackageName)
{
    TMndxFindResult Struct1C;

    // Search the database for the file name
    Struct1C.SetSearchPath(szPackageName, strlen(szPackageName));

    // Search the file name in the second MAR info (the one with stripped package names)
    MAR_FILE_SearchFile(pMarFile, &Struct1C);
}

static void TestFileSearch(PMAR_FILE pMarFile, const char * szFileName)
{
    TMndxFindResult Struct1C_1;
    TMndxFindResult Struct1C_2;
    size_t nLength = strlen(szFileName);
    char szNameBuff[MAX_PATH + 1];

    // Set an empty path as search mask (?)
    Struct1C_1.SetSearchPath(szFileName, nLength);
    Struct1C_2.SetSearchPath(szFileName, nLength);

    // Keep searching
    for(;;)
    {
        bool bFindResult1 = false;
        bool bFindResult2 = false;

        // Search the next file name (orig HOTS code)
        sub_1956CE0_x86(pMarFile->pDatabasePtr, &Struct1C_1, &bFindResult1);

        // Search the next file name (our code)
        pMarFile->pDatabasePtr->sub_1956CE0(&Struct1C_2, &bFindResult2);

        assert(bFindResult1 == bFindResult2);
        assert(Struct1C_1.cchFoundPath == Struct1C_1.cchFoundPath);
        assert(Struct1C_1.FileNameIndex == Struct1C_2.FileNameIndex);
        assert(strncmp(Struct1C_1.szFoundPath, Struct1C_2.szFoundPath, Struct1C_1.cchFoundPath) == 0);
        assert(Struct1C_1.cchFoundPath < MAX_PATH);

        // Stop the search in case of failure
        if(bFindResult1 == false || bFindResult2 == false)
            break;

        // Printf the found file name
        memcpy(szNameBuff, Struct1C_2.szFoundPath, Struct1C_2.cchFoundPath);
        szNameBuff[Struct1C_2.cchFoundPath] = 0;
//      printf("%s        \r", szNameBuff);

        // Perform sub-searches on this string and its substrings that are longer than 4 chars
//      TestFileSearch_SubStrings(pMarFile, szNameBuff, Struct1C_2.cchFoundPath);
    }

    // Free the search structures
    Struct1C_1.FreeStruct40();
    Struct1C_2.FreeStruct40();
}

static void TestMarFile(PMAR_FILE pMarFile, const char * szFileName, size_t nLength)
{
    TFileNameDatabase * pDB = pMarFile->pDatabasePtr->pDB;
    DWORD dwFileNameIndex1 = 0xFFFFFFFF;
    DWORD dwFileNameIndex2 = 0xFFFFFFFF;

    // Perform the search using original HOTS code
    {
        TMndxFindResult Struct1C;

        Struct1C.CreateStruct40();
        Struct1C.SetSearchPath(szFileName, nLength);

        // Call the original HOTS function
        sub_1957EF0_x86(pDB, &Struct1C);
        dwFileNameIndex1 = Struct1C.FileNameIndex;
    }

    // Perform the search using our code
    {
        TMndxFindResult Struct1C;

        Struct1C.CreateStruct40();
        Struct1C.SetSearchPath(szFileName, nLength);

        // Call our function
        pDB->FindFileInDatabase(&Struct1C);
        dwFileNameIndex2 = Struct1C.FileNameIndex;
    }

    // Compare both
    assert(dwFileNameIndex1 == dwFileNameIndex2);
}

static void TestMndxFunctions(PMAR_FILE pMarFile)
{
    TFileNameDatabase * pDB = pMarFile->pDatabasePtr->pDB;

    // Exercise function sub_19573D0
    for(DWORD arg_0 = 0; arg_0 < 0x100; arg_0++)
    {
        for(DWORD arg_4 = 0; arg_4 < 0x100; arg_4++)
        {
            DWORD dwResult1 = sub_19573D0_x86(pDB, arg_0, arg_4);
            DWORD dwResult2 = pDB->GetNameFragmentOffsetEx(arg_0, arg_4);

            assert(dwResult1 == dwResult2);
        }
    }

    // Exercise function GetItemValue
    for(DWORD i = 0; i < 0x10000; i++)
    {
        DWORD dwResult1 = GetItemValue_x86(&pDB->Struct68_D0, i);
        DWORD dwResult2 = pDB->Struct68_D0.GetItemValue(i);

        assert(dwResult1 == dwResult2);
    }

    // Exercise function sub_1959CB0
    for(DWORD i = 0; i < 0x9C; i++)
    {
        DWORD dwResult1 = sub_1959CB0_x86(pDB, i);
        DWORD dwResult2 = pDB->sub_1959CB0(i);

        assert(dwResult1 == dwResult2);
    }

    // Exercise function sub_1959F50
    for(DWORD i = 0; i < 0x40; i++)
    {
        DWORD dwResult1 = sub_1959F50_x86(pDB, i);
        DWORD dwResult2 = pDB->sub_1959F50(i);

        assert(dwResult1 == dwResult2);
    }
}

void TestMndxRootFile(PCASC_MNDX_INFO pMndxInfo)
{
    size_t nLength;
    char szFileName[MAX_PATH+1];
    void * pvListFile;

    // Exercise low level functions and compare their results
    // with original code from Heroes of the Storm
    TestMndxFunctions(pMndxInfo->pMarFile1);
    TestMndxFunctions(pMndxInfo->pMarFile2);
    TestMndxFunctions(pMndxInfo->pMarFile3);

    // Find a "mods" in the package array
    TestFindPackage(pMndxInfo->pMarFile3, "mods/heroes.stormmod/base.stormassets/assets/textures/glow_green2.dds");
    TestMarFile(pMndxInfo->pMarFile3, "mods/heroes.stormmod/base.stormassets/assets/textures/glow_green2.dds", 69);

    // Search the package MAR file aith a path shorter than a fragment
    TestFileSearch(pMndxInfo->pMarFile1, "mods/heroes.s");

    // Test the file search
    TestFileSearch(pMndxInfo->pMarFile1, "");
    TestFileSearch(pMndxInfo->pMarFile2, "");
    TestFileSearch(pMndxInfo->pMarFile3, "");

    // False file search
    TestFileSearch(pMndxInfo->pMarFile2, "assets/textures/storm_temp_hrhu");

    // Open the listfile stream and initialize the listfile cache
    pvListFile = ListFile_OpenExternal(_T("e:\\Ladik\\Appdir\\CascLib\\listfile\\listfile-hots-29049.txt"));
    if(pvListFile != NULL)
    {
        // Check every file in the database
        while((nLength = ListFile_GetNext(pvListFile, "*", szFileName, MAX_PATH)) != 0)
        {
            // Normalize the file name: ToLower + BackSlashToSlash
            NormalizeFileName_LowerSlash(szFileName, szFileName, MAX_PATH);

            // Check the file with all three MAR files
            TestMarFile(pMndxInfo->pMarFile1, szFileName, nLength);
            TestMarFile(pMndxInfo->pMarFile2, szFileName, nLength);
            TestMarFile(pMndxInfo->pMarFile3, szFileName, nLength);
        }

        ListFile_Free(pvListFile);
    }
}
#endif  // defined(_DEBUG) && defined(_X86_) && defined(CASCLIB_TEST)