aboutsummaryrefslogtreecommitdiff
path: root/CPP/7zip/Archive/Zip/ZipIn.cpp
blob: 4236df71d4d9f2cdfceac12b00ac3325a5eed2cb (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
// Archive/ZipIn.cpp

#include "StdAfx.h"

// #include <stdio.h>

#include "../../../Common/DynamicBuffer.h"
#include "../../../Common/IntToString.h"
#include "../../../Common/MyException.h"
#include "../../../Common/StringToInt.h"

#include "../../../Windows/PropVariant.h"

#include "../IArchive.h"

#include "ZipIn.h"

#define Get16(p) GetUi16(p)
#define Get32(p) GetUi32(p)
#define Get64(p) GetUi64(p)

#define G16(offs, v) v = Get16(p + (offs))
#define G32(offs, v) v = Get32(p + (offs))
#define G64(offs, v) v = Get64(p + (offs))

namespace NArchive {
namespace NZip {

/* we try to use same size of Buffer (1 << 17) for all tasks.
   it allow to avoid reallocations and cache clearing. */

static const size_t kSeqBufferSize = (size_t)1 << 17;

/*
Open()
{
  _inBufMode = false;
  ReadVols()
    FindCd();
      TryEcd64()
  SeekToVol()
  FindMarker()
    _inBufMode = true;
  ReadHeaders()
    _inBufMode = false;
    ReadCd()
      FindCd()
        TryEcd64()
      TryReadCd()
      {
        SeekToVol();
        _inBufMode = true;
      }
    _inBufMode = true;
    ReadLocals()
    ReadCdItem()
    ....
}
FindCd() writes to Buffer without touching (_inBufMode)
*/

/*
  if (not defined ZIP_SELF_CHECK) : it reads CD and if error in first pass CD reading, it reads LOCALS-CD-MODE
  if (    defined ZIP_SELF_CHECK) : it always reads CD and LOCALS-CD-MODE
  use ZIP_SELF_CHECK to check LOCALS-CD-MODE for any zip archive
*/

// #define ZIP_SELF_CHECK


struct CEcd
{
  UInt16 ThisDisk;
  UInt16 CdDisk;
  UInt16 NumEntries_in_ThisDisk;
  UInt16 NumEntries;
  UInt32 Size;
  UInt32 Offset;
  UInt16 CommentSize;
  
  bool IsEmptyArc() const
  {
    return ThisDisk == 0
        && CdDisk == 0
        && NumEntries_in_ThisDisk == 0
        && NumEntries == 0
        && Size == 0
        && Offset == 0 // test it
    ;
  }

  void Parse(const Byte *p); // (p) doesn't include signature
};

void CEcd::Parse(const Byte *p)
{
  // (p) doesn't include signature
  G16(0, ThisDisk);
  G16(2, CdDisk);
  G16(4, NumEntries_in_ThisDisk);
  G16(6, NumEntries);
  G32(8, Size);
  G32(12, Offset);
  G16(16, CommentSize);
}


void CCdInfo::ParseEcd32(const Byte *p)
{
  IsFromEcd64 = false;
  // (p) includes signature
  p += 4;
  G16(0, ThisDisk);
  G16(2, CdDisk);
  G16(4, NumEntries_in_ThisDisk);
  G16(6, NumEntries);
  G32(8, Size);
  G32(12, Offset);
  G16(16, CommentSize);
}

void CCdInfo::ParseEcd64e(const Byte *p)
{
  IsFromEcd64 = true;
  // (p) exclude signature
  G16(0, VersionMade);
  G16(2, VersionNeedExtract);
  G32(4, ThisDisk);
  G32(8, CdDisk);

  G64(12, NumEntries_in_ThisDisk);
  G64(20, NumEntries);
  G64(28, Size);
  G64(36, Offset);
}


struct CLocator
{
  UInt32 Ecd64Disk;
  UInt32 NumDisks;
  UInt64 Ecd64Offset;
  
  CLocator(): Ecd64Disk(0), NumDisks(0), Ecd64Offset(0) {}

  void Parse(const Byte *p)
  {
    G32(0, Ecd64Disk);
    G64(4, Ecd64Offset);
    G32(12, NumDisks);
  }

  bool IsEmptyArc() const
  {
    return Ecd64Disk == 0 && NumDisks == 0 && Ecd64Offset == 0;
  }
};

  


void CInArchive::ClearRefs()
{
  StreamRef.Release();
  Stream = NULL;
  StartStream = NULL;
  Callback = NULL;

  Vols.Clear();
}

void CInArchive::Close()
{
  _cnt = 0;
  DisableBufMode();

  IsArcOpen = false;

  IsArc = false;
  IsZip64 = false;
  
  IsApk = false;
  IsCdUnsorted = false;

  HeadersError = false;
  HeadersWarning = false;
  ExtraMinorError = false;
  
  UnexpectedEnd = false;
  LocalsWereRead = false;
  LocalsCenterMerged = false;
  NoCentralDir = false;
  Overflow32bit = false;
  Cd_NumEntries_Overflow_16bit = false;
  
  MarkerIsFound = false;
  MarkerIsSafe = false;

  IsMultiVol = false;
  UseDisk_in_SingleVol = false;
  EcdVolIndex = 0;
 
  ArcInfo.Clear();

  ClearRefs();
}



HRESULT CInArchive::Seek_SavePos(UInt64 offset)
{
  // InitBuf();
  // if (!Stream) return S_FALSE;
  return Stream->Seek((Int64)offset, STREAM_SEEK_SET, &_streamPos);
}


/* SeekToVol() will keep the cached mode, if new volIndex is
   same Vols.StreamIndex volume, and offset doesn't go out of cached region */

HRESULT CInArchive::SeekToVol(int volIndex, UInt64 offset)
{
  if (volIndex != Vols.StreamIndex)
  {
    if (IsMultiVol && volIndex >= 0)
    {
      if ((unsigned)volIndex >= Vols.Streams.Size())
        return S_FALSE;
      if (!Vols.Streams[(unsigned)volIndex].Stream)
        return S_FALSE;
      Stream = Vols.Streams[(unsigned)volIndex].Stream;
    }
    else if (volIndex == -2)
    {
      if (!Vols.ZipStream)
        return S_FALSE;
      Stream = Vols.ZipStream;
    }
    else
      Stream = StartStream;
    Vols.StreamIndex = volIndex;
  }
  else
  {
    if (offset <= _streamPos)
    {
      const UInt64 back = _streamPos - offset;
      if (back <= _bufCached)
      {
        _bufPos = _bufCached - (size_t)back;
        return S_OK;
      }
    }
  }
  InitBuf();
  return Seek_SavePos(offset);
}


HRESULT CInArchive::AllocateBuffer(size_t size)
{
  if (size <= Buffer.Size())
    return S_OK;
  /* in cached mode virtual_pos is not equal to phy_pos (_streamPos)
     so we change _streamPos and do Seek() to virtual_pos before cache clearing */
  if (_bufPos != _bufCached)
  {
    RINOK(Seek_SavePos(GetVirtStreamPos()))
  }
  InitBuf();
  Buffer.AllocAtLeast(size);
  if (!Buffer.IsAllocated())
    return E_OUTOFMEMORY;
  return S_OK;
}

// ---------- ReadFromCache ----------
// reads from cache and from Stream
// move to next volume can be allowed if (CanStartNewVol) and only before first byte reading

HRESULT CInArchive::ReadFromCache(Byte *data, unsigned size, unsigned &processed)
{
  HRESULT result = S_OK;
  processed = 0;

  for (;;)
  {
    if (size == 0)
      return S_OK;
    
    const size_t avail = GetAvail();

    if (avail != 0)
    {
      unsigned cur = size;
      if (cur > avail)
        cur = (unsigned)avail;
      memcpy(data, (const Byte *)Buffer + _bufPos, cur);

      data += cur;
      size -= cur;
      processed += cur;

      _bufPos += cur;
      _cnt += cur;

      CanStartNewVol = false;
      
      continue;
    }

    InitBuf();

    if (_inBufMode)
    {
      UInt32 cur = 0;
      result = Stream->Read(Buffer, (UInt32)Buffer.Size(), &cur);
      _bufPos = 0;
      _bufCached = cur;
      _streamPos += cur;
      if (cur != 0)
        CanStartNewVol = false;
      if (result != S_OK)
        break;
      if (cur != 0)
        continue;
    }
    else
    {
      size_t cur = size;
      result = ReadStream(Stream, data, &cur);
      data += cur;
      size -= (unsigned)cur;
      processed += (unsigned)cur;
      _streamPos += cur;
      _cnt += cur;
      if (cur != 0)
      {
        CanStartNewVol = false;
        break;
      }
      if (result != S_OK)
        break;
    }

    if (   !IsMultiVol
        || !CanStartNewVol
        || Vols.StreamIndex < 0
        || (unsigned)Vols.StreamIndex + 1 >= Vols.Streams.Size())
      break;

    const CVols::CSubStreamInfo &s = Vols.Streams[(unsigned)Vols.StreamIndex + 1];
    if (!s.Stream)
      break;
    result = s.SeekToStart();
    if (result != S_OK)
      break;
    Vols.StreamIndex++;
    _streamPos = 0;
    // Vols.NeedSeek = false;

    Stream = s.Stream;
  }

  return result;
}


HRESULT CInArchive::ReadFromCache_FALSE(Byte *data, unsigned size)
{
  unsigned processed;
  HRESULT res = ReadFromCache(data, size, processed);
  if (res == S_OK && size != processed)
    return S_FALSE;
  return res;
}


static bool CheckDosTime(UInt32 dosTime)
{
  if (dosTime == 0)
    return true;
  unsigned month = (dosTime >> 21) & 0xF;
  unsigned day = (dosTime >> 16) & 0x1F;
  unsigned hour = (dosTime >> 11) & 0x1F;
  unsigned min = (dosTime >> 5) & 0x3F;
  unsigned sec = (dosTime & 0x1F) * 2;
  if (month < 1 || month > 12 || day < 1 || day > 31 || hour > 23 || min > 59 || sec > 59)
    return false;
  return true;
}

API_FUNC_IsArc IsArc_Zip(const Byte *p, size_t size)
{
  if (size < 8)
    return k_IsArc_Res_NEED_MORE;
  if (p[0] != 'P')
    return k_IsArc_Res_NO;

  UInt32 sig = Get32(p);

  if (sig == NSignature::kNoSpan || sig == NSignature::kSpan)
  {
    p += 4;
    size -= 4;
  }

  sig = Get32(p);

  if (sig == NSignature::kEcd64)
  {
    if (size < kEcd64_FullSize)
      return k_IsArc_Res_NEED_MORE;

    const UInt64 recordSize = Get64(p + 4);
    if (   recordSize < kEcd64_MainSize
        || recordSize > kEcd64_MainSize + (1 << 20))
      return k_IsArc_Res_NO;
    CCdInfo cdInfo;
    cdInfo.ParseEcd64e(p + 12);
    if (!cdInfo.IsEmptyArc())
      return k_IsArc_Res_NO;
    return k_IsArc_Res_YES; // k_IsArc_Res_YES_2;
  }

  if (sig == NSignature::kEcd)
  {
    if (size < kEcdSize)
      return k_IsArc_Res_NEED_MORE;
    CEcd ecd;
    ecd.Parse(p + 4);
    // if (ecd.cdSize != 0)
    if (!ecd.IsEmptyArc())
      return k_IsArc_Res_NO;
    return k_IsArc_Res_YES; // k_IsArc_Res_YES_2;
  }
 
  if (sig != NSignature::kLocalFileHeader)
    return k_IsArc_Res_NO;

  if (size < kLocalHeaderSize)
    return k_IsArc_Res_NEED_MORE;
  
  p += 4;

  {
    const unsigned kPureHeaderSize = kLocalHeaderSize - 4;
    unsigned i;
    for (i = 0; i < kPureHeaderSize && p[i] == 0; i++);
    if (i == kPureHeaderSize)
      return k_IsArc_Res_NEED_MORE;
  }

  /*
  if (p[0] >= 128) // ExtractVersion.Version;
    return k_IsArc_Res_NO;
  */

  // ExtractVersion.Version = p[0];
  // ExtractVersion.HostOS = p[1];
  // Flags = Get16(p + 2);
  // Method = Get16(p + 4);
  /*
  // 9.33: some zip archives contain incorrect value in timestamp. So we don't check it now
  UInt32 dosTime = Get32(p + 6);
  if (!CheckDosTime(dosTime))
    return k_IsArc_Res_NO;
  */
  // Crc = Get32(p + 10);
  // PackSize = Get32(p + 14);
  // Size = Get32(p + 18);
  const unsigned nameSize = Get16(p + 22);
  unsigned extraSize = Get16(p + 24);
  const UInt32 extraOffset = kLocalHeaderSize + (UInt32)nameSize;
  
  /*
  // 21.02: fixed. we don't use the following check
  if (extraOffset + extraSize > (1 << 16))
    return k_IsArc_Res_NO;
  */

  p -= 4;

  {
    size_t rem = size - kLocalHeaderSize;
    if (rem > nameSize)
      rem = nameSize;
    const Byte *p2 = p + kLocalHeaderSize;
    for (size_t i = 0; i < rem; i++)
      if (p2[i] == 0)
      {
        // we support some "bad" zip archives that contain zeros after name
        for (size_t k = i + 1; k < rem; k++)
          if (p2[k] != 0)
            return k_IsArc_Res_NO;
        break;
        /*
        if (i != nameSize - 1)
          return k_IsArc_Res_NO;
        */
      }
  }

  if (size < extraOffset)
    return k_IsArc_Res_NEED_MORE;

  if (extraSize > 0)
  {
    p += extraOffset;
    size -= extraOffset;
    while (extraSize != 0)
    {
      if (extraSize < 4)
      {
        // 7-Zip before 9.31 created incorrect WzAES Extra in folder's local headers.
        // so we return k_IsArc_Res_YES to support such archives.
        // return k_IsArc_Res_NO; // do we need to support such extra ?
        return k_IsArc_Res_YES;
      }
      if (size < 4)
        return k_IsArc_Res_NEED_MORE;
      unsigned dataSize = Get16(p + 2);
      size -= 4;
      extraSize -= 4;
      p += 4;
      if (dataSize > extraSize)
      {
        // It can be error on header.
        // We want to support such rare case bad archives.
        // We use additional checks to reduce false-positive probability.
        if (nameSize == 0
            || nameSize > (1 << 9)
            || extraSize > (1 << 9))
          return k_IsArc_Res_NO;
        return k_IsArc_Res_YES;
      }
      if (dataSize > size)
        return k_IsArc_Res_NEED_MORE;
      size -= dataSize;
      extraSize -= dataSize;
      p += dataSize;
    }
  }
  
  return k_IsArc_Res_YES;
}

static UInt32 IsArc_Zip_2(const Byte *p, size_t size, bool isFinal)
{
  UInt32 res = IsArc_Zip(p, size);
  if (res == k_IsArc_Res_NEED_MORE && isFinal)
    return k_IsArc_Res_NO;
  return res;
}


  
/* FindPK_4() is allowed to access data up to and including &limit[3].
   limit[4] access is not allowed.
  return:
    (return_ptr <  limit) : "PK" was found at (return_ptr)
    (return_ptr >= limit) : limit was reached or crossed. So no "PK" found before limit
*/
Z7_NO_INLINE
static const Byte *FindPK_4(const Byte *p, const Byte *limit)
{
  for (;;)
  {
    for (;;)
    {
      if (p >= limit)
        return limit;
      Byte b = p[1];
      if (b == 0x4B) { if (p[0] == 0x50) { return p;     } p += 1; break; }
      if (b == 0x50) { if (p[2] == 0x4B) { return p + 1; } p += 2; break; }
      b = p[3];
      p += 4;
      if (b == 0x4B) { if (p[-2]== 0x50) { return p - 2; } p -= 1; break; }
      if (b == 0x50) { if (p[0] == 0x4B) { return p - 1; }         break; }
    }
  }
  /*
  for (;;)
  {
    for (;;)
    {
      if (p >= limit)
        return limit;
      if (*p++ == 0x50) break;
      if (*p++ == 0x50) break;
      if (*p++ == 0x50) break;
      if (*p++ == 0x50) break;
    }
    if (*p == 0x4B)
      return p - 1;
  }
  */
}


/*
---------- FindMarker ----------
returns:
  S_OK:
    ArcInfo.MarkerVolIndex : volume of marker
    ArcInfo.MarkerPos   : Pos of first signature
    ArcInfo.MarkerPos2  : Pos of main signature (local item signature in most cases)
    _streamPos          : stream pos
    _cnt                : The number of virtal Bytes after start of search to offset after signature
    _signature          : main signature
 
  S_FALSE: can't find marker, or there is some non-zip data after marker

  Error code: stream reading error.
*/

HRESULT CInArchive::FindMarker(const UInt64 *searchLimit)
{
  ArcInfo.MarkerPos = GetVirtStreamPos();
  ArcInfo.MarkerPos2 = ArcInfo.MarkerPos;
  ArcInfo.MarkerVolIndex = Vols.StreamIndex;

  _cnt = 0;

  CanStartNewVol = false;

  if (searchLimit && *searchLimit == 0)
  {
    Byte startBuf[kMarkerSize];
    RINOK(ReadFromCache_FALSE(startBuf, kMarkerSize))

    UInt32 marker = Get32(startBuf);
    _signature = marker;

    if (   marker == NSignature::kNoSpan
        || marker == NSignature::kSpan)
    {
      RINOK(ReadFromCache_FALSE(startBuf, kMarkerSize))
      _signature = Get32(startBuf);
    }
      
    if (   _signature != NSignature::kEcd
        && _signature != NSignature::kEcd64
        && _signature != NSignature::kLocalFileHeader)
      return S_FALSE;

    ArcInfo.MarkerPos2 = GetVirtStreamPos() - 4;
    ArcInfo.IsSpanMode = (marker == NSignature::kSpan);

    // we use weak test in case of (*searchLimit == 0)
    // since error will be detected later in Open function
    return S_OK;
  }

  // zip specification: (_zip_header_size < (1 << 16))
  // so we need such size to check header
  const size_t kCheckSize = (size_t)1 << 16;
  const size_t kBufSize   = (size_t)1 << 17; // (kBufSize must be > kCheckSize)

  RINOK(AllocateBuffer(kBufSize))

  _inBufMode = true;

  UInt64 progressPrev = 0;

  for (;;)
  {
    RINOK(LookAhead(kBufSize))
    
    const size_t avail = GetAvail();
    
    size_t limitPos;
    // (avail > kBufSize) is possible, if (Buffer.Size() > kBufSize)
    const bool isFinished = (avail < kBufSize);
    if (isFinished)
    {
      const unsigned kMinAllowed = 4;
      if (avail <= kMinAllowed)
      {
        if (   !IsMultiVol
            || Vols.StreamIndex < 0
            || (unsigned)Vols.StreamIndex + 1 >= Vols.Streams.Size())
          break;

        SkipLookahed(avail);

        const CVols::CSubStreamInfo &s = Vols.Streams[(unsigned)Vols.StreamIndex + 1];
        if (!s.Stream)
          break;
        
        RINOK(s.SeekToStart())
        
        InitBuf();
        Vols.StreamIndex++;
        _streamPos = 0;
        Stream = s.Stream;
        continue;
      }
      limitPos = avail - kMinAllowed;
    }
    else
      limitPos = (avail - kCheckSize);

    // we don't check at (limitPos) for good fast aligned operations

    if (searchLimit)
    {
      if (_cnt > *searchLimit)
        break;
      UInt64 rem = *searchLimit - _cnt;
      if (limitPos > rem)
        limitPos = (size_t)rem + 1;
    }

    if (limitPos == 0)
      break;

    const Byte * const pStart = Buffer + _bufPos;
    const Byte * p = pStart;
    const Byte * const limit = pStart + limitPos;
   
    for (;; p++)
    {
      p = FindPK_4(p, limit);
      if (p >= limit)
        break;
      size_t rem = (size_t)(pStart + avail - p);
      /* 22.02 : we limit check size with kCheckSize to be consistent for
         any different combination of _bufPos in Buffer and size of Buffer. */
      if (rem > kCheckSize)
        rem = kCheckSize;
      const UInt32 res = IsArc_Zip_2(p, rem, isFinished);
      if (res != k_IsArc_Res_NO)
      {
        if (rem < kMarkerSize)
          return S_FALSE;
        _signature = Get32(p);
        SkipLookahed((size_t)(p - pStart));
        ArcInfo.MarkerVolIndex = Vols.StreamIndex;
        ArcInfo.MarkerPos = GetVirtStreamPos();
        ArcInfo.MarkerPos2 = ArcInfo.MarkerPos;
        SkipLookahed(4);
        if (   _signature == NSignature::kNoSpan
            || _signature == NSignature::kSpan)
        {
          if (rem < kMarkerSize * 2)
            return S_FALSE;
          ArcInfo.IsSpanMode = (_signature == NSignature::kSpan);
          _signature = Get32(p + 4);
          ArcInfo.MarkerPos2 += 4;
          SkipLookahed(4);
        }
        return S_OK;
      }
    }

    if (!IsMultiVol && isFinished)
      break;

    SkipLookahed((size_t)(p - pStart));

    if (Callback && (_cnt - progressPrev) >= ((UInt32)1 << 23))
    {
      progressPrev = _cnt;
      // const UInt64 numFiles64 = 0;
      RINOK(Callback->SetCompleted(NULL, &_cnt))
    }
  }
  
  return S_FALSE;
}


/*
---------- IncreaseRealPosition ----------
moves virtual offset in virtual stream.
changing to new volumes is allowed
*/

HRESULT CInArchive::IncreaseRealPosition(UInt64 offset, bool &isFinished)
{
  isFinished = false;

  for (;;)
  {
    const size_t avail = GetAvail();
    
    if (offset <= avail)
    {
      _bufPos += (size_t)offset;
      _cnt += offset;
      return S_OK;
    }
    
    _cnt += avail;
    offset -= avail;
    
    _bufCached = 0;
    _bufPos = 0;
    
    if (!_inBufMode)
      break;
  
    CanStartNewVol = true;
    LookAhead(1);

    if (GetAvail() == 0)
      return S_OK;
  }

  // cache is empty

  if (!IsMultiVol)
  {
    _cnt += offset;
    return Stream->Seek((Int64)offset, STREAM_SEEK_CUR, &_streamPos);
  }

  for (;;)
  {
    if (offset == 0)
      return S_OK;
    
    if (Vols.StreamIndex < 0)
      return S_FALSE;
    if ((unsigned)Vols.StreamIndex >= Vols.Streams.Size())
    {
      isFinished = true;
      return S_OK;
    }
    {
      const CVols::CSubStreamInfo &s = Vols.Streams[(unsigned)Vols.StreamIndex];
      if (!s.Stream)
      {
        isFinished = true;
        return S_OK;
      }
      if (_streamPos > s.Size)
        return S_FALSE;
      const UInt64 rem = s.Size - _streamPos;
      if ((UInt64)offset <= rem)
      {
        _cnt += offset;
        return Stream->Seek((Int64)offset, STREAM_SEEK_CUR, &_streamPos);
      }
      RINOK(Seek_SavePos(s.Size))
      offset -= rem;
      _cnt += rem;
    }
    
    Stream = NULL;
    _streamPos = 0;
    Vols.StreamIndex++;
    if ((unsigned)Vols.StreamIndex >= Vols.Streams.Size())
    {
      isFinished = true;
      return S_OK;
    }
    const CVols::CSubStreamInfo &s2 = Vols.Streams[(unsigned)Vols.StreamIndex];
    if (!s2.Stream)
    {
      isFinished = true;
      return S_OK;
    }
    Stream = s2.Stream;
    RINOK(Seek_SavePos(0))
  }
}



/*
---------- LookAhead ----------
Reads data to buffer, if required.

It can read from volumes as long as Buffer.Size().
But it moves to new volume, only if it's required to provide minRequired bytes in buffer.

in:
  (minRequired <= Buffer.Size())

return:
  S_OK : if (GetAvail() < minRequired) after function return, it's end of stream(s) data, or no new volume stream.
  Error codes: IInStream::Read() error or IInStream::Seek() error for multivol
*/

HRESULT CInArchive::LookAhead(size_t minRequired)
{
  for (;;)
  {
    const size_t avail = GetAvail();

    if (minRequired <= avail)
      return S_OK;
    
    if (_bufPos != 0)
    {
      if (avail != 0)
        memmove(Buffer, Buffer + _bufPos, avail);
      _bufPos = 0;
      _bufCached = avail;
    }

    const size_t pos = _bufCached;
    UInt32 processed = 0;
    HRESULT res = Stream->Read(Buffer + pos, (UInt32)(Buffer.Size() - pos), &processed);
    _streamPos += processed;
    _bufCached += processed;

    if (res != S_OK)
      return res;

    if (processed != 0)
      continue;

    if (   !IsMultiVol
        || !CanStartNewVol
        || Vols.StreamIndex < 0
        || (unsigned)Vols.StreamIndex + 1 >= Vols.Streams.Size())
      return S_OK;

    const CVols::CSubStreamInfo &s = Vols.Streams[(unsigned)Vols.StreamIndex + 1];
    if (!s.Stream)
      return S_OK;
    
    RINOK(s.SeekToStart())

    Vols.StreamIndex++;
    _streamPos = 0;
    Stream = s.Stream;
    // Vols.NeedSeek = false;
  }
}


class CUnexpectEnd {};


/*
---------- SafeRead ----------

reads data of exact size from stream(s)

in:
  _inBufMode
  if (CanStartNewVol) it can go to next volume before first byte reading, if there is end of volume data.

in, out:
  _streamPos  :  position in Stream
  Stream
  Vols  :  if (IsMultiVol)
  _cnt

out:
  (CanStartNewVol == false), if some data was read

return:
  S_OK : success reading of requested data

exceptions:
  CSystemException() - stream reading error
  CUnexpectEnd()  :  could not read data of requested size
*/

void CInArchive::SafeRead(Byte *data, unsigned size)
{
  unsigned processed;
  HRESULT result = ReadFromCache(data, size, processed);
  if (result != S_OK)
    throw CSystemException(result);
  if (size != processed)
    throw CUnexpectEnd();
}

void CInArchive::ReadBuffer(CByteBuffer &buffer, unsigned size)
{
  buffer.Alloc(size);
  if (size != 0)
    SafeRead(buffer, size);
}

// Byte CInArchive::ReadByte  () { Byte b;      SafeRead(&b, 1); return b; }
// UInt16 CInArchive::ReadUInt16() { Byte buf[2]; SafeRead(buf, 2); return Get16(buf); }
UInt32 CInArchive::ReadUInt32() { Byte buf[4]; SafeRead(buf, 4); return Get32(buf); }
UInt64 CInArchive::ReadUInt64() { Byte buf[8]; SafeRead(buf, 8); return Get64(buf); }

void CInArchive::ReadSignature()
{
  CanStartNewVol = true;
  _signature = ReadUInt32();
  // CanStartNewVol = false; // it's already changed in SafeRead
}


// we Skip() inside headers only, so no need for stream change in multivol.

void CInArchive::Skip(size_t num)
{
  while (num != 0)
  {
    const unsigned kBufSize = (size_t)1 << 10;
    Byte buf[kBufSize];
    unsigned step = kBufSize;
    if (step > num)
      step = (unsigned)num;
    SafeRead(buf, step);
    num -= step;
  }
}

/*
HRESULT CInArchive::Callback_Completed(unsigned numFiles)
{
  const UInt64 numFiles64 = numFiles;
  return Callback->SetCompleted(&numFiles64, &_cnt);
}
*/

HRESULT CInArchive::Skip64(UInt64 num, unsigned numFiles)
{
  if (num == 0)
    return S_OK;

  for (;;)
  {
    size_t step = (size_t)1 << 24;
    if (step > num)
      step = (size_t)num;
    Skip(step);
    num -= step;
    if (num == 0)
      return S_OK;
    if (Callback)
    {
      const UInt64 numFiles64 = numFiles;
      RINOK(Callback->SetCompleted(&numFiles64, &_cnt))
    }
  }
}


bool CInArchive::ReadFileName(unsigned size, AString &s)
{
  if (size == 0)
  {
    s.Empty();
    return true;
  }
  char *p = s.GetBuf(size);
  SafeRead((Byte *)p, size);
  unsigned i = size;
  do
  {
    if (p[i - 1] != 0)
      break;
  }
  while (--i);
  s.ReleaseBuf_CalcLen(size);
  return s.Len() == i;
}


#define ZIP64_IS_32_MAX(n) ((n) == 0xFFFFFFFF)
#define ZIP64_IS_16_MAX(n) ((n) == 0xFFFF)


bool CInArchive::ReadExtra(const CLocalItem &item, unsigned extraSize, CExtraBlock &extra,
    UInt64 &unpackSize, UInt64 &packSize,
    CItem *cdItem)
{
  extra.Clear();
  
  while (extraSize >= 4)
  {
    CExtraSubBlock subBlock;
    const UInt32 pair = ReadUInt32();
    subBlock.ID = (pair & 0xFFFF);
    unsigned size = (unsigned)(pair >> 16);
    // const unsigned origSize = size;
    
    extraSize -= 4;
    
    if (size > extraSize)
    {
      // it's error in extra
      HeadersWarning = true;
      extra.Error = true;
      Skip(extraSize);
      return false;
    }
 
    extraSize -= size;
    
    if (subBlock.ID == NFileHeader::NExtraID::kZip64)
    {
      extra.IsZip64 = true;
      bool isOK = true;

      if (!cdItem
          && size == 16
          && !ZIP64_IS_32_MAX(unpackSize)
          && !ZIP64_IS_32_MAX(packSize))
      {
        /* Win10 Explorer's "Send to Zip" for big (3500 MiB) files
           creates Zip64 Extra in local file header.
           But if both uncompressed and compressed sizes are smaller than 4 GiB,
           Win10 doesn't store 0xFFFFFFFF in 32-bit fields as expected by zip specification.
           21.04: we ignore these minor errors in Win10 zip archives. */
        if (ReadUInt64() != unpackSize)
          isOK = false;
        if (ReadUInt64() != packSize)
          isOK = false;
        size = 0;
      }
      else
      {
        if (ZIP64_IS_32_MAX(unpackSize))
          { if (size < 8) isOK = false; else { size -= 8; unpackSize = ReadUInt64(); }}
      
        if (isOK && ZIP64_IS_32_MAX(packSize))
          { if (size < 8) isOK = false; else { size -= 8; packSize = ReadUInt64(); }}
      
        if (cdItem)
        {
          if (isOK)
          {
            if (ZIP64_IS_32_MAX(cdItem->LocalHeaderPos))
              { if (size < 8) isOK = false; else { size -= 8; cdItem->LocalHeaderPos = ReadUInt64(); }}
            /*
            else if (size == 8)
            {
              size -= 8;
              const UInt64 v = ReadUInt64();
              // soong_zip, an AOSP tool (written in the Go) writes incorrect value.
              // we can ignore that minor error here
              if (v != cdItem->LocalHeaderPos)
                isOK = false; // ignore error
              // isOK = false; // force error
            }
            */
          }
         
          if (isOK && ZIP64_IS_16_MAX(cdItem->Disk))
            { if (size < 4) isOK = false; else { size -= 4; cdItem->Disk = ReadUInt32(); }}
        }
      }
    
      // we can ignore errors, when some zip archiver still write all fields to zip64 extra in local header
      // if (&& (cdItem || !isOK || origSize != 8 * 3 + 4 || size != 8 * 1 + 4))
      if (!isOK || size != 0)
      {
        HeadersWarning = true;
        extra.Error = true;
        extra.IsZip64_Error = true;
      }
      Skip(size);
    }
    else
    {
      ReadBuffer(subBlock.Data, size);
      extra.SubBlocks.Add(subBlock);
      if (subBlock.ID == NFileHeader::NExtraID::kIzUnicodeName)
      {
        if (!subBlock.CheckIzUnicode(item.Name))
          extra.Error = true;
      }
    }
  }

  if (extraSize != 0)
  {
    ExtraMinorError = true;
    extra.MinorError = true;
    // 7-Zip before 9.31 created incorrect WzAES Extra in folder's local headers.
    // so we don't return false, but just set warning flag
    // return false;
    Skip(extraSize);
  }

  return true;
}


bool CInArchive::ReadLocalItem(CItemEx &item)
{
  item.Disk = 0;
  if (IsMultiVol && Vols.StreamIndex >= 0)
    item.Disk = (UInt32)Vols.StreamIndex;
  const unsigned kPureHeaderSize = kLocalHeaderSize - 4;
  Byte p[kPureHeaderSize];
  SafeRead(p, kPureHeaderSize);
  {
    unsigned i;
    for (i = 0; i < kPureHeaderSize && p[i] == 0; i++);
    if (i == kPureHeaderSize)
      return false;
  }

  item.ExtractVersion.Version = p[0];
  item.ExtractVersion.HostOS = p[1];
  G16(2, item.Flags);
  G16(4, item.Method);
  G32(6, item.Time);
  G32(10, item.Crc);
  G32(14, item.PackSize);
  G32(18, item.Size);
  const unsigned nameSize = Get16(p + 22);
  const unsigned extraSize = Get16(p + 24);
  bool isOkName = ReadFileName(nameSize, item.Name);
  item.LocalFullHeaderSize = kLocalHeaderSize + (UInt32)nameSize + extraSize;
  item.DescriptorWasRead = false;

  /*
  if (item.IsDir())
    item.Size = 0; // check It
  */

  if (extraSize > 0)
  {
    if (!ReadExtra(item, extraSize, item.LocalExtra, item.Size, item.PackSize, NULL))
    {
      /* Most of archives are OK for Extra. But there are some rare cases
         that have error. And if error in first item, it can't open archive.
         So we ignore that error */
      // return false;
    }
  }
  
  if (!CheckDosTime(item.Time))
  {
    HeadersWarning = true;
    // return false;
  }
  
  if (item.Name.Len() != nameSize)
  {
    // we support some "bad" zip archives that contain zeros after name
    if (!isOkName)
      return false;
    HeadersWarning = true;
  }
  
  // return item.LocalFullHeaderSize <= ((UInt32)1 << 16);
  return true;
}


static bool FlagsAreSame(const CItem &i1, const CItem &i2_cd)
{
  if (i1.Method != i2_cd.Method)
    return false;
  
  UInt32 mask = i1.Flags ^ i2_cd.Flags;
  if (mask == 0)
    return true;
  switch (i1.Method)
  {
    case NFileHeader::NCompressionMethod::kDeflate:
      mask &= 0x7FF9;
      break;
    default:
      if (i1.Method <= NFileHeader::NCompressionMethod::kImplode)
        mask &= 0x7FFF;
  }

  // we can ignore utf8 flag, if name is ascii, or if only cdItem has utf8 flag
  if (mask & NFileHeader::NFlags::kUtf8)
    if ((i1.Name.IsAscii() && i2_cd.Name.IsAscii())
        || (i2_cd.Flags & NFileHeader::NFlags::kUtf8))
      mask &= ~NFileHeader::NFlags::kUtf8;

  // some bad archive in rare case can use descriptor without descriptor flag in Central Dir
  // if (i1.HasDescriptor())
  mask &= ~NFileHeader::NFlags::kDescriptorUsedMask;
  
  return (mask == 0);
}


// #ifdef _WIN32
static bool AreEqualPaths_IgnoreSlashes(const char *s1, const char *s2)
{
  for (;;)
  {
    char c1 = *s1++;
    char c2 = *s2++;
    if (c1 == c2)
    {
      if (c1 == 0)
        return true;
    }
    else
    {
      if (c1 == '\\') c1 = '/';
      if (c2 == '\\') c2 = '/';
      if (c1 != c2)
        return false;
    }
  }
}
// #endif


static bool AreItemsEqual(const CItemEx &localItem, const CItemEx &cdItem)
{
  if (!FlagsAreSame(localItem, cdItem))
    return false;
  if (!localItem.HasDescriptor())
  {
    if (cdItem.PackSize != localItem.PackSize
        || cdItem.Size != localItem.Size
        || (cdItem.Crc != localItem.Crc && cdItem.Crc != 0)) // some program writes 0 to crc field in central directory
      return false;
  }
  /* pkzip 2.50 creates incorrect archives. It uses
       - WIN encoding for name in local header
       - OEM encoding for name in central header
     We don't support these strange items. */

  /* if (cdItem.Name.Len() != localItem.Name.Len())
    return false;
  */
  if (cdItem.Name != localItem.Name)
  {
    // #ifdef _WIN32
    // some xap files use backslash in central dir items.
    // we can ignore such errors in windows, where all slashes are converted to backslashes
    unsigned hostOs = cdItem.GetHostOS();
    
    if (hostOs == NFileHeader::NHostOS::kFAT ||
        hostOs == NFileHeader::NHostOS::kNTFS)
    {
      if (!AreEqualPaths_IgnoreSlashes(cdItem.Name, localItem.Name))
      {
        // pkzip 2.50 uses DOS encoding in central dir and WIN encoding in local header.
        // so we ignore that error
        if (hostOs != NFileHeader::NHostOS::kFAT
            || cdItem.MadeByVersion.Version < 25
            || cdItem.MadeByVersion.Version > 40)
          return false;
      }
    }
    /*
    else
    #endif
      return false;
    */
  }
  return true;
}


HRESULT CInArchive::Read_LocalItem_After_CdItem(CItemEx &item, bool &isAvail, bool &headersError)
{
  isAvail = true;
  headersError = false;
  if (item.FromLocal)
    return S_OK;
  try
  {
    UInt64 offset = item.LocalHeaderPos;

    if (IsMultiVol)
    {
      if (item.Disk >= Vols.Streams.Size())
      {
        isAvail = false;
        return S_FALSE;
      }
      Stream = Vols.Streams[item.Disk].Stream;
      Vols.StreamIndex = (int)item.Disk;
      if (!Stream)
      {
        isAvail = false;
        return S_FALSE;
      }
    }
    else
    {
      if (UseDisk_in_SingleVol && item.Disk != EcdVolIndex)
      {
        isAvail = false;
        return S_FALSE;
      }
      Stream = StreamRef;

      offset = (UInt64)((Int64)offset + ArcInfo.Base);
      if (ArcInfo.Base < 0 && (Int64)offset < 0)
      {
        isAvail = false;
        return S_FALSE;
      }
    }

    _inBufMode = false;
    RINOK(Seek_SavePos(offset))
    InitBuf();
    /*
    // we can use buf mode with small buffer to reduce
    // the number of Read() calls in ReadLocalItem()
    _inBufMode = true;
    Buffer.Alloc(1 << 10);
    if (!Buffer.IsAllocated())
      return E_OUTOFMEMORY;
    */

    CItemEx localItem;
    if (ReadUInt32() != NSignature::kLocalFileHeader)
      return S_FALSE;
    ReadLocalItem(localItem);
    if (!AreItemsEqual(localItem, item))
      return S_FALSE;
    item.LocalFullHeaderSize = localItem.LocalFullHeaderSize;
    item.LocalExtra = localItem.LocalExtra;
    if (item.Crc != localItem.Crc && !localItem.HasDescriptor())
    {
      item.Crc = localItem.Crc;
      headersError = true;
    }
    if ((item.Flags ^ localItem.Flags) & NFileHeader::NFlags::kDescriptorUsedMask)
    {
      item.Flags = (UInt16)(item.Flags ^ NFileHeader::NFlags::kDescriptorUsedMask);
      headersError = true;
    }
    item.FromLocal = true;
  }
  catch(...) { return S_FALSE; }
  return S_OK;
}


/*
---------- FindDescriptor ----------

in:
  _streamPos : position in Stream
  Stream :
  Vols : if (IsMultiVol)

action:
  searches descriptor in input stream(s).
  sets
    item.DescriptorWasRead = true;
    item.Size
    item.PackSize
    item.Crc
  if descriptor was found

out:
  S_OK:
      if ( item.DescriptorWasRead) : if descriptor was found
      if (!item.DescriptorWasRead) : if descriptor was not found : unexpected end of stream(s)

  S_FALSE: if no items or there is just one item with strange properies that doesn't look like real archive.

  another error code: Callback error.

exceptions :
  CSystemException() : stream reading error
*/

HRESULT CInArchive::FindDescriptor(CItemEx &item, unsigned numFiles)
{
  // const size_t kBufSize = (size_t)1 << 5; // don't increase it too much. It reads data look ahead.

  // Buffer.Alloc(kBufSize);
  // Byte *buf = Buffer;
  
  UInt64 packedSize = 0;
  
  UInt64 progressPrev = _cnt;

  for (;;)
  {
    /* appnote specification claims that we must use 64-bit descriptor, if there is zip64 extra.
       But some old third-party xps archives used 64-bit descriptor without zip64 extra. */
    // unsigned descriptorSize = kDataDescriptorSize64 + kNextSignatureSize;
    
    // const unsigned kNextSignatureSize = 0;  // we can disable check for next signatuire
    const unsigned kNextSignatureSize = 4;  // we check also for signature for next File headear

    const unsigned descriptorSize4 = item.GetDescriptorSize() + kNextSignatureSize;

    if (descriptorSize4 > Buffer.Size()) return E_FAIL;

    // size_t processedSize;
    CanStartNewVol = true;
    RINOK(LookAhead(descriptorSize4))
    const size_t avail = GetAvail();
    
    if (avail < descriptorSize4)
    {
      // we write to packSize all these available bytes.
      // later it's simpler to work with such value than with 0
      // if (item.PackSize == 0)
        item.PackSize = packedSize + avail;
      if (item.Method == 0)
        item.Size = item.PackSize;
      SkipLookahed(avail);
      return S_OK;
    }

    const Byte * const pStart = Buffer + _bufPos;
    const Byte * p = pStart;
    const Byte * const limit = pStart + (avail - descriptorSize4);
    
    for (; p <= limit; p++)
    {
      // descriptor signature field is Info-ZIP's extension to pkware Zip specification.
      // New ZIP specification also allows descriptorSignature.
      
      p = FindPK_4(p, limit + 1);
      if (p > limit)
        break;

      /*
      if (*p != 0x50)
        continue;
      */

      if (Get32(p) != NSignature::kDataDescriptor)
        continue;

      // we check next signatuire after descriptor
      // maybe we need check only 2 bytes "PK" instead of 4 bytes, if some another type of header is possible after descriptor
      const UInt32 sig = Get32(p + descriptorSize4 - kNextSignatureSize);
      if (   sig != NSignature::kLocalFileHeader
          && sig != NSignature::kCentralFileHeader)
        continue;

      const UInt64 packSizeCur = packedSize + (size_t)(p - pStart);
      if (descriptorSize4 == kDataDescriptorSize64 + kNextSignatureSize) // if (item.LocalExtra.IsZip64)
      {
        const UInt64 descriptorPackSize = Get64(p + 8);
        if (descriptorPackSize != packSizeCur)
          continue;
        item.Size = Get64(p + 16);
      }
      else
      {
        const UInt32 descriptorPackSize = Get32(p + 8);
        if (descriptorPackSize != (UInt32)packSizeCur)
          continue;
        item.Size = Get32(p + 12);
        // that item.Size can be truncated to 32-bit value here
      }
      // We write calculated 64-bit packSize, even if descriptor64 was not used
      item.PackSize = packSizeCur;
      
      item.DescriptorWasRead = true;
      item.Crc = Get32(p + 4);

      const size_t skip = (size_t)(p - pStart) + descriptorSize4 - kNextSignatureSize;

      SkipLookahed(skip);

      return S_OK;
    }
    
    const size_t skip = (size_t)(p - pStart);
    SkipLookahed(skip);

    packedSize += skip;

    if (Callback)
    if (_cnt - progressPrev >= ((UInt32)1 << 22))
    {
      progressPrev = _cnt;
      const UInt64 numFiles64 = numFiles;
      RINOK(Callback->SetCompleted(&numFiles64, &_cnt))
    }
  }
}


HRESULT CInArchive::CheckDescriptor(const CItemEx &item)
{
  if (!item.HasDescriptor())
    return S_OK;
  
  // pkzip's version without descriptor signature is not supported
  
  bool isFinished = false;
  RINOK(IncreaseRealPosition(item.PackSize, isFinished))
  if (isFinished)
    return S_FALSE;

  /*
  if (!IsMultiVol)
  {
    RINOK(Seek_SavePos(ArcInfo.Base + item.GetDataPosition() + item.PackSize));
  }
  */
  
  Byte buf[kDataDescriptorSize64];
  try
  {
    CanStartNewVol = true;
    SafeRead(buf, item.GetDescriptorSize());
  }
  catch (const CSystemException &e) { return e.ErrorCode; }
  // catch (const CUnexpectEnd &)
  catch(...)
  {
    return S_FALSE;
  }
  // RINOK(ReadStream_FALSE(Stream, buf, item.GetDescriptorSize()));

  if (Get32(buf) != NSignature::kDataDescriptor)
    return S_FALSE;
  UInt32 crc = Get32(buf + 4);
  UInt64 packSize, unpackSize;
  
  if (item.LocalExtra.IsZip64)
  {
    packSize = Get64(buf + 8);
    unpackSize = Get64(buf + 16);
  }
  else
  {
    packSize = Get32(buf + 8);
    unpackSize = Get32(buf + 12);
  }
  
  if (crc != item.Crc || item.PackSize != packSize || item.Size != unpackSize)
    return S_FALSE;
  return S_OK;
}


HRESULT CInArchive::Read_LocalItem_After_CdItem_Full(CItemEx &item)
{
  if (item.FromLocal)
    return S_OK;
  try
  {
    bool isAvail = true;
    bool headersError = false;
    RINOK(Read_LocalItem_After_CdItem(item, isAvail, headersError))
    if (headersError)
      return S_FALSE;
    if (item.HasDescriptor())
      return CheckDescriptor(item);
  }
  catch(...) { return S_FALSE; }
  return S_OK;
}
  

HRESULT CInArchive::ReadCdItem(CItemEx &item)
{
  item.FromCentral = true;
  Byte p[kCentralHeaderSize - 4];
  SafeRead(p, kCentralHeaderSize - 4);

  item.MadeByVersion.Version = p[0];
  item.MadeByVersion.HostOS = p[1];
  item.ExtractVersion.Version = p[2];
  item.ExtractVersion.HostOS = p[3];
  G16(4, item.Flags);
  G16(6, item.Method);
  G32(8, item.Time);
  G32(12, item.Crc);
  G32(16, item.PackSize);
  G32(20, item.Size);
  const unsigned nameSize = Get16(p + 24);
  const unsigned extraSize = Get16(p + 26);
  const unsigned commentSize = Get16(p + 28);
  G16(30, item.Disk);
  G16(32, item.InternalAttrib);
  G32(34, item.ExternalAttrib);
  G32(38, item.LocalHeaderPos);
  ReadFileName(nameSize, item.Name);
  
  if (extraSize > 0)
    ReadExtra(item, extraSize, item.CentralExtra, item.Size, item.PackSize, &item);

  // May be these strings must be deleted
  /*
  if (item.IsDir())
    item.Size = 0;
  */
  
  ReadBuffer(item.Comment, commentSize);
  return S_OK;
}


/*
TryEcd64()
  (_inBufMode == false) is expected here
  so TryEcd64() can't change the Buffer.
  if (Ecd64 is not covered by cached region),
    TryEcd64() can change cached region ranges (_bufCached, _bufPos) and _streamPos.
*/

HRESULT CInArchive::TryEcd64(UInt64 offset, CCdInfo &cdInfo)
{
  if (offset >= ((UInt64)1 << 63))
    return S_FALSE;
  Byte buf[kEcd64_FullSize];

  RINOK(SeekToVol(Vols.StreamIndex, offset))
  RINOK(ReadFromCache_FALSE(buf, kEcd64_FullSize))

  if (Get32(buf) != NSignature::kEcd64)
    return S_FALSE;
  UInt64 mainSize = Get64(buf + 4);
  if (mainSize < kEcd64_MainSize || mainSize > ((UInt64)1 << 40))
    return S_FALSE;
  cdInfo.ParseEcd64e(buf + 12);
  return S_OK;
}


/* FindCd() doesn't use previous cached region,
   but it uses Buffer. So it sets new cached region */

HRESULT CInArchive::FindCd(bool checkOffsetMode)
{
  CCdInfo &cdInfo = Vols.ecd;

  UInt64 endPos;
  
  // There are no useful data in cache in most cases here.
  // So here we don't use cache data from previous operations .
  
  InitBuf();
  RINOK(InStream_GetSize_SeekToEnd(Stream, endPos))
  _streamPos = endPos;
  
  // const UInt32 kBufSizeMax2 = ((UInt32)1 << 16) + kEcdSize + kEcd64Locator_Size + kEcd64_FullSize;
  const size_t kBufSizeMax = ((size_t)1 << 17); // must be larger than kBufSizeMax2
  
  const size_t bufSize = (endPos < kBufSizeMax) ? (size_t)endPos : kBufSizeMax;
  if (bufSize < kEcdSize)
    return S_FALSE;
  // CByteArr byteBuffer(bufSize);

  RINOK(AllocateBuffer(kBufSizeMax))

  RINOK(Seek_SavePos(endPos - bufSize))

  size_t processed = bufSize;
  HRESULT res = ReadStream(Stream, Buffer, &processed);
  _streamPos += processed;
  _bufCached = processed;
  _bufPos = 0;
  _cnt += processed;
  if (res != S_OK)
    return res;
  if (processed != bufSize)
    return S_FALSE;

  
  for (size_t i = bufSize - kEcdSize + 1;;)
  {
    if (i == 0)
      return S_FALSE;
    
    const Byte *buf = Buffer;

    for (;;)
    {
      i--;
      if (buf[i] == 0x50)
        break;
      if (i == 0)
        return S_FALSE;
    }
    
    if (Get32(buf + i) != NSignature::kEcd)
      continue;

    cdInfo.ParseEcd32(buf + i);
    
    if (i >= kEcd64Locator_Size)
    {
      const size_t locatorIndex = i - kEcd64Locator_Size;
      if (Get32(buf + locatorIndex) == NSignature::kEcd64Locator)
      {
        CLocator locator;
        locator.Parse(buf + locatorIndex + 4);
        UInt32 numDisks = locator.NumDisks;
        // we ignore the error, where some zip creators use (NumDisks == 0)
        if (numDisks == 0)
          numDisks = 1;
        if ((cdInfo.ThisDisk == numDisks - 1 || ZIP64_IS_16_MAX(cdInfo.ThisDisk))
            && locator.Ecd64Disk < numDisks)
        {
          if (locator.Ecd64Disk != cdInfo.ThisDisk && !ZIP64_IS_16_MAX(cdInfo.ThisDisk))
            return E_NOTIMPL;
          
          // Most of the zip64 use fixed size Zip64 ECD
          // we try relative backward reading.

          UInt64 absEcd64 = endPos - bufSize + i - (kEcd64Locator_Size + kEcd64_FullSize);
          
          if (locatorIndex >= kEcd64_FullSize)
          if (checkOffsetMode || absEcd64 == locator.Ecd64Offset)
          {
            const Byte *ecd64 = buf + locatorIndex - kEcd64_FullSize;
            if (Get32(ecd64) == NSignature::kEcd64)
            {
              UInt64 mainEcd64Size = Get64(ecd64 + 4);
              if (mainEcd64Size == kEcd64_MainSize)
              {
                cdInfo.ParseEcd64e(ecd64 + 12);
                ArcInfo.Base = (Int64)(absEcd64 - locator.Ecd64Offset);
                // ArcInfo.BaseVolIndex = cdInfo.ThisDisk;
                return S_OK;
              }
            }
          }
          
          // some zip64 use variable size Zip64 ECD.
          // we try to use absolute offset from locator.

          if (absEcd64 != locator.Ecd64Offset)
          {
            if (TryEcd64(locator.Ecd64Offset, cdInfo) == S_OK)
            {
              ArcInfo.Base = 0;
              // ArcInfo.BaseVolIndex = cdInfo.ThisDisk;
              return S_OK;
            }
          }
          
          // for variable Zip64 ECD with for archives with offset != 0.

          if (checkOffsetMode
              && ArcInfo.MarkerPos != 0
              && ArcInfo.MarkerPos + locator.Ecd64Offset != absEcd64)
          {
            if (TryEcd64(ArcInfo.MarkerPos + locator.Ecd64Offset, cdInfo) == S_OK)
            {
              ArcInfo.Base = (Int64)ArcInfo.MarkerPos;
              // ArcInfo.BaseVolIndex = cdInfo.ThisDisk;
              return S_OK;
            }
          }
        }
      }
    }
    
    // bool isVolMode = (Vols.EndVolIndex != -1);
    // UInt32 searchDisk = (isVolMode ? Vols.EndVolIndex : 0);
    
    if (/* searchDisk == thisDisk && */ cdInfo.CdDisk <= cdInfo.ThisDisk)
    {
      // if (isVolMode)
      {
        if (cdInfo.CdDisk != cdInfo.ThisDisk)
          return S_OK;
      }
      
      UInt64 absEcdPos = endPos - bufSize + i;
      UInt64 cdEnd = cdInfo.Size + cdInfo.Offset;
      ArcInfo.Base = 0;
      // ArcInfo.BaseVolIndex = cdInfo.ThisDisk;
      if (absEcdPos != cdEnd)
      {
        /*
        if (cdInfo.Offset <= 16 && cdInfo.Size != 0)
        {
          // here we support some rare ZIP files with Central directory at the start
          ArcInfo.Base = 0;
        }
        else
        */
        ArcInfo.Base = (Int64)(absEcdPos - cdEnd);
      }
      return S_OK;
    }
  }
}


HRESULT CInArchive::TryReadCd(CObjectVector<CItemEx> &items, const CCdInfo &cdInfo, UInt64 cdOffset, UInt64 cdSize)
{
  items.Clear();
  IsCdUnsorted = false;
  
  // _startLocalFromCd_Disk = (UInt32)(Int32)-1;
  // _startLocalFromCd_Offset = (UInt64)(Int64)-1;

  RINOK(SeekToVol(IsMultiVol ? (int)cdInfo.CdDisk : -1, cdOffset))

  _inBufMode = true;
  _cnt = 0;

  if (Callback)
  {
    RINOK(Callback->SetTotal(&cdInfo.NumEntries, IsMultiVol ? &Vols.TotalBytesSize : NULL))
  }
  UInt64 numFileExpected = cdInfo.NumEntries;
  const UInt64 *totalFilesPtr = &numFileExpected;
  bool isCorrect_NumEntries = (cdInfo.IsFromEcd64 || numFileExpected >= ((UInt32)1 << 16));

  while (_cnt < cdSize)
  {
    CanStartNewVol = true;
    if (ReadUInt32() != NSignature::kCentralFileHeader)
      return S_FALSE;
    CanStartNewVol = false;
    {
      CItemEx cdItem;
      RINOK(ReadCdItem(cdItem))
      
      /*
      if (cdItem.Disk < _startLocalFromCd_Disk ||
          cdItem.Disk == _startLocalFromCd_Disk &&
          cdItem.LocalHeaderPos < _startLocalFromCd_Offset)
      {
        _startLocalFromCd_Disk = cdItem.Disk;
        _startLocalFromCd_Offset = cdItem.LocalHeaderPos;
      }
      */

      if (items.Size() > 0 && !IsCdUnsorted)
      {
        const CItemEx &prev = items.Back();
        if (cdItem.Disk < prev.Disk
            || (cdItem.Disk == prev.Disk &&
            cdItem.LocalHeaderPos < prev.LocalHeaderPos))
          IsCdUnsorted = true;
      }

      items.Add(cdItem);
    }
    if (Callback && (items.Size() & 0xFFF) == 0)
    {
      const UInt64 numFiles = items.Size();

      if (numFiles > numFileExpected && totalFilesPtr)
      {
        if (isCorrect_NumEntries)
          totalFilesPtr = NULL;
        else
          while (numFiles > numFileExpected)
            numFileExpected += (UInt32)1 << 16;
        RINOK(Callback->SetTotal(totalFilesPtr, NULL))
      }

      RINOK(Callback->SetCompleted(&numFiles, &_cnt))
    }
  }

  CanStartNewVol = true;

  return (_cnt == cdSize) ? S_OK : S_FALSE;
}


/*
static int CompareCdItems(void *const *elem1, void *const *elem2, void *)
{
  const CItemEx *i1 = *(const CItemEx **)elem1;
  const CItemEx *i2 = *(const CItemEx **)elem2;

  if (i1->Disk < i2->Disk) return -1;
  if (i1->Disk > i2->Disk) return 1;
  if (i1->LocalHeaderPos < i2->LocalHeaderPos) return -1;
  if (i1->LocalHeaderPos > i2->LocalHeaderPos) return 1;
  if (i1 < i2) return -1;
  if (i1 > i2) return 1;
  return 0;
}
*/

HRESULT CInArchive::ReadCd(CObjectVector<CItemEx> &items, UInt32 &cdDisk, UInt64 &cdOffset, UInt64 &cdSize)
{
  bool checkOffsetMode = true;
  
  if (IsMultiVol)
  {
    if (Vols.EndVolIndex == -1)
      return S_FALSE;
    Stream = Vols.Streams[(unsigned)Vols.EndVolIndex].Stream;
    if (!Vols.StartIsZip)
      checkOffsetMode = false;
  }
  else
    Stream = StartStream;

  if (!Vols.ecd_wasRead)
  {
    RINOK(FindCd(checkOffsetMode))
  }

  CCdInfo &cdInfo = Vols.ecd;
  
  HRESULT res = S_FALSE;
  
  cdSize = cdInfo.Size;
  cdOffset = cdInfo.Offset;
  cdDisk = cdInfo.CdDisk;

  if (!IsMultiVol)
  {
    if (cdInfo.ThisDisk != cdInfo.CdDisk)
      return S_FALSE;
  }

  const UInt64 base = (IsMultiVol ? 0 : (UInt64)ArcInfo.Base);
  res = TryReadCd(items, cdInfo, base + cdOffset, cdSize);
  
  if (res == S_FALSE && !IsMultiVol && base != ArcInfo.MarkerPos)
  {
    // do we need that additional attempt to read cd?
    res = TryReadCd(items, cdInfo, ArcInfo.MarkerPos + cdOffset, cdSize);
    if (res == S_OK)
      ArcInfo.Base = (Int64)ArcInfo.MarkerPos;
  }
  
  // Some rare case files are unsorted
  // items.Sort(CompareCdItems, NULL);
  return res;
}


static int FindItem(const CObjectVector<CItemEx> &items, const CItemEx &item)
{
  unsigned left = 0, right = items.Size();
  for (;;)
  {
    if (left >= right)
      return -1;
    const unsigned index = (unsigned)(((size_t)left + (size_t)right) / 2);
    const CItemEx &item2 = items[index];
    if (item.Disk < item2.Disk)
      right = index;
    else if (item.Disk > item2.Disk)
      left = index + 1;
    else if (item.LocalHeaderPos == item2.LocalHeaderPos)
      return (int)index;
    else if (item.LocalHeaderPos < item2.LocalHeaderPos)
      right = index;
    else
      left = index + 1;
  }
}

static bool IsStrangeItem(const CItem &item)
{
  return item.Name.Len() > (1 << 14) || item.Method > (1 << 8);
}



/*
  ---------- ReadLocals ----------

in:
  (_signature == NSignature::kLocalFileHeader)
  VirtStreamPos : after _signature : position in Stream
  Stream :
  Vols : if (IsMultiVol)
  (_inBufMode == false)

action:
  it parses local items.

  if ( IsMultiVol) it writes absolute offsets to CItemEx::LocalHeaderPos
  if (!IsMultiVol) it writes relative (from ArcInfo.Base) offsets to CItemEx::LocalHeaderPos
               later we can correct CItemEx::LocalHeaderPos values, if
               some new value for ArcInfo.Base will be detected
out:
  S_OK:
    (_signature != NSignature::kLocalFileHeade)
    _streamPos : after _signature

  S_FALSE: if no items or there is just one item with strange properies that doesn't look like real archive.

  another error code: stream reading error or Callback error.

  CUnexpectEnd() exception : it's not fatal exception here.
      It means that reading was interrupted by unexpected end of input stream,
      but some CItemEx items were parsed OK.
      We can stop further archive parsing.
      But we can use all filled CItemEx items.
*/

HRESULT CInArchive::ReadLocals(CObjectVector<CItemEx> &items)
{
  items.Clear();

  UInt64 progressPrev = _cnt;
  
  if (Callback)
  {
    RINOK(Callback->SetTotal(NULL, IsMultiVol ? &Vols.TotalBytesSize : NULL))
  }

  while (_signature == NSignature::kLocalFileHeader)
  {
    CItemEx item;

    item.LocalHeaderPos = GetVirtStreamPos() - 4;
    if (!IsMultiVol)
      item.LocalHeaderPos = (UInt64)((Int64)item.LocalHeaderPos - ArcInfo.Base);
    
    try
    {
      ReadLocalItem(item);
      item.FromLocal = true;
      bool isFinished = false;

      if (item.HasDescriptor())
      {
        RINOK(FindDescriptor(item, items.Size()))
        isFinished = !item.DescriptorWasRead;
      }
      else
      {
        if (item.PackSize >= ((UInt64)1 << 62))
          throw CUnexpectEnd();
        RINOK(IncreaseRealPosition(item.PackSize, isFinished))
      }
   
      items.Add(item);
      
      if (isFinished)
        throw CUnexpectEnd();

      ReadSignature();
    }
    catch (CUnexpectEnd &)
    {
      if (items.IsEmpty() || (items.Size() == 1 && IsStrangeItem(items[0])))
        return S_FALSE;
      throw;
    }


    if (Callback)
    if ((items.Size() & 0xFF) == 0
        || _cnt - progressPrev >= ((UInt32)1 << 22))
    {
      progressPrev = _cnt;
      const UInt64 numFiles = items.Size();
      RINOK(Callback->SetCompleted(&numFiles, &_cnt))
    }
  }

  if (items.Size() == 1 && _signature != NSignature::kCentralFileHeader)
    if (IsStrangeItem(items[0]))
      return S_FALSE;
  
  return S_OK;
}



HRESULT CVols::ParseArcName(IArchiveOpenVolumeCallback *volCallback)
{
  UString name;
  {
    NWindows::NCOM::CPropVariant prop;
    RINOK(volCallback->GetProperty(kpidName, &prop))
    if (prop.vt != VT_BSTR)
      return S_OK;
    name = prop.bstrVal;
  }

  const int dotPos = name.ReverseFind_Dot();
  if (dotPos < 0)
    return S_OK;
  const UString ext = name.Ptr((unsigned)(dotPos + 1));
  name.DeleteFrom((unsigned)(dotPos + 1));

  StartVolIndex = (Int32)(-1);

  if (ext.IsEmpty())
    return S_OK;
  {
    wchar_t c = ext[0];
    IsUpperCase = (c >= 'A' && c <= 'Z');
    if (ext.IsEqualTo_Ascii_NoCase("zip"))
    {
      BaseName = name;
      StartIsZ = true;
      StartIsZip = true;
      return S_OK;
    }
    else if (ext.IsEqualTo_Ascii_NoCase("exe"))
    {
      /* possible cases:
         - exe with zip inside
         - sfx: a.exe, a.z02, a.z03,... , a.zip
                a.exe is start volume.
         - zip renamed to exe
      */

      StartIsExe = true;
      BaseName = name;
      StartVolIndex = 0;
      /* sfx-zip can use both arc.exe and arc.zip
         We can open arc.zip, if it was requesed to open arc.exe.
         But it's possible that arc.exe and arc.zip are not parts of same archive.
         So we can disable such operation */

      // 18.04: we still want to open zip renamed to exe.
      /*
      {
        UString volName = name;
        volName += IsUpperCase ? "Z01" : "z01";
        {
          CMyComPtr<IInStream> stream;
          HRESULT res2 = volCallback->GetStream(volName, &stream);
          if (res2 == S_OK)
            DisableVolsSearch = true;
        }
      }
      */
      DisableVolsSearch = true;
      return S_OK;
    }
    else if (ext[0] == 'z' || ext[0] == 'Z')
    {
      if (ext.Len() < 3)
        return S_OK;
      const wchar_t *end = NULL;
      UInt32 volNum = ConvertStringToUInt32(ext.Ptr(1), &end);
      if (*end != 0 || volNum < 1 || volNum > ((UInt32)1 << 30))
        return S_OK;
      StartVolIndex = (Int32)(volNum - 1);
      BaseName = name;
      StartIsZ = true;
    }
    else
      return S_OK;
  }

  UString volName = BaseName;
  volName += (IsUpperCase ? "ZIP" : "zip");
  
  HRESULT res = volCallback->GetStream(volName, &ZipStream);
  
  if (res == S_FALSE || !ZipStream)
  {
    if (MissingName.IsEmpty())
    {
      MissingZip = true;
      MissingName = volName;
    }
    return S_OK;
  }

  return res;
}


HRESULT CInArchive::ReadVols2(IArchiveOpenVolumeCallback *volCallback,
    unsigned start, int lastDisk, int zipDisk, unsigned numMissingVolsMax, unsigned &numMissingVols)
{
  if (Vols.DisableVolsSearch)
    return S_OK;

  numMissingVols = 0;

  for (unsigned i = start;; i++)
  {
    if (lastDisk >= 0 && i >= (unsigned)lastDisk)
      break;
    
    if (i < Vols.Streams.Size())
      if (Vols.Streams[i].Stream)
        continue;

    CMyComPtr<IInStream> stream;

    if ((int)i == zipDisk)
    {
      stream = Vols.ZipStream;
    }
    else if ((int)i == Vols.StartVolIndex)
    {
      stream = StartStream;
    }
    else
    {
      UString volName = Vols.BaseName;
      {
        volName += (char)(Vols.IsUpperCase ? 'Z' : 'z');
        unsigned v = i + 1;
        if (v < 10)
          volName += '0';
        volName.Add_UInt32(v);
      }
        
      HRESULT res = volCallback->GetStream(volName, &stream);
      if (res != S_OK && res != S_FALSE)
        return res;
      if (res == S_FALSE || !stream)
      {
        if (i == 0)
        {
          UString volName_exe = Vols.BaseName;
          volName_exe += (Vols.IsUpperCase ? "EXE" : "exe");
          
          HRESULT res2 = volCallback->GetStream(volName_exe, &stream);
          if (res2 != S_OK && res2 != S_FALSE)
            return res2;
          res = res2;
        }
      }
      if (res == S_FALSE || !stream)
      {
        if (i == 1 && Vols.StartIsExe)
          return S_OK;
        if (Vols.MissingName.IsEmpty())
          Vols.MissingName = volName;
        numMissingVols++;
        if (numMissingVols > numMissingVolsMax)
          return S_OK;
        if (lastDisk == -1 && numMissingVols != 0)
          return S_OK;
        continue;
      }
    }

    UInt64 pos, size;
    RINOK(InStream_GetPos_GetSize(stream, pos, size))

    while (i >= Vols.Streams.Size())
      Vols.Streams.AddNew();
    
    CVols::CSubStreamInfo &ss = Vols.Streams[i];
    Vols.NumVols++;
    Vols.TotalBytesSize += size;

    ss.Stream = stream;
    ss.Size = size;

    if ((int)i == zipDisk)
    {
      Vols.EndVolIndex = (int)(Vols.Streams.Size() - 1);
      break;
    }
  }

  return S_OK;
}


HRESULT CInArchive::ReadVols()
{
  CMyComPtr<IArchiveOpenVolumeCallback> volCallback;

  Callback->QueryInterface(IID_IArchiveOpenVolumeCallback, (void **)&volCallback);
  if (!volCallback)
    return S_OK;

  RINOK(Vols.ParseArcName(volCallback))

  // const int startZIndex = Vols.StartVolIndex;

  if (!Vols.StartIsZ)
  {
    if (!Vols.StartIsExe)
      return S_OK;
  }

  int zipDisk = -1;
  int cdDisk = -1;

  if (Vols.StartIsZip)
    Vols.ZipStream = StartStream;

  if (Vols.ZipStream)
  {
    Stream = Vols.ZipStream;
    
    if (Vols.StartIsZip)
      Vols.StreamIndex = -1;
    else
    {
      Vols.StreamIndex = -2;
      InitBuf();
    }

    HRESULT res = FindCd(true);

    CCdInfo &ecd = Vols.ecd;
    if (res == S_OK)
    {
      zipDisk = (int)ecd.ThisDisk;
      Vols.ecd_wasRead = true;

      // if is not multivol or bad multivol, we return to main single stream code
      if (ecd.ThisDisk == 0
          || ecd.ThisDisk >= ((UInt32)1 << 30)
          || ecd.ThisDisk < ecd.CdDisk)
        return S_OK;
      
      cdDisk = (int)ecd.CdDisk;
      if (Vols.StartVolIndex < 0)
        Vols.StartVolIndex = (Int32)ecd.ThisDisk;
      else if ((UInt32)Vols.StartVolIndex >= ecd.ThisDisk)
        return S_OK;

      // Vols.StartVolIndex = ecd.ThisDisk;
      // Vols.EndVolIndex = ecd.ThisDisk;
      unsigned numMissingVols;
      if (cdDisk != zipDisk)
      {
        // get volumes required for cd.
        RINOK(ReadVols2(volCallback, (unsigned)cdDisk, zipDisk, zipDisk, 0, numMissingVols))
        if (numMissingVols != 0)
        {
          // cdOK = false;
        }
      }
    }
    else if (res != S_FALSE)
      return res;
  }

  if (Vols.StartVolIndex < 0)
  {
    // is not mutivol;
    return S_OK;
  }

  /*
  if (!Vols.Streams.IsEmpty())
    IsMultiVol = true;
  */
  
  unsigned numMissingVols;

  if (cdDisk != 0)
  {
    // get volumes that were no requested still
    const unsigned kNumMissingVolsMax = 1 << 12;
    RINOK(ReadVols2(volCallback, 0, cdDisk < 0 ? -1 : cdDisk, zipDisk, kNumMissingVolsMax, numMissingVols))
  }

  // if (Vols.StartVolIndex >= 0)
  {
    if (Vols.Streams.IsEmpty())
      if (Vols.StartVolIndex > (1 << 20))
        return S_OK;
    if ((unsigned)Vols.StartVolIndex >= Vols.Streams.Size()
        || !Vols.Streams[(unsigned)Vols.StartVolIndex].Stream)
    {
      // we get volumes starting from StartVolIndex, if they we not requested before know the volume index (if FindCd() was ok)
      RINOK(ReadVols2(volCallback, (unsigned)Vols.StartVolIndex, zipDisk, zipDisk, 0, numMissingVols))
    }
  }

  if (Vols.ZipStream)
  {
    // if there is no another volumes and volumeIndex is too big, we don't use multivol mode
    if (Vols.Streams.IsEmpty())
      if (zipDisk > (1 << 10))
        return S_OK;
    if (zipDisk >= 0)
    {
      // we create item in Streams for ZipStream, if we know the volume index (if FindCd() was ok)
      RINOK(ReadVols2(volCallback, (unsigned)zipDisk, zipDisk + 1, zipDisk, 0, numMissingVols))
    }
  }

  if (!Vols.Streams.IsEmpty())
  {
    IsMultiVol = true;
    /*
    if (cdDisk)
      IsMultiVol = true;
    */
    const int startZIndex = Vols.StartVolIndex;
    if (startZIndex >= 0)
    {
      // if all volumes before start volume are OK, we can start parsing from 0
      // if there are missing volumes before startZIndex, we start parsing in current startZIndex
      if ((unsigned)startZIndex < Vols.Streams.Size())
      {
        for (unsigned i = 0; i <= (unsigned)startZIndex; i++)
          if (!Vols.Streams[i].Stream)
          {
            Vols.StartParsingVol = startZIndex;
            break;
          }
      }
    }
  }

  return S_OK;
}



HRESULT CVols::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  if (processedSize)
    *processedSize = 0;
  if (size == 0)
    return S_OK;

  for (;;)
  {
    if (StreamIndex < 0)
      return S_OK;
    if ((unsigned)StreamIndex >= Streams.Size())
      return S_OK;
    const CVols::CSubStreamInfo &s = Streams[(unsigned)StreamIndex];
    if (!s.Stream)
      return S_FALSE;
    if (NeedSeek)
    {
      RINOK(s.SeekToStart())
      NeedSeek = false;
    }
    UInt32 realProcessedSize = 0;
    HRESULT res = s.Stream->Read(data, size, &realProcessedSize);
    if (processedSize)
      *processedSize = realProcessedSize;
    if (res != S_OK)
      return res;
    if (realProcessedSize != 0)
      return res;
    StreamIndex++;
    NeedSeek = true;
  }
}

Z7_COM7F_IMF(CVolStream::Read(void *data, UInt32 size, UInt32 *processedSize))
{
  return Vols->Read(data, size, processedSize);
}




#define COPY_ECD_ITEM_16(n) if (!isZip64 || !ZIP64_IS_16_MAX(ecd. n))     cdInfo. n = ecd. n;
#define COPY_ECD_ITEM_32(n) if (!isZip64 || !ZIP64_IS_32_MAX(ecd. n)) cdInfo. n = ecd. n;


HRESULT CInArchive::ReadHeaders(CObjectVector<CItemEx> &items)
{
  // buffer that can be used for cd reading
  RINOK(AllocateBuffer(kSeqBufferSize))

  // here we can read small records. So we switch off _inBufMode.
  _inBufMode = false;

  HRESULT res = S_OK;

  bool localsWereRead = false;

  /* we try to open archive with the following modes:
     1) CD-MODE        : fast mode : we read backward ECD and CD, compare CD items with first Local item.
     2) LOCALS-CD-MODE : slow mode, if CD-MODE fails : we sequentially read all Locals and then CD.
     Then we read sequentially ECD64, Locator, ECD again at the end.

     - in LOCALS-CD-MODE we use use the following
         variables (with real cd properties) to set Base archive offset
         and check real cd properties with values from ECD/ECD64.
  */

  UInt64 cdSize = 0;
  UInt64 cdRelatOffset = 0;
  UInt32 cdDisk = 0;

  UInt64 cdAbsOffset = 0;   // absolute cd offset, for LOCALS-CD-MODE only.

if (Force_ReadLocals_Mode)
{
  IsArc = true;
  res = S_FALSE; // we will use LOCALS-CD-MODE mode
}
else
{
  if (!MarkerIsFound || !MarkerIsSafe)
  {
    IsArc = true;
    res = ReadCd(items, cdDisk, cdRelatOffset, cdSize);
    if (res == S_OK)
      ReadSignature();
    else if (res != S_FALSE)
      return res;
  }
  else  // (MarkerIsFound && MarkerIsSafe)
  {
 
  // _signature must be kLocalFileHeader or kEcd or kEcd64

  SeekToVol(ArcInfo.MarkerVolIndex, ArcInfo.MarkerPos2 + 4);

  CanStartNewVol = false;

  if (_signature == NSignature::kEcd64)
  {
    // UInt64 ecd64Offset = GetVirtStreamPos() - 4;
    IsZip64 = true;

    {
      const UInt64 recordSize = ReadUInt64();
      if (recordSize < kEcd64_MainSize)
        return S_FALSE;
      if (recordSize >= ((UInt64)1 << 62))
        return S_FALSE;
      
      {
        const unsigned kBufSize = kEcd64_MainSize;
        Byte buf[kBufSize];
        SafeRead(buf, kBufSize);
        CCdInfo cdInfo;
        cdInfo.ParseEcd64e(buf);
        if (!cdInfo.IsEmptyArc())
          return S_FALSE;
      }
      
      RINOK(Skip64(recordSize - kEcd64_MainSize, 0))
    }

    ReadSignature();
    if (_signature != NSignature::kEcd64Locator)
      return S_FALSE;

    {
      const unsigned kBufSize = 16;
      Byte buf[kBufSize];
      SafeRead(buf, kBufSize);
      CLocator locator;
      locator.Parse(buf);
      if (!locator.IsEmptyArc())
        return S_FALSE;
    }

    ReadSignature();
    if (_signature != NSignature::kEcd)
      return S_FALSE;
  }
  
  if (_signature == NSignature::kEcd)
  {
    // It must be empty archive or backware archive
    // we don't support backware archive still
    
    const unsigned kBufSize = kEcdSize - 4;
    Byte buf[kBufSize];
    SafeRead(buf, kBufSize);
    CEcd ecd;
    ecd.Parse(buf);
    // if (ecd.cdSize != 0)
    // Do we need also to support the case where empty zip archive with PK00 uses cdOffset = 4 ??
    if (!ecd.IsEmptyArc())
      return S_FALSE;

    ArcInfo.Base = (Int64)ArcInfo.MarkerPos;
    IsArc = true; // check it: we need more tests?

    RINOK(SeekToVol(ArcInfo.MarkerVolIndex, ArcInfo.MarkerPos2))
    ReadSignature();
  }
  else
  {
    CItemEx firstItem;
    try
    {
      try
      {
        if (!ReadLocalItem(firstItem))
          return S_FALSE;
      }
      catch(CUnexpectEnd &)
      {
        return S_FALSE;
      }

      IsArc = true;
      res = ReadCd(items, cdDisk, cdRelatOffset, cdSize);
      if (res == S_OK)
        ReadSignature();
    }
    catch(CUnexpectEnd &) { res = S_FALSE; }
    
    if (res != S_FALSE && res != S_OK)
      return res;

    if (res == S_OK && items.Size() == 0)
      res = S_FALSE;

    if (res == S_OK)
    {
      // we can't read local items here to keep _inBufMode state
      if ((Int64)ArcInfo.MarkerPos2 < ArcInfo.Base)
        res = S_FALSE;
      else
      {
        firstItem.LocalHeaderPos = (UInt64)((Int64)ArcInfo.MarkerPos2 - ArcInfo.Base);
        int index = -1;

        UInt32 min_Disk = (UInt32)(Int32)-1;
        UInt64 min_LocalHeaderPos = (UInt64)(Int64)-1;

        if (!IsCdUnsorted)
          index = FindItem(items, firstItem);
        else
        {
          FOR_VECTOR (i, items)
          {
            const CItemEx &cdItem = items[i];
            if (cdItem.Disk == firstItem.Disk
                && (cdItem.LocalHeaderPos == firstItem.LocalHeaderPos))
              index = (int)i;
            
            if (i == 0
                || cdItem.Disk < min_Disk
                || (cdItem.Disk == min_Disk && cdItem.LocalHeaderPos < min_LocalHeaderPos))
            {
              min_Disk = cdItem.Disk;
              min_LocalHeaderPos = cdItem.LocalHeaderPos;
            }
          }
        }

        if (index == -1)
          res = S_FALSE;
        else if (!AreItemsEqual(firstItem, items[(unsigned)index]))
          res = S_FALSE;
        else
        {
          ArcInfo.CdWasRead = true;
          if (IsCdUnsorted)
            ArcInfo.FirstItemRelatOffset = min_LocalHeaderPos;
          else
            ArcInfo.FirstItemRelatOffset = items[0].LocalHeaderPos;

          // ArcInfo.FirstItemRelatOffset = _startLocalFromCd_Offset;
        }
      }
    }
  }
  } // (MarkerIsFound && MarkerIsSafe)

} // (!onlyLocalsMode)


  CObjectVector<CItemEx> cdItems;

  bool needSetBase = false; // we set needSetBase only for LOCALS_CD_MODE
  unsigned numCdItems = items.Size();
  
  #ifdef ZIP_SELF_CHECK
  res = S_FALSE; // if uncommented, it uses additional LOCALS-CD-MODE mode to check the code
  #endif

  if (res != S_OK)
  {
    // ---------- LOCALS-CD-MODE ----------
    // CD doesn't match firstItem,
    // so we clear items and read Locals and CD.

    items.Clear();
    localsWereRead = true;
    
    HeadersError = false;
    HeadersWarning = false;
    ExtraMinorError = false;

    /* we can use any mode: with buffer and without buffer
         without buffer : skips packed data : fast for big files : slow for small files
         with    buffer : reads packed data : slow for big files : fast for small files
       Buffer mode is more effective. */
    // _inBufMode = false;
    _inBufMode = true;
    // we could change the buffer size here, if we want smaller Buffer.
    // RINOK(ReAllocateBuffer(kSeqBufferSize));
    // InitBuf()
    
    ArcInfo.Base = 0;

   if (!Disable_FindMarker)
   {
    if (!MarkerIsFound)
    {
      if (!IsMultiVol)
        return S_FALSE;
      if (Vols.StartParsingVol != 0)
        return S_FALSE;
      // if (StartParsingVol == 0) and we didn't find marker, we use default zero marker.
      // so we suppose that there is no sfx stub
      RINOK(SeekToVol(0, ArcInfo.MarkerPos2))
    }
    else
    {
      if (ArcInfo.MarkerPos != 0)
      {
        /*
        If multi-vol or there is (No)Span-marker at start of stream, we set (Base) as 0.
        In another caes:
          (No)Span-marker is supposed as false positive. So we set (Base) as main marker (MarkerPos2).
          The (Base) can be corrected later after ECD reading.
          But sfx volume with stub and (No)Span-marker in (!IsMultiVol) mode will have incorrect (Base) here.
        */
        ArcInfo.Base = (Int64)ArcInfo.MarkerPos2;
      }
      RINOK(SeekToVol(ArcInfo.MarkerVolIndex, ArcInfo.MarkerPos2))
    }
   }
    _cnt = 0;

    ReadSignature();
    
    LocalsWereRead = true;

    RINOK(ReadLocals(items))

    if (_signature != NSignature::kCentralFileHeader)
    {
      // GetVirtStreamPos() - 4
      if (items.IsEmpty())
        return S_FALSE;

      bool isError = true;

      const UInt32 apkSize = _signature;
      const unsigned kApkFooterSize = 16 + 8;
      if (apkSize >= kApkFooterSize && apkSize <= (1 << 20))
      {
        if (ReadUInt32() == 0)
        {
          CByteBuffer apk;
          apk.Alloc(apkSize);
          SafeRead(apk, apkSize);
          ReadSignature();
          const Byte *footer = apk + apkSize - kApkFooterSize;
          if (_signature == NSignature::kCentralFileHeader)
          if (GetUi64(footer) == apkSize)
          if (memcmp(footer + 8, "APK Sig Block 42", 16) == 0)
          {
            isError = false;
            IsApk = true;
          }
        }
      }
      
      if (isError)
      {
        NoCentralDir = true;
        HeadersError = true;
        return S_OK;
      }
    }
    
    _inBufMode = true;

    cdAbsOffset = GetVirtStreamPos() - 4;
    cdDisk = (UInt32)Vols.StreamIndex;

    #ifdef ZIP_SELF_CHECK
    if (!IsMultiVol && _cnt != GetVirtStreamPos() - ArcInfo.MarkerPos2)
      return E_FAIL;
    #endif

    const UInt64 processedCnt_start = _cnt;

    for (;;)
    {
      CItemEx cdItem;
      
      RINOK(ReadCdItem(cdItem))
      
      cdItems.Add(cdItem);
      if (Callback && (cdItems.Size() & 0xFFF) == 0)
      {
        const UInt64 numFiles = items.Size();
        const UInt64 numBytes = _cnt;
        RINOK(Callback->SetCompleted(&numFiles, &numBytes))
      }
      ReadSignature();
      if (_signature != NSignature::kCentralFileHeader)
        break;
    }
    
    cdSize = _cnt - processedCnt_start;

    #ifdef ZIP_SELF_CHECK
    if (!IsMultiVol)
    {
      if (_cnt != GetVirtStreamPos() - ArcInfo.MarkerPos2)
        return E_FAIL;
      if (cdSize != (GetVirtStreamPos() - 4) - cdAbsOffset)
        return E_FAIL;
    }
    #endif

    needSetBase = true;
    numCdItems = cdItems.Size();
    cdRelatOffset = (UInt64)((Int64)cdAbsOffset - ArcInfo.Base);

    if (!cdItems.IsEmpty())
    {
      ArcInfo.CdWasRead = true;
      ArcInfo.FirstItemRelatOffset = cdItems[0].LocalHeaderPos;
    }
  }

  
  
  CCdInfo cdInfo;
  CLocator locator;
  bool isZip64 = false;
  const UInt64 ecd64AbsOffset = GetVirtStreamPos() - 4;
  int ecd64Disk = -1;
  
  if (_signature == NSignature::kEcd64)
  {
    ecd64Disk = Vols.StreamIndex;

    IsZip64 = isZip64 = true;

    {
      const UInt64 recordSize = ReadUInt64();
      if (recordSize < kEcd64_MainSize
          || recordSize >= ((UInt64)1 << 62))
      {
        HeadersError = true;
        return S_OK;
      }

      {
        const unsigned kBufSize = kEcd64_MainSize;
        Byte buf[kBufSize];
        SafeRead(buf, kBufSize);
        cdInfo.ParseEcd64e(buf);
      }
      
      RINOK(Skip64(recordSize - kEcd64_MainSize, items.Size()))
    }


    ReadSignature();

    if (_signature != NSignature::kEcd64Locator)
    {
      HeadersError = true;
      return S_OK;
    }
  
    {
      const unsigned kBufSize = 16;
      Byte buf[kBufSize];
      SafeRead(buf, kBufSize);
      locator.Parse(buf);
      // we ignore the error, where some zip creators use (NumDisks == 0)
      // if (locator.NumDisks == 0) HeadersWarning = true;
    }

    ReadSignature();
  }
  
  
  if (_signature != NSignature::kEcd)
  {
    HeadersError = true;
    return S_OK;
  }

  
  CanStartNewVol = false;

  // ---------- ECD ----------

  CEcd ecd;
  {
    const unsigned kBufSize = kEcdSize - 4;
    Byte buf[kBufSize];
    SafeRead(buf, kBufSize);
    ecd.Parse(buf);
  }

  COPY_ECD_ITEM_16(ThisDisk)
  COPY_ECD_ITEM_16(CdDisk)
  COPY_ECD_ITEM_16(NumEntries_in_ThisDisk)
  COPY_ECD_ITEM_16(NumEntries)
  COPY_ECD_ITEM_32(Size)
  COPY_ECD_ITEM_32(Offset)

  bool cdOK = true;

  if ((UInt32)cdInfo.Size != (UInt32)cdSize)
  {
    // return S_FALSE;
    cdOK = false;
  }

  if (isZip64)
  {
    if (cdInfo.NumEntries != numCdItems
        || cdInfo.Size != cdSize)
    {
      cdOK = false;
    }
  }


  if (IsMultiVol)
  {
    if (cdDisk != cdInfo.CdDisk)
      HeadersError = true;
  }
  else if (needSetBase && cdOK)
  {
    const UInt64 oldBase = (UInt64)ArcInfo.Base;
    // localsWereRead == true
    // ArcInfo.Base == ArcInfo.MarkerPos2
    // cdRelatOffset == (cdAbsOffset - ArcInfo.Base)

    if (isZip64)
    {
      if (ecd64Disk == Vols.StartVolIndex)
      {
        const Int64 newBase = (Int64)ecd64AbsOffset - (Int64)locator.Ecd64Offset;
        if (newBase <= (Int64)ecd64AbsOffset)
        {
          if (!localsWereRead || newBase <= (Int64)ArcInfo.MarkerPos2)
          {
            ArcInfo.Base = newBase;
            cdRelatOffset = (UInt64)((Int64)cdAbsOffset - newBase);
          }
          else
            cdOK = false;
        }
      }
    }
    else if (numCdItems != 0) // we can't use ecd.Offset in empty archive?
    {
      if ((int)cdDisk == Vols.StartVolIndex)
      {
        const Int64 newBase = (Int64)cdAbsOffset - (Int64)cdInfo.Offset;
        if (newBase <= (Int64)cdAbsOffset)
        {
          if (!localsWereRead || newBase <= (Int64)ArcInfo.MarkerPos2)
          {
            // cd can be more accurate, when it points before Locals
            // so we change Base and cdRelatOffset
            ArcInfo.Base = newBase;
            cdRelatOffset = cdInfo.Offset;
          }
          else
          {
            // const UInt64 delta = ((UInt64)cdRelatOffset - cdInfo.Offset);
            const UInt64 delta = ((UInt64)(newBase - ArcInfo.Base));
            if ((UInt32)delta == 0)
            {
              // we set Overflow32bit mode, only if there is (x<<32) offset
              // between real_CD_offset_from_MarkerPos and CD_Offset_in_ECD.
              // Base and cdRelatOffset unchanged
              Overflow32bit = true;
            }
            else
              cdOK = false;
          }
        }
        else
          cdOK = false;
      }
    }
    // cdRelatOffset = cdAbsOffset - ArcInfo.Base;

    if (localsWereRead)
    {
      const UInt64 delta = (UInt64)((Int64)oldBase - ArcInfo.Base);
      if (delta != 0)
      {
        FOR_VECTOR (i, items)
          items[i].LocalHeaderPos += delta;
      }
    }
  }

  if (!cdOK)
    HeadersError = true;

  EcdVolIndex = cdInfo.ThisDisk;

  if (!IsMultiVol)
  {
    if (EcdVolIndex == 0 && Vols.MissingZip && Vols.StartIsExe)
    {
      Vols.MissingName.Empty();
      Vols.MissingZip = false;
    }

    if (localsWereRead)
    {
      if (EcdVolIndex != 0)
      {
        FOR_VECTOR (i, items)
          items[i].Disk = EcdVolIndex;
      }
    }

    UseDisk_in_SingleVol = true;
  }

  if (isZip64)
  {
    if ((cdInfo.ThisDisk == 0 && ecd64AbsOffset != (UInt64)(ArcInfo.Base + (Int64)locator.Ecd64Offset))
        // || cdInfo.NumEntries_in_ThisDisk != numCdItems
        || cdInfo.NumEntries != numCdItems
        || cdInfo.Size != cdSize
        || (cdInfo.Offset != cdRelatOffset && !items.IsEmpty()))
    {
      HeadersError = true;
      return S_OK;
    }
  }

  if (cdOK && !cdItems.IsEmpty())
  {
    // ---------- merge Central Directory Items ----------
  
    CRecordVector<unsigned> items2;

    int nextLocalIndex = 0;

    LocalsCenterMerged = true;

    FOR_VECTOR (i, cdItems)
    {
      if (Callback)
      if ((i & 0x3FFF) == 0)
      {
        const UInt64 numFiles64 = items.Size() + items2.Size();
        RINOK(Callback->SetCompleted(&numFiles64, &_cnt))
      }

      const CItemEx &cdItem = cdItems[i];
      
      int index = -1;
      
      if (nextLocalIndex != -1)
      {
        if ((unsigned)nextLocalIndex < items.Size())
        {
          CItemEx &item = items[(unsigned)nextLocalIndex];
          if (item.Disk == cdItem.Disk &&
              (item.LocalHeaderPos == cdItem.LocalHeaderPos
              || (Overflow32bit && (UInt32)item.LocalHeaderPos == cdItem.LocalHeaderPos)))
            index = nextLocalIndex++;
          else
            nextLocalIndex = -1;
        }
      }

      if (index == -1)
        index = FindItem(items, cdItem);

      // index = -1;

      if (index == -1)
      {
        items2.Add(i);
        HeadersError = true;
        continue;
      }

      CItemEx &item = items[(unsigned)index];
      if (item.Name != cdItem.Name
          // || item.Name.Len() != cdItem.Name.Len()
          || item.PackSize != cdItem.PackSize
          || item.Size != cdItem.Size
          // item.ExtractVersion != cdItem.ExtractVersion
          || !FlagsAreSame(item, cdItem)
          || item.Crc != cdItem.Crc)
      {
        HeadersError = true;
        continue;
      }

      // item.Name = cdItem.Name;
      item.MadeByVersion = cdItem.MadeByVersion;
      item.CentralExtra = cdItem.CentralExtra;
      item.InternalAttrib = cdItem.InternalAttrib;
      item.ExternalAttrib = cdItem.ExternalAttrib;
      item.Comment = cdItem.Comment;
      item.FromCentral = cdItem.FromCentral;
      // 22.02: we force utf8 flag, if central header has utf8 flag
      if (cdItem.Flags & NFileHeader::NFlags::kUtf8)
        item.Flags |= NFileHeader::NFlags::kUtf8;
    }

    FOR_VECTOR (k, items2)
      items.Add(cdItems[items2[k]]);
  }

  if (ecd.NumEntries < ecd.NumEntries_in_ThisDisk)
    HeadersError = true;

  if (ecd.ThisDisk == 0)
  {
    // if (isZip64)
    {
      if (ecd.NumEntries != ecd.NumEntries_in_ThisDisk)
        HeadersError = true;
    }
  }

  if (isZip64)
  {
    if (cdInfo.NumEntries != items.Size()
        || (ecd.NumEntries != items.Size() && ecd.NumEntries != 0xFFFF))
      HeadersError = true;
  }
  else
  {
    // old 7-zip could store 32-bit number of CD items to 16-bit field.
    // if (ecd.NumEntries != items.Size())
    if (ecd.NumEntries > items.Size())
      HeadersError = true;

    if (cdInfo.NumEntries != numCdItems)
    {
      if ((UInt16)cdInfo.NumEntries != (UInt16)numCdItems)
        HeadersError = true;
      else
        Cd_NumEntries_Overflow_16bit = true;
    }
  }

  ReadBuffer(ArcInfo.Comment, ecd.CommentSize);

  _inBufMode = false;

  // DisableBufMode();
  // Buffer.Free();
  /* we can't clear buf varibles. we need them to calculate PhySize of archive */

  if ((UInt16)cdInfo.NumEntries != (UInt16)numCdItems
      || (UInt32)cdInfo.Size != (UInt32)cdSize
      || ((UInt32)cdInfo.Offset != (UInt32)cdRelatOffset && !items.IsEmpty()))
  {
    // return S_FALSE;
    HeadersError = true;
  }

  #ifdef ZIP_SELF_CHECK
  if (localsWereRead)
  {
    const UInt64 endPos = ArcInfo.MarkerPos2 + _cnt;
    if (endPos != (IsMultiVol ? Vols.TotalBytesSize : ArcInfo.FileEndPos))
    {
      // there are some data after the end of archive or error in code;
      return E_FAIL;
    }
  }
  #endif
       
  // printf("\nOpen OK");
  return S_OK;
}



HRESULT CInArchive::Open(IInStream *stream, const UInt64 *searchLimit,
    IArchiveOpenCallback *callback, CObjectVector<CItemEx> &items)
{
  items.Clear();
  
  Close();

  UInt64 startPos;
  RINOK(InStream_GetPos(stream, startPos))
  RINOK(InStream_GetSize_SeekToEnd(stream, ArcInfo.FileEndPos))
  _streamPos = ArcInfo.FileEndPos;

  StartStream = stream;
  Stream = stream;
  Callback = callback;

  DisableBufMode();
  
  bool volWasRequested = false;

  if (!Disable_VolsRead)
  if (callback
      && (startPos == 0 || !searchLimit || *searchLimit != 0))
  {
    // we try to read volumes only if it's first call (offset == 0) or scan is allowed.
    volWasRequested = true;
    RINOK(ReadVols())
  }

  if (Disable_FindMarker)
  {
    RINOK(SeekToVol(-1, startPos))
    StreamRef = stream;
    Stream = stream;
    MarkerIsFound = true;
    MarkerIsSafe = true;
    ArcInfo.MarkerPos = startPos;
    ArcInfo.MarkerPos2 = startPos;
  }
  else
  if (IsMultiVol && Vols.StartParsingVol == 0 && (unsigned)Vols.StartParsingVol < Vols.Streams.Size())
  {
    // only StartParsingVol = 0 is safe search.
    RINOK(SeekToVol(0, 0))
    // if (Stream)
    {
      // UInt64 limit = 1 << 22; // for sfx
      UInt64 limit = 0; // without sfx
    
      HRESULT res = FindMarker(&limit);
      
      if (res == S_OK)
      {
        MarkerIsFound = true;
        MarkerIsSafe = true;
      }
      else if (res != S_FALSE)
        return res;
    }
  }
  else
  {
    // printf("\nOpen offset = %u\n", (unsigned)startPos);
    if (IsMultiVol
        && (unsigned)Vols.StartParsingVol < Vols.Streams.Size()
        && Vols.Streams[(unsigned)Vols.StartParsingVol].Stream)
    {
      RINOK(SeekToVol(Vols.StartParsingVol, Vols.StreamIndex == Vols.StartVolIndex ? startPos : 0))
    }
    else
    {
      RINOK(SeekToVol(-1, startPos))
    }
    
    // UInt64 limit = 1 << 22;
    // HRESULT res = FindMarker(&limit);

    HRESULT res = FindMarker(searchLimit);
    
    // const UInt64 curPos = GetVirtStreamPos();
    const UInt64 curPos = ArcInfo.MarkerPos2 + 4;

    if (res == S_OK)
      MarkerIsFound = true;
    else if (!IsMultiVol)
    {
      /*
      // if (startPos != 0), probably CD could be already tested with another call with (startPos == 0).
      // so we don't want to try to open CD again in that case.
      if (startPos != 0)
        return res;
      // we can try to open CD, if there is no Marker and (startPos == 0).
      // is it OK to open such files as ZIP, or big number of false positive, when CD can be find in end of file ?
      */
      return res;
    }
    
    if (ArcInfo.IsSpanMode && !volWasRequested)
    {
      RINOK(ReadVols())
      if (IsMultiVol && MarkerIsFound && ArcInfo.MarkerVolIndex < 0)
        ArcInfo.MarkerVolIndex = Vols.StartVolIndex;
    }

    MarkerIsSafe = !IsMultiVol
        || (ArcInfo.MarkerVolIndex == 0 && ArcInfo.MarkerPos == 0)
        ;
    

    if (IsMultiVol)
    {
      if ((unsigned)Vols.StartVolIndex < Vols.Streams.Size())
      {
        Stream = Vols.Streams[(unsigned)Vols.StartVolIndex].Stream;
        if (Stream)
        {
          RINOK(Seek_SavePos(curPos))
        }
        else
          IsMultiVol = false;
      }
      else
        IsMultiVol = false;
    }

    if (!IsMultiVol)
    {
      if (Vols.StreamIndex != -1)
      {
        Stream = StartStream;
        Vols.StreamIndex = -1;
        InitBuf();
        RINOK(Seek_SavePos(curPos))
      }

      ArcInfo.MarkerVolIndex = -1;
      StreamRef = stream;
      Stream = stream;
    }
  }


  if (!IsMultiVol)
    Vols.ClearRefs();

  {
    HRESULT res;
    try
    {
      res = ReadHeaders(items);
    }
    catch (const CSystemException &e) { res = e.ErrorCode; }
    catch (const CUnexpectEnd &)
    {
      if (items.IsEmpty())
        return S_FALSE;
      UnexpectedEnd = true;
      res = S_OK;
    }
    catch (...)
    {
      DisableBufMode();
      throw;
    }
    
    if (IsMultiVol)
    {
      ArcInfo.FinishPos = ArcInfo.FileEndPos;
      if ((unsigned)Vols.StreamIndex < Vols.Streams.Size())
        if (GetVirtStreamPos() < Vols.Streams[(unsigned)Vols.StreamIndex].Size)
          ArcInfo.ThereIsTail = true;
    }
    else
    {
      ArcInfo.FinishPos = GetVirtStreamPos();
      ArcInfo.ThereIsTail = (ArcInfo.FileEndPos > ArcInfo.FinishPos);
    }

    DisableBufMode();

    IsArcOpen = true;
    if (!IsMultiVol)
      Vols.Streams.Clear();
    return res;
  }
}


HRESULT CInArchive::GetItemStream(const CItemEx &item, bool seekPackData, CMyComPtr<ISequentialInStream> &stream)
{
  stream.Release();

  UInt64 pos = item.LocalHeaderPos;
  if (seekPackData)
    pos += item.LocalFullHeaderSize;

  if (!IsMultiVol)
  {
    if (UseDisk_in_SingleVol && item.Disk != EcdVolIndex)
      return S_OK;
    pos = (UInt64)((Int64)pos + ArcInfo.Base);
    RINOK(InStream_SeekSet(StreamRef, pos))
    stream = StreamRef;
    return S_OK;
  }

  if (item.Disk >= Vols.Streams.Size())
    return S_OK;
    
  IInStream *str2 = Vols.Streams[item.Disk].Stream;
  if (!str2)
    return S_OK;
  RINOK(InStream_SeekSet(str2, pos))
    
  Vols.NeedSeek = false;
  Vols.StreamIndex = (int)item.Disk;
    
  CVolStream *volsStreamSpec = new CVolStream;
  volsStreamSpec->Vols = &Vols;
  stream = volsStreamSpec;
  
  return S_OK;
}

}}