summaryrefslogtreecommitdiff
path: root/zipfile.c
blob: 1990cde4cc63c59f0efeefa61d19b7b8238c1a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
/*
  zipfile.c - Zip 3

  Copyright (c) 1990-2008 Info-ZIP.  All rights reserved.

  See the accompanying file LICENSE, version 2007-Mar-4 or later
  (the contents of which are also included in zip.h) for terms of use.
  If, for some reason, all these files are missing, the Info-ZIP license
  also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
*/
/*
 *  zipfile.c by Mark Adler.
 */
#define __ZIPFILE_C

#include "zip.h"
#include "revision.h"
#ifdef UNICODE_SUPPORT
# include "crc32.h"
#endif

/* for realloc 2/6/2005 EG */
#include <stdlib.h>

#include <errno.h>

/* for toupper() */
#include <ctype.h>

#ifdef VMS
#  include "vms/vms.h"
#  include "vms/vmsmunch.h"
#  include "vms/vmsdefs.h"
#endif

#ifdef WIN32
#  define WIN32_LEAN_AND_MEAN
#  include <windows.h>
#endif

/*
 * XXX start of zipfile.h
 */
#ifdef THEOS
 /* Macros cause stack overflow in compiler */
 ush SH(uch* p) { return ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8)); }
 ulg LG(uch* p) { return ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16)); }
#else /* !THEOS */
 /* Macros for converting integers in little-endian to machine format */
# define SH(a) ((ush)(((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8)))
# define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
# ifdef ZIP64_SUPPORT           /* zip64 support 08/31/2003 R.Nausedat */
#  define LLG(a) ((zoff_t)LG(a) | ((zoff_t)LG((a)+4) << 32))
# endif
#endif /* ?THEOS */

/* Macros for writing machine integers to little-endian format */
#define PUTSH(a,f) {putc((char)((a) & 0xff),(f)); putc((char)((a) >> 8),(f));}
#define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}

#ifdef ZIP64_SUPPORT           /* zip64 support 08/31/2003 R.Nausedat */
# define PUTLLG(a,f) {PUTLG((a) & 0xffffffff,(f)) PUTLG((a) >> 32,(f))}
#endif


/* -- Structure of a ZIP file -- */

/* Signatures for zip file information headers */
#define LOCSIG     0x04034b50L
#define CENSIG     0x02014b50L
#define ENDSIG     0x06054b50L
#define EXTLOCSIG  0x08074b50L

/* Offsets of values in headers */
/* local header */
#define LOCVER  0               /* version needed to extract */
#define LOCFLG  2               /* encrypt, deflate flags */
#define LOCHOW  4               /* compression method */
#define LOCTIM  6               /* last modified file time, DOS format */
#define LOCDAT  8               /* last modified file date, DOS format */
#define LOCCRC  10              /* uncompressed crc-32 for file */
#define LOCSIZ  14              /* compressed size in zip file */
#define LOCLEN  18              /* uncompressed size */
#define LOCNAM  22              /* length of filename */
#define LOCEXT  24              /* length of extra field */

/* extended local header (data descriptor) following file data (if bit 3 set) */
/* if Zip64 then all are 8 byte and not below - 11/1/03 EG */
#define EXTCRC  0               /* uncompressed crc-32 for file */
#define EXTSIZ  4               /* compressed size in zip file */
#define EXTLEN  8               /* uncompressed size */

/* central directory header */
#define CENVEM  0               /* version made by */
#define CENVER  2               /* version needed to extract */
#define CENFLG  4               /* encrypt, deflate flags */
#define CENHOW  6               /* compression method */
#define CENTIM  8               /* last modified file time, DOS format */
#define CENDAT  10              /* last modified file date, DOS format */
#define CENCRC  12              /* uncompressed crc-32 for file */
#define CENSIZ  16              /* compressed size in zip file */
#define CENLEN  20              /* uncompressed size */
#define CENNAM  24              /* length of filename */
#define CENEXT  26              /* length of extra field */
#define CENCOM  28              /* file comment length */
#define CENDSK  30              /* disk number start */
#define CENATT  32              /* internal file attributes */
#define CENATX  34              /* external file attributes */
#define CENOFF  38              /* relative offset of local header */

/* end of central directory record */
#define ENDDSK  0               /* number of this disk */
#define ENDBEG  2               /* number of the starting disk */
#define ENDSUB  4               /* entries on this disk */
#define ENDTOT  6               /* total number of entries */
#define ENDSIZ  8               /* size of entire central directory */
#define ENDOFF  12              /* offset of central on starting disk */
#define ENDCOM  16              /* length of zip file comment */

/* zip64 support 08/31/2003 R.Nausedat */

/* EOCDL_SIG used to detect Zip64 archive */
#define ZIP64_EOCDL_SIG                  0x07064b50
/* EOCDL size is used in the empty archive check */
#define ZIP64_EOCDL_OFS_SIZE                20

#define ZIP_UWORD16_MAX                  0xFFFF                        /* border value */
#define ZIP_UWORD32_MAX                  0xFFFFFFFF                    /* border value */
#define ZIP_EF_HEADER_SIZE               4                             /* size of pre-header of extra fields */

#ifdef ZIP64_SUPPORT
# define ZIP64_EXTCRC                    0                             /* uncompressed crc-32 for file */
# define ZIP64_EXTSIZ                    4                             /* compressed size in zip file */
# define ZIP64_EXTLEN                    12                            /* uncompressed size */
# define ZIP64_EOCD_SIG                  0x06064b50
# define ZIP64_EOCD_OFS_SIZE             40
# define ZIP64_EOCD_OFS_CD_START         48
# define ZIP64_EOCDL_OFS_SIZE                20
# define ZIP64_EOCDL_OFS_EOCD_START      8
# define ZIP64_EOCDL_OFS_TOTALDISKS      16
# define ZIP64_MIN_VER                   45                            /* min version to set in the CD extra records */
# define ZIP64_CENTRAL_DIR_TAIL_SIZE     (56 - 8 - 4)                  /* size of zip64 central dir tail, minus sig and size field bytes */
# define ZIP64_CENTRAL_DIR_TAIL_SIG      0x06064B50L                   /* zip64 central dir tail signature */
# define ZIP64_CENTRAL_DIR_TAIL_END_SIG  0x07064B50L                   /* zip64 end of cen dir locator signature */
# define ZIP64_LARGE_FILE_HEAD_SIZE      32                            /* total size of zip64 extra field */
# define ZIP64_EF_TAG                    0x0001                        /* ID for zip64 extra field */
# define ZIP64_EFIELD_OFS_OSIZE          ZIP_EF_HEADER_SIZE            /* zip64 extra field: offset to original file size */
# define ZIP64_EFIELD_OFS_CSIZE          (ZIP64_EFIELD_OFS_OSIZE + 8)  /* zip64 extra field: offset to compressed file size */
# define ZIP64_EFIELD_OFS_OFS            (ZIP64_EFIELD_OFS_CSIZE + 8)  /* zip64 extra field: offset to offset in archive */
# define ZIP64_EFIELD_OFS_DISK           (ZIP64_EFIELD_OFS_OFS + 8)    /* zip64 extra field: offset to start disk # */
/* -------------------------------------------------------------------------------------------------------------------------- */
 local int adjust_zip_local_entry OF((struct zlist far *));
 local void adjust_zip_central_entry OF((struct zlist far *));
#if 0
 local int remove_local_extra_field OF((struct zlist far *, ulg));
 local int remove_central_extra_field OF((struct zlist far *, ulg));
#endif
 local int add_central_zip64_extra_field OF((struct zlist far *));
 local int add_local_zip64_extra_field OF((struct zlist far *));
#endif /* ZIP64_SUPPORT */
#ifdef UNICODE_SUPPORT
# define UTF8_PATH_EF_TAG                0x7075                        /* ID for Unicode path (up) extra field */
 local int add_Unicode_Path_local_extra_field OF((struct zlist far *));
 local int add_Unicode_Path_cen_extra_field OF((struct zlist far *));
#endif

/* New General Purpose Bit Flag bit 11 flags when entry path and
   comment are in UTF-8 */
#define UTF8_BIT (1 << 11)

/* moved out of ZIP64_SUPPORT - 2/6/2005 EG */
local void write_ushort_to_mem OF((ush, char *));                      /* little endian conversions */
local void write_ulong_to_mem OF((ulg, char *));
#ifdef ZIP64_SUPPORT
 local void write_int64_to_mem OF((uzoff_t, char *));
#endif /* def ZIP64_SUPPORT */
#ifdef UNICODE_SUPPORT
 local void write_string_to_mem OF((char *, char *));
#endif
#if 0
local char *get_extra_field OF((ush, char *, unsigned));           /* zip64 */
#endif
#ifdef UNICODE_SUPPORT
local void read_Unicode_Path_entry OF((struct zlist far *));
local void read_Unicode_Path_local_entry OF((struct zlist far *));
#endif

/* added these self allocators - 2/6/2005 EG */
local void append_ushort_to_mem OF((ush, char **, extent *, extent *));
local void append_ulong_to_mem OF((ulg, char **, extent *, extent *));
#ifdef ZIP64_SUPPORT
 local void append_int64_to_mem OF((uzoff_t, char **, extent *, extent *));
#endif /* def ZIP64_SUPPORT */
local void append_string_to_mem OF((char *, int, char**, extent *, extent *));


/* Local functions */

local int find_next_signature OF((FILE *f));
local int find_signature OF((FILE *, ZCONST char *));
local int is_signature OF((ZCONST char *, ZCONST char *));
local int at_signature OF((FILE *, ZCONST char *));

local int zqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
#ifdef UNICODE_SUPPORT
local int zuqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
#endif
#if 0
 local int scanzipf_reg OF((FILE *f));
#endif
local int scanzipf_regnew OF((void));
#ifndef UTIL
 local int rqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
 local int zbcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
# ifdef UNICODE_SUPPORT
 local int zubcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
#  if 0
 local int zuebcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
#  endif
# endif /* UNICODE_SUPPORT */
 local void zipoddities OF((struct zlist far *));
# if 0
  local int scanzipf_fix OF((FILE *f));
# endif
 local int scanzipf_fixnew OF((void));
# ifdef USE_EF_UT_TIME
   local int ef_scan_ut_time OF((char *ef_buf, extent ef_len, int ef_is_cent,
                                   iztimes *z_utim));
# endif /* USE_EF_UT_TIME */
 local void cutpath OF((char *p, int delim));
#endif /* !UTIL */

/*
 * XXX end of zipfile.h
 */

/* Local data */

#ifdef HANDLE_AMIGA_SFX
   ulg amiga_sfx_offset;        /* place where size field needs updating */
#endif

local int zqcmp(a, b)
ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries */
/* Used by qsort() to compare entries in the zfile list.
 * Compares the internal names z->iname */
{
  char *aname = (*(struct zlist far **)a)->iname;
  char *bname = (*(struct zlist far **)b)->iname;

  return namecmp(aname, bname);
}

#ifdef UNICODE_SUPPORT
local int zuqcmp(a, b)
ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries */
/* Used by qsort() to compare entries in the zfile list.
 * Compares the internal names z->zuname */
{
  char *aname = (*(struct zlist far **)a)->iname;
  char *bname = (*(struct zlist far **)b)->iname;

  /* zuname could be NULL */
  if ((*(struct zlist far **)a)->zuname)
    aname = (*(struct zlist far **)a)->zuname;
  if ((*(struct zlist far **)b)->zuname)
    bname = (*(struct zlist far **)b)->zuname;
  return namecmp(aname, bname);
}
#endif


#ifndef UTIL

local int rqcmp(a, b)
ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries */
/* Used by qsort() to compare entries in the zfile list.
 * Compare the internal names z->iname, but in reverse order. */
{
  return namecmp((*(struct zlist far **)b)->iname,
                 (*(struct zlist far **)a)->iname);
}


local int zbcmp(n, z)
ZCONST zvoid *n;        /* string to search for */
ZCONST zvoid far *z;    /* pointer to a pointer to a zip entry */
/* Used by search() to compare a target to an entry in the zfile list. */
{
  return namecmp((char *)n, ((struct zlist far *)z)->zname);
}

#ifdef UNICODE_SUPPORT
/* search unicode paths */
local int zubcmp(n, z)
ZCONST zvoid *n;        /* string to search for */
ZCONST zvoid far *z;    /* pointer to a pointer to a zip entry */
/* Used by search() to compare a target to an entry in the zfile list. */
{
  char *zuname = ((struct zlist far *)z)->zuname;

  /* zuname is NULL if no UTF-8 name */
  if (zuname == NULL)
    zuname = ((struct zlist far *)z)->zname;

  return namecmp((char *)n, zuname);
}

#if 0
/* search escaped unicode paths */
local int zuebcmp(n, z)
ZCONST zvoid *n;        /* string to search for */
ZCONST zvoid far *z;    /* pointer to a pointer to a zip entry */
/* Used by search() to compare a target to an entry in the zfile list. */
{
  char *zuname = ((struct zlist far *)z)->zuname;
  char *zuename;
  int k;

  /* zuname is NULL if no UTF-8 name */
  if (zuname == NULL)
    zuname = ((struct zlist far *)z)->zname;
  zuename = local_to_escape_string(zuname);
  k = namecmp((char *)n, zuename);
  free(zuename);

  return k;
}
#endif
#endif


struct zlist far *zsearch(n)
  ZCONST char *n;      /* name to find */
/* Return a pointer to the entry in zfile with the name n, or NULL if
   not found. */
{
  zvoid far **p;        /* result of search() */

  if (zcount) {
    if ((p = search(n, (ZCONST zvoid far **)zsort, zcount, zbcmp)) != NULL)
      return *(struct zlist far **)p;
#ifdef UNICODE_SUPPORT
    else if (unicode_mismatch != 3 && fix != 2 &&
        (p = search(n, (ZCONST zvoid far **)zusort, zcount, zubcmp)) != NULL)
      return *(struct zlist far **)p;
#endif
    else
      return NULL;
  }
  return NULL;
}

#endif /* !UTIL */

#ifndef VMS     /* See [.VMS]VMS.C for VMS-specific ziptyp(). */
#  ifndef PATHCUT
#    define PATHCUT '/'
#  endif

char *ziptyp(s)
  char *s;             /* file name to force to zip */
/* If the file name *s has a dot (other than the first char), or if
   the -A option is used (adjust self-extracting file) then return
   the name, otherwise append .zip to the name.  Allocate the space for
   the name in either case.  Return a pointer to the new name, or NULL
   if malloc() fails. */
{
  char *q;              /* temporary pointer */
  char *t;              /* pointer to malloc'ed string */
#  ifdef THEOS
  char *r;              /* temporary pointer */
  char *disk;
#  endif

  if ((t = malloc(strlen(s) + 5)) == NULL)
    return NULL;
  strcpy(t, s);
#  ifdef __human68k__
  _toslash(t);
#  endif
#  ifdef MSDOS
  for (q = t; *q; INCSTR(q))
    if (*q == '\\')
      *q = '/';
#  endif /* MSDOS */
#  if defined(__RSXNT__) || defined(WIN32_CRT_OEM)
   /* RSXNT/EMX C rtl uses OEM charset */
  AnsiToOem(t, t);
#  endif
  if (adjust) return t;
#  ifndef RISCOS
#    ifndef QDOS
#      ifdef AMIGA
  if ((q = MBSRCHR(t, '/')) == NULL)
    q = MBSRCHR(t, ':');
  if (MBSRCHR((q ? q + 1 : t), '.') == NULL)
#      else /* !AMIGA */
#        ifdef THEOS
  /* the argument expansion add a dot to the end of file names when
   * there is no extension and at least one of a argument has wild cards.
   * So check for at least one character in the extension if there is a dot
   * in file name */
  if ((q = MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.')) == NULL
    || q[1] == '\0') {
#        else /* !THEOS */
#          ifdef TANDEM
  if (MBSRCHR((q = MBSRCHR(t, '.')) == NULL ? t : q + 1, ' ') == NULL)
#          else /* !TANDEM */
  if (MBSRCHR((q = MBSRCHR(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)
#          endif /* ?TANDEM */
#        endif /* ?THEOS */
#      endif /* ?AMIGA */
#      ifdef CMS_MVS
    if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)
#      endif /* CMS_MVS */
#      ifdef THEOS
    /* insert .zip extension before disk name */
    if ((r = MBSRCHR(t, ':')) != NULL) {
        /* save disk name */
        if ((disk = strdup(r)) == NULL)
            return NULL;
        strcpy(r[-1] == '.' ? r - 1 : r, ".zip");
        strcat(t, disk);
        free(disk);
    } else {
        if (q != NULL && *q == '.')
          strcpy(q, ".zip");
        else
          strcat(t, ".zip");
    }
  }
#      else /* !THEOS */
#        ifdef TANDEM     /*  Tandem can't cope with extensions */
    strcat(t, " ZIP");
#        else /* !TANDEM */
    strcat(t, ".zip");
#        endif /* ?TANDEM */
#      endif /* ?THEOS */
#    else /* QDOS */
  q = LastDir(t);
  if(MBSRCHR(q, '_') == NULL && MBSRCHR(q, '.') == NULL)
  {
      strcat(t, "_zip");
  }
#    endif /* QDOS */
#  endif /* !RISCOS */
  return t;
}
#endif  /* ndef VMS */

/* ---------------------------------------------------- */

/* moved out of ZIP64_SUPPORT - 2/6/2005 EG */

/* 08/31/2003 R.Nausedat */

local void write_ushort_to_mem( OFT( ush) usValue,
                                OFT( char *)pPtr)
#ifdef NO_PROTO
  ush usValue;
  char *pPtr;
#endif /* def NO_PROTO */
{
  *pPtr++ = ((char)(usValue) & 0xff);
  *pPtr = ((char)(usValue >> 8) & 0xff);
}

local void write_ulong_to_mem(uValue, pPtr)
ulg uValue;
char *pPtr;
{
  write_ushort_to_mem((ush)(uValue & 0xffff), pPtr);
  write_ushort_to_mem((ush)((uValue >> 16) & 0xffff), pPtr + 2);
}

#ifdef ZIP64_SUPPORT

local void write_int64_to_mem(l64Value,pPtr)
  uzoff_t l64Value;
  char *pPtr;
{
  write_ulong_to_mem((ulg)(l64Value & 0xffffffff),pPtr);
  write_ulong_to_mem((ulg)((l64Value >> 32) & 0xffffffff),pPtr + 4);
}

#endif /* def ZIP64_SUPPORT */

#ifdef UNICODE_SUPPORT

/* Write a string to memory */
local void write_string_to_mem(strValue, pPtr)
  char *strValue;
  char *pPtr;
{
  if (strValue != NULL) {
    int ssize = strlen(strValue);
    int i;

    for (i = 0; i < ssize; i++) {
      *(pPtr + i) = *(strValue + i);
    }
  }
}

#endif /* def UNICODE_SUPPORT */



/* same as above but allocate memory as needed and keep track of current end
   using offset - 2/6/05 EG */

#if 0 /* ubyte version not used */
local void append_ubyte_to_mem( OFT( unsigned char) ubValue,
                                OFT( char **) pPtr,
                                OFT( extent *) offset,
                                OFT( extent *) blocksize)
#ifdef NO_PROTO
  unsigned char ubValue;  /* byte to append */
  char **pPtr;            /* start of block */
  extent *offset;         /* next byte to write */
  extent *blocksize;      /* current size of block */
#endif /* def NO_PROTO */
{
  if (*pPtr == NULL) {
    /* malloc a 1K block */
    (*blocksize) = 1024;
    *pPtr = (char *) malloc(*blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_ubyte_to_mem");
    }
  }
  /* if (*offset) + 1 > (*blocksize) - 1 */
  else if ((*offset) > (*blocksize) - (1 + 1)) {
    /* realloc a bigger block in 1 K increments */
    (*blocksize) += 1024;
    *pPtr = realloc(*pPtr, *blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_ubyte_to_mem");
    }
  }
  *(*pPtr + *offset) = ubValue;
  (*offset)++;
}
#endif

local void append_ushort_to_mem( OFT( ush) usValue,
                                 OFT( char **) pPtr,
                                 OFT( extent *) offset,
                                 OFT( extent *) blocksize)
#ifdef NO_PROTO
  ush usValue;
  char **pPtr;
  extent *offset;
  extent *blocksize;
#endif /* def NO_PROTO */
{
  if (*pPtr == NULL) {
    /* malloc a 1K block */
    (*blocksize) = 1024;
    *pPtr = (char *) malloc(*blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_ushort_to_mem");
    }
  }
  /* if (*offset) + 2 > (*blocksize) - 1 */
  else if ((*offset) > (*blocksize) - (1 + 2)) {
    /* realloc a bigger block in 1 K increments */
    (*blocksize) += 1024;
    *pPtr = realloc(*pPtr, (extent)*blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_ushort_to_mem");
    }
  }
  write_ushort_to_mem(usValue, (*pPtr) + (*offset));
  (*offset) += 2;
}

local void append_ulong_to_mem(uValue, pPtr, offset, blocksize)
  ulg uValue;
  char **pPtr;
  extent *offset;
  extent *blocksize;
{
  if (*pPtr == NULL) {
    /* malloc a 1K block */
    (*blocksize) = 1024;
    *pPtr = (char *) malloc(*blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_ulong_to_mem");
    }
  }
  else if ((*offset) > (*blocksize) - (1 + 4)) {
    /* realloc a bigger block in 1 K increments */
    (*blocksize) += 1024;
    *pPtr = realloc(*pPtr, *blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_ulong_to_mem");
    }
  }
  write_ulong_to_mem(uValue, (*pPtr) + (*offset));
  (*offset) += 4;
}

#ifdef ZIP64_SUPPORT

local void append_int64_to_mem(l64Value, pPtr, offset, blocksize)
  uzoff_t l64Value;
  char **pPtr;
  extent *offset;
  extent *blocksize;
{
  if (*pPtr == NULL) {
    /* malloc a 1K block */
    (*blocksize) = 1024;
    *pPtr = (char *) malloc(*blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_int64_to_mem");
    }
  }
  else if ((*offset) > (*blocksize) - (1 + 8)) {
    /* realloc a bigger block in 1 K increments */
    (*blocksize) += 1024;
    *pPtr = realloc(*pPtr, *blocksize);
    if (*pPtr == NULL) {
      ziperr(ZE_MEM, "append_int64_to_mem");
    }
  }
  write_int64_to_mem(l64Value, (*pPtr) + (*offset));
  (*offset) += 8;
}

#endif /* def ZIP64_SUPPORT */

/* Append a string to the memory block. */
local void append_string_to_mem(strValue, strLength, pPtr, offset, blocksize)
  char *strValue;
  int  strLength;
  char **pPtr;
  extent *offset;
  extent *blocksize;
{
  if (strValue != NULL) {
    unsigned bsize = 1024;
    unsigned ssize = strLength;
    unsigned i;

    if (ssize > bsize) {
      bsize = ssize;
    }
    if (*pPtr == NULL) {
      /* malloc a 1K block */
      (*blocksize) = bsize;
      *pPtr = (char *) malloc(*blocksize);
      if (*pPtr == NULL) {
        ziperr(ZE_MEM, "append_string_to_mem");
      }
    }
    else if ((*offset) + ssize > (*blocksize) - 1) {
      /* realloc a bigger block in 1 K increments */
      (*blocksize) += bsize;
      *pPtr = realloc(*pPtr, *blocksize);
      if (*pPtr == NULL) {
        ziperr(ZE_MEM, "append_string_to_mem");
      }
    }
    for (i = 0; i < ssize; i++) {
      *(*pPtr + *offset + i) = *(strValue + i);
    }
    (*offset) += ssize;
  }
}

/* ---------------------------------------------------- */

/* zip64 support 08/31/2003 R.Nausedat */
/* moved out of zip64 support 10/22/05 */

/* Searches pExtra for extra field with specified tag.
 * If it finds one it returns a pointer to it, else NULL.
 * Renamed and made generic.  10/3/03
 */
char *get_extra_field( OFT( ush) tag,
                       OFT( char *) pExtra,
                       OFT( unsigned) iExtraLen)
#ifdef NO_PROTO
  ush tag;              /* tag to look for */
  char *pExtra;         /* pointer to extra field in memory */
  unsigned iExtraLen;   /* length of extra field */
#endif /* def NO_PROTO */
{
  char  *pTemp;
  ush   usBlockTag;
  ush   usBlockSize;

  if( pExtra == NULL )
    return NULL;

  for (pTemp = pExtra; pTemp < pExtra  + iExtraLen - ZIP_EF_HEADER_SIZE;)
  {
    usBlockTag = SH(pTemp);       /* get tag */
    usBlockSize = SH(pTemp + 2);  /* get field data size */
    if (usBlockTag == tag)
      return pTemp;
    pTemp += (usBlockSize + ZIP_EF_HEADER_SIZE);
  }
  return NULL;
}

/* copy_nondup_extra_fields
 *
 * Copy any extra fields in old that are not in new to new.
 * Returns the new extra fields block and newLen is new length.
 */
char *copy_nondup_extra_fields(oldExtra, oldExtraLen, newExtra, newExtraLen, newLen)
  char *oldExtra;       /* pointer to old extra fields */
  unsigned oldExtraLen; /* length of old extra fields */
  char *newExtra;       /* pointer to new extra fields */
  unsigned newExtraLen; /* length of new extra fields */
  unsigned *newLen;     /* length of new extra fields after copy */
{
  char *returnExtra = NULL;
  ush   returnExtraLen = 0;
  char *tempExtra;
  char *pTemp;
  ush   tag;
  ush   blocksize;

  if( oldExtra == NULL ) {
    /* no old extra fields so return copy of newExtra */
    if (newExtra == NULL || newExtraLen == 0) {
      *newLen = 0;
      return NULL;
    } else {
      if ((returnExtra = malloc(newExtraLen)) == NULL)
        ZIPERR(ZE_MEM, "extra field copy");
      memcpy(returnExtra, newExtra, newExtraLen);
      returnExtraLen = newExtraLen;
      *newLen = returnExtraLen;
      return returnExtra;
    }
  }

  /* allocate block large enough for all extra fields */
  if ((tempExtra = malloc(0xFFFF)) == NULL)
    ZIPERR(ZE_MEM, "extra field copy");

  /* look for each old extra field in new block */
  for (pTemp = oldExtra; pTemp < oldExtra  + oldExtraLen;)
  {
    tag = SH(pTemp);            /* get tag */
    blocksize = SH(pTemp + 2);  /* get field data size */
    if (get_extra_field(tag, newExtra, newExtraLen) == NULL) {
      /* tag not in new block so add it */
      memcpy(tempExtra + returnExtraLen, pTemp, blocksize + 4);
      returnExtraLen += blocksize + 4;
    }
    pTemp += blocksize + 4;
  }

  /* copy all extra fields from new block */
  memcpy(tempExtra + returnExtraLen, newExtra, newExtraLen);
  returnExtraLen += newExtraLen;

  /* copy tempExtra to returnExtra */
  if ((returnExtra = malloc(returnExtraLen)) == NULL)
    ZIPERR(ZE_MEM, "extra field copy");
  memcpy(returnExtra, tempExtra, returnExtraLen);
  free(tempExtra);

  *newLen = returnExtraLen;
  return returnExtra;
}

#ifdef UNICODE_SUPPORT

/* The latest format is
     1 byte     Version of Unicode Path Extra Field
     4 bytes    Name Field CRC32 Checksum
     variable   UTF-8 Version Of Name
 */

local void read_Unicode_Path_entry(pZipListEntry)
  struct zlist far *pZipListEntry;
{
  char *pTemp;
  char *UPath;
  char *iname;
  ush ELen;
  uch Version;
  ush ULen;
  ulg chksum = CRCVAL_INITIAL;
  ulg iname_chksum;

  /* check if we have a Unicode Path extra field ... */
  pTemp = get_extra_field( UTF8_PATH_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext );
  pZipListEntry->uname = NULL;
  if( pTemp == NULL ) {
    return;
  }

  /* ... if so, update corresponding entries in struct zlist */

  pTemp += 2;

  /* length of this extra field */
  ELen = SH(pTemp);
  pTemp += 2;

  /* version */
  Version = (uch) *pTemp;
  pTemp += 1;
  if (Version > 1) {
    zipwarn("Unicode Path Extra Field version > 1 - skipping", pZipListEntry->oname);
    return;
  }

  /* iname CRC */
  iname_chksum = LG(pTemp);
  pTemp += 4;

  /*
   * Compute the CRC-32 checksum of iname
   */
/*
  crc_16 = crc16f((uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
 */

  if ((iname = malloc(strlen(pZipListEntry->iname) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "write Unicode");
  }
  strcpy(iname, pZipListEntry->iname);

  chksum = crc32(chksum, (uch *)(iname), strlen(iname));

  free(iname);

/*  chksum = adler16(ADLERVAL_INITIAL,
    (uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
*/

  /* If the checksums's don't match then likely iname has been modified and
   * the Unicode Path is no longer valid
   */
  if (chksum != iname_chksum) {
    printf("unicode_mismatch = %d\n", unicode_mismatch);
    if (unicode_mismatch == 1) {
      /* warn and continue */
      zipwarn("Unicode does not match path - ignoring Unicode: ", pZipListEntry->oname);
    } else if (unicode_mismatch == 2) {
      /* ignore and continue */
    } else if (unicode_mismatch == 0) {
      /* error */
      sprintf(errbuf, "Unicode does not match path:  %s\n", pZipListEntry->oname);
      strcat(errbuf,
        "                     Likely entry name changed but Unicode not updated\n");
      strcat(errbuf,
        "                     Use -UN=i to ignore errors or n for no Unicode paths");
      zipwarn(errbuf, "");
      ZIPERR(ZE_FORM, "Unicode path error");
    }
    return;
  }

  ULen = ELen - 5;

  /* UTF-8 Path */
  if (ULen == 0) {
    /* standard path is UTF-8 so use that */
    ULen = pZipListEntry->nam;
    if ((UPath = malloc(ULen + 1)) == NULL) {
      return;
    }
    strcpy(UPath, pZipListEntry->name);
  } else {
    /* use Unicode path */
    if ((UPath = malloc(ULen + 1)) == NULL) {
      return;
    }
    strncpy(UPath, pTemp, ULen);
    UPath[ULen] = '\0';
  }
  pZipListEntry->uname = UPath;
  return;
}

local void read_Unicode_Path_local_entry(pZipListEntry)
  struct zlist far *pZipListEntry;
{
  char *pTemp;
  char *UPath;
  char *iname;
  ush ELen;
  uch Version;
  ush ULen;
  ulg chksum = CRCVAL_INITIAL;
  ulg iname_chksum;

  /* check if we have a Unicode Path extra field ... */
  pTemp = get_extra_field( UTF8_PATH_EF_TAG, pZipListEntry->extra, pZipListEntry->ext );
  pZipListEntry->uname = NULL;
  if( pTemp == NULL ) {
    return;
  }

  /* ... if so, update corresponding entries in struct zlist */

  pTemp += 2;

  /* length of this extra field */
  ELen = SH(pTemp);
  pTemp += 2;

  /* version */
  Version = (uch) *pTemp;
  pTemp += 1;
  if (Version > 1) {
    zipwarn("Unicode Path Extra Field version > 1 - skipping", pZipListEntry->oname);
    return;
  }

  /* iname CRC */
  iname_chksum = LG(pTemp);
  pTemp += 4;

  /*
   * Compute 32-bit crc of iname and AND halves to make 16-bit version
   */
  /*
  chksum = adler16(ADLERVAL_INITIAL,
    (uch *)(pZipListEntry->iname), strlen(pZipListEntry->iname));
  */

  if ((iname = malloc(strlen(pZipListEntry->iname) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "write Unicode");
  }
  strcpy(iname, pZipListEntry->iname);

  chksum = crc32(chksum, (uch *)(iname), strlen(iname));

  free(iname);

  /* If the checksums's don't match then likely iname has been modified and
   * the Unicode Path is no longer valid
   */
  if (chksum != iname_chksum) {
    if (unicode_mismatch == 1) {
      /* warn and continue */
      zipwarn("Unicode does not match path - ignoring Unicode: ", pZipListEntry->oname);
    } else if (unicode_mismatch == 2) {
      /* ignore and continue */
    } else if (unicode_mismatch == 0) {
      /* error */
      sprintf(errbuf, "Unicode does not match path:  %s\n", pZipListEntry->oname);
      strcat(errbuf,
        "                     Likely entry name changed but Unicode not updated\n");
      strcat(errbuf,
        "                     Use -UN=i to ignore errors or n for no Unicode paths");
      zipwarn(errbuf, "");
      ZIPERR(ZE_FORM, "Unicode path error");
    }
    return;
  }

  ULen = ELen - 5;

  /* UTF-8 Path */
  if (ULen == 0) {
    /* standard path is UTF-8 so use that */
    ULen = pZipListEntry->nam;
    if ((UPath = malloc(ULen + 1)) == NULL) {
      return;
    }
    strcpy(UPath, pZipListEntry->name);
  } else {
    /* use Unicode path */
    if ((UPath = malloc(ULen + 1)) == NULL) {
      return;
    }
    strncpy(UPath, pTemp, ULen);
    UPath[ULen] = '\0';
  }
  pZipListEntry->uname = UPath;
  return;
}

#endif /* def UNICODE_SUPPORT */

#ifdef ZIP64_SUPPORT           /* zip64 support 08/31/2003 R.Nausedat */

/* searches the cextra member of zlist for a zip64 extra field. if it finds one it  */
/* updates the len, siz and off members of zlist with the corresponding values of   */
/* the zip64 extra field, that is if either the len, siz or off member of zlist is  */
/* set to its max value we have to use the corresponding value from the zip64 extra */
/* field. as of now the dsk member of zlist is not much of interest since we should */
/* not modify multi volume archives at all.                                         */
local void adjust_zip_central_entry(pZipListEntry)
  struct zlist far *pZipListEntry;
{
  char  *pTemp;

  /* assume not using zip64 fields */
  zip64_entry = 0;

  /* check if we have a "large file" Zip64 extra field ... */
  pTemp = get_extra_field( ZIP64_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext );
  if( pTemp == NULL )
    return;

  /* using zip64 field */
  zip64_entry = 1;
  pTemp += ZIP_EF_HEADER_SIZE;

  /* ... if so, update corresponding entries in struct zlist */
  if (pZipListEntry->len == ZIP_UWORD32_MAX)
  {
    pZipListEntry->len = LLG(pTemp);
    pTemp += 8;
  }

  if (pZipListEntry->siz == ZIP_UWORD32_MAX)
  {
    pZipListEntry->siz = LLG(pTemp);
    pTemp += 8;
  }

  if (pZipListEntry->off == ZIP_UWORD32_MAX)
  {
    pZipListEntry->off = LLG(pTemp);
    pTemp += 8;
  }

  if (pZipListEntry->dsk == ZIP_UWORD16_MAX)
  {
    pZipListEntry->dsk = LG(pTemp);
  }

}


/* adjust_zip_local_entry
 *
 * Return 1 if there is a Zip64 extra field and 0 if not
 */
local int adjust_zip_local_entry(pZipListEntry)
  struct zlist far *pZipListEntry;
{
  char  *pTemp;

  /* assume not using zip64 fields */
  zip64_entry = 0;

  /* check if we have a "large file" Zip64 extra field ... */
  pTemp = get_extra_field(ZIP64_EF_TAG, pZipListEntry->extra, pZipListEntry->ext );
  if( pTemp == NULL )
    return zip64_entry;

  /* using zip64 field */
  zip64_entry = 1;
  pTemp += ZIP_EF_HEADER_SIZE;

  /* ... if so, update corresponding entries in struct zlist */
  if (pZipListEntry->len == ZIP_UWORD32_MAX)
  {
    pZipListEntry->len = LLG(pTemp);
    pTemp += 8;
  }

  if (pZipListEntry->siz == ZIP_UWORD32_MAX)
  {
    pZipListEntry->siz = LLG(pTemp);
    pTemp += 8;
  }
  return zip64_entry;
}

/* adds a zip64 extra field to the data the cextra member of zlist points to. If
 * there is already a zip64 extra field present delete it first.
 */
local int add_central_zip64_extra_field(pZipListEntry)
  struct zlist far *pZipListEntry;
{
  char   *pExtraFieldPtr;
  char   *pTemp;
  ush    usTemp;
  ush    efsize = 0;
  ush    esize;
  ush    oldefsize;
  extent len;
  int    used_zip64 = 0;

  /* get length of ef based on which fields exceed limits */
  /* AppNote says:
   *      The order of the fields in the ZIP64 extended
   *      information record is fixed, but the fields will
   *      only appear if the corresponding Local or Central
   *      directory record field is set to 0xFFFF or 0xFFFFFFFF.
   */
  efsize = ZIP_EF_HEADER_SIZE;             /* type + size */
  if (pZipListEntry->len > ZIP_UWORD32_MAX || force_zip64 == 1) {
    /* compressed size */
    efsize += 8;
    used_zip64 = 1;
  }
  if (pZipListEntry->siz > ZIP_UWORD32_MAX) {
    /* uncompressed size */
    efsize += 8;
    used_zip64 = 1;
  }
  if (pZipListEntry->off > ZIP_UWORD32_MAX) {
    /* offset */
    efsize += 8;
    used_zip64 = 1;
  }
  if (pZipListEntry->dsk > ZIP_UWORD16_MAX) {
    /* disk number */
    efsize += 4;
    used_zip64 = 1;
  }

  if (used_zip64 && force_zip64 == 0) {
    zipwarn("Large entry support disabled using -fz- but needed", "");
    return ZE_BIG;
  }

  /* malloc zip64 extra field? */
  if( pZipListEntry->cextra == NULL )
  {
    if (efsize == ZIP_EF_HEADER_SIZE) {
      return ZE_OK;
    }
    if ((pExtraFieldPtr = pZipListEntry->cextra = (char *) malloc(efsize)) == NULL) {
      return ZE_MEM;
    }
    pZipListEntry->cext = efsize;
  }
  else
  {
    /* check if we have a "large file" extra field ... */
    pExtraFieldPtr = get_extra_field(ZIP64_EF_TAG, pZipListEntry->cextra, pZipListEntry->cext);
    if( pExtraFieldPtr == NULL )
    {
      /* ... we don't, so re-malloc enough memory for the old extra data plus
       * the size of the zip64 extra field
       */
      if ((pExtraFieldPtr = (char *) malloc(efsize + pZipListEntry->cext)) == NULL) {
        return ZE_MEM;
      }
      /* move the old extra field */
      memmove(pExtraFieldPtr, pZipListEntry->cextra, pZipListEntry->cext);
      free(pZipListEntry->cextra);
      pZipListEntry->cextra = pExtraFieldPtr;
      pExtraFieldPtr += pZipListEntry->cext;
      pZipListEntry->cext += efsize;
    }
    else
    {
      /* ... we have. sort out the existing zip64 extra field and remove it from
       * pZipListEntry->cextra, re-malloc enough memory for the old extra data
       * left plus the size of the zip64 extra field
       */
      usTemp = SH(pExtraFieldPtr + 2);
      /* if pZipListEntry->cextra == pExtraFieldPtr and pZipListEntry->cext == usTemp + efsize
       * we should have only one extra field, and this is a zip64 extra field. as some
       * zip tools seem to require fixed zip64 extra fields we have to check if
       * usTemp + ZIP_EF_HEADER_SIZE is equal to ZIP64_LARGE_FILE_HEAD_SIZE. if it
       * isn't, we free the old extra field and allocate memory for a new one
       */
      if( pZipListEntry->cext == (extent)(usTemp + ZIP_EF_HEADER_SIZE) )
      {
        /* just Zip64 extra field in extra field */
        if( pZipListEntry->cext != efsize )
        {
          /* wrong size */
          if ((pExtraFieldPtr = (char *) malloc(efsize)) == NULL) {
            return ZE_MEM;
          }
          free(pZipListEntry->cextra);
          pZipListEntry->cextra = pExtraFieldPtr;
          pZipListEntry->cext = efsize;
        }
      }
      else
      {
        /* get the old Zip64 extra field out and add new */
        oldefsize = usTemp + ZIP_EF_HEADER_SIZE;
        if ((pTemp = (char *) malloc(pZipListEntry->cext - oldefsize + efsize)) == NULL) {
          return ZE_MEM;
        }
        len = (extent)(pExtraFieldPtr - pZipListEntry->cextra);
        memcpy(pTemp, pZipListEntry->cextra, len);
        memcpy(pTemp + len, pExtraFieldPtr + oldefsize,
          pZipListEntry->cext - oldefsize - len);
        pZipListEntry->cext -= oldefsize;
        pExtraFieldPtr = pTemp + pZipListEntry->cext;
        pZipListEntry->cext += efsize;
        free(pZipListEntry->cextra);
        pZipListEntry->cextra = pTemp;
      }
    }
  }

  /* set zip64 extra field members */
  write_ushort_to_mem(ZIP64_EF_TAG, pExtraFieldPtr);
  write_ushort_to_mem((ush) (efsize - ZIP_EF_HEADER_SIZE), pExtraFieldPtr + 2);
  esize = ZIP_EF_HEADER_SIZE;
  if (pZipListEntry->len > ZIP_UWORD32_MAX || force_zip64 == 1) {
    write_int64_to_mem(pZipListEntry->len, pExtraFieldPtr + esize);
    esize += 8;
  }
  if (pZipListEntry->siz > ZIP_UWORD32_MAX) {
    write_int64_to_mem(pZipListEntry->siz, pExtraFieldPtr + esize);
    esize += 8;
  }
  if (pZipListEntry->off > ZIP_UWORD32_MAX) {
    write_int64_to_mem(pZipListEntry->off, pExtraFieldPtr + esize);
    esize += 8;
  }
  if (pZipListEntry->dsk > ZIP_UWORD16_MAX) {
    write_ulong_to_mem(pZipListEntry->dsk, pExtraFieldPtr + esize);
  }

  /* un' wech */
  return ZE_OK;
}

#if 0
/* Remove extra field in local extra field
 * Return 1 if found, else 0
 * 12/28/05
 */
local int remove_local_extra_field(pZEntry, tag)
  struct zlist far *pZEntry;
  ulg tag;
{
  char  *pExtra;
  char  *pOldExtra;
  char  *pOldTemp;
  char  *pTemp;
  ush   newEFSize;
  ush   usTemp;
  ush   blocksize;

  /* check if we have the extra field ... */
  pOldExtra = get_extra_field( (ush)tag, pZEntry->extra, pZEntry->ext );
  if (pOldExtra)
  {
    /* We have. Get rid of it. */
    blocksize = SH( pOldExtra + 2 );
    newEFSize = pZEntry->ext - blocksize;
    pExtra = (char *) malloc( newEFSize );
    if( pExtra == NULL )
      ziperr(ZE_MEM, "Remove Local Extra Field");
    /* move all before EF */
    usTemp = (extent) (pOldExtra - pZEntry->extra);
    pTemp = pExtra;
    memcpy( pTemp, pZEntry->extra, usTemp );
    /* move all after old Zip64 EF */
    pTemp = pExtra + usTemp;
    pOldTemp = pOldExtra + blocksize;
    usTemp = pZEntry->ext - usTemp - blocksize;
    memcpy( pTemp, pOldTemp, usTemp);
    /* replace extra fields */
    pZEntry->ext = newEFSize;
    free(pZEntry->extra);
    pZEntry->extra = pExtra;
    return 1;
  } else {
    return 0;
  }
}

/* Remove extra field in central extra field
 * Return 1 if found, else 0
 * 12/28/05
 */
local int remove_central_extra_field(pZEntry, tag)
  struct zlist far *pZEntry;
  ulg tag;
{
  char  *pExtra;
  char  *pOldExtra;
  char  *pOldTemp;
  char  *pTemp;
  ush   newEFSize;
  ush   usTemp;
  ush   blocksize;

  /* check if we have the extra field ... */
  pOldExtra = get_extra_field( (ush)tag, pZEntry->cextra, pZEntry->cext );
  if (pOldExtra)
  {
    /* We have. Get rid of it. */
    blocksize = SH( pOldExtra + 2 );
    newEFSize = pZEntry->cext - blocksize;
    pExtra = (char *) malloc( newEFSize );
    if( pExtra == NULL )
      ziperr(ZE_MEM, "Remove Local Extra Field");
    /* move all before EF */
    usTemp = (extent) (pOldExtra - pZEntry->cextra);
    pTemp = pExtra;
    memcpy( pTemp, pZEntry->cextra, usTemp );
    /* move all after old Zip64 EF */
    pTemp = pExtra + usTemp;
    pOldTemp = pOldExtra + blocksize;
    usTemp = pZEntry->cext - usTemp - blocksize;
    memcpy( pTemp, pOldTemp, usTemp);
    /* replace extra fields */
    pZEntry->cext = newEFSize;
    free(pZEntry->cextra);
    pZEntry->cextra = pExtra;
    return 1;
  } else {
    return 0;
  }
}
#endif

/* Add Zip64 extra field to local header
 * 10/5/03 EG
 */
local int add_local_zip64_extra_field(pZEntry)
  struct zlist far *pZEntry;
{
  char  *pZ64Extra;
  char  *pOldZ64Extra;
  char  *pOldTemp;
  char  *pTemp;
  ush   newEFSize;
  ush   usTemp;
  ush   blocksize;
  ush   Z64LocalLen = ZIP_EF_HEADER_SIZE +  /* tag + EF Data Len */
                      8 +                   /* original uncompressed length of file */
                      8;                    /* compressed size of file */

  /* malloc zip64 extra field? */
  /* after the below pZ64Extra should point to start of Zip64 extra field */
  if (pZEntry->ext == 0 || pZEntry->extra == NULL)
  {
    /* get new extra field */
    pZ64Extra = pZEntry->extra = (char *) malloc(Z64LocalLen);
    if (pZEntry->extra == NULL) {
      ziperr( ZE_MEM, "Zip64 local extra field" );
    }
    pZEntry->ext = Z64LocalLen;
  }
  else
  {
    /* check if we have a Zip64 extra field ... */
    pOldZ64Extra = get_extra_field( ZIP64_EF_TAG, pZEntry->extra, pZEntry->ext );
    if (pOldZ64Extra == NULL)
    {
      /* ... we don't, so re-malloc enough memory for the old extra data plus */
      /* the size of the zip64 extra field */
      pZ64Extra = (char *) malloc( Z64LocalLen + pZEntry->ext );
      if (pZ64Extra == NULL)
        ziperr( ZE_MEM, "Zip64 Extra Field" );
      /* move old extra field and update pointer and length */
      memmove( pZ64Extra, pZEntry->extra, pZEntry->ext);
      free( pZEntry->extra );
      pZEntry->extra = pZ64Extra;
      pZ64Extra += pZEntry->ext;
      pZEntry->ext += Z64LocalLen;
    }
    else
    {
      /* ... we have. Sort out the existing zip64 extra field and remove it
       * from pZEntry->extra, re-malloc enough memory for the old extra data
       * left plus the size of the zip64 extra field */
      blocksize = SH( pOldZ64Extra + 2 );
      /* If the right length then go with it, else get rid of it and add a new extra field
       * to existing block. */
      if (blocksize == Z64LocalLen - ZIP_EF_HEADER_SIZE)
      {
        /* looks good */
        pZ64Extra = pOldZ64Extra;
      }
      else
      {
        newEFSize = pZEntry->ext - (blocksize + ZIP_EF_HEADER_SIZE) + Z64LocalLen;
        pZ64Extra = (char *) malloc( newEFSize );
        if( pZ64Extra == NULL )
          ziperr(ZE_MEM, "Zip64 Extra Field");
        /* move all before Zip64 EF */
        usTemp = (extent) (pOldZ64Extra - pZEntry->extra);
        pTemp = pZ64Extra;
        memcpy( pTemp, pZEntry->extra, usTemp );
        /* move all after old Zip64 EF */
        pTemp = pZ64Extra + usTemp;
        pOldTemp = pOldZ64Extra + ZIP_EF_HEADER_SIZE + blocksize;
        usTemp = pZEntry->ext - usTemp - blocksize;
        memcpy( pTemp, pOldTemp, usTemp);
        /* replace extra fields */
        pZEntry->ext = newEFSize;
        free(pZEntry->extra);
        pZEntry->extra = pZ64Extra;
        pZ64Extra = pTemp + usTemp;
      }
    }
  }
  /* set/update zip64 extra field members */
  write_ushort_to_mem(ZIP64_EF_TAG, pZ64Extra);
  write_ushort_to_mem((ush) (Z64LocalLen - ZIP_EF_HEADER_SIZE), pZ64Extra + 2);
  write_int64_to_mem(pZEntry->len, pZ64Extra + 2 + 2);
  write_int64_to_mem(pZEntry->siz, pZ64Extra + 2 + 2 + 8);

  return ZE_OK;
}

# endif /* ZIP64_SUPPORT */

#ifdef UNICODE_SUPPORT
/* Add UTF-8 path extra field
 * 10/11/05
 */
local int add_Unicode_Path_local_extra_field(pZEntry)
  struct zlist far *pZEntry;
{
  char  *pUExtra;
  char  *pOldUExtra;
  char  *pOldTemp;
  char  *pTemp;
#ifdef WIN32_OEM
  char  *inameLocal;
#endif
  ush   newEFSize;
  ush   usTemp;
  ush   ULen = strlen(pZEntry->uname);
  ush   blocksize;
  ulg   chksum = CRCVAL_INITIAL;
  ush   ULocalLen = ZIP_EF_HEADER_SIZE +  /* tag + EF Data Len */
                    1 +                   /* version */
                    4 +                   /* iname chksum */
                    ULen;                 /* UTF-8 path */

  /* malloc Unicode Path extra field? */
  /* after the below pUExtra should point to start of Unicode Path extra field */
  if (pZEntry->ext == 0 || pZEntry->extra == NULL)
  {
    /* get new extra field */
    pUExtra = pZEntry->extra = (char *) malloc(ULocalLen);
    if (pZEntry->extra == NULL) {
      ziperr( ZE_MEM, "UTF-8 Path local extra field" );
    }
    pZEntry->ext = ULocalLen;
  }
  else
  {
    /* check if we have a Unicode Path extra field ... */
    pOldUExtra = get_extra_field( UTF8_PATH_EF_TAG, pZEntry->extra, pZEntry->ext );
    if (pOldUExtra == NULL)
    {
      /* ... we don't, so re-malloc enough memory for the old extra data plus */
      /* the size of the UTF-8 Path extra field */
      pUExtra = (char *) malloc( ULocalLen + pZEntry->ext );
      if (pUExtra == NULL)
        ziperr( ZE_MEM, "UTF-8 Path Extra Field" );
      /* move old extra field and update pointer and length */
      memmove( pUExtra, pZEntry->extra, pZEntry->ext);
      free( pZEntry->extra );
      pZEntry->extra = pUExtra;
      pUExtra += pZEntry->ext;
      pZEntry->ext += ULocalLen;
    }
    else
    {
      /* ... we have. Sort out the existing UTF-8 Path extra field and remove it
       * from pZEntry->extra, re-malloc enough memory for the old extra data
       * left plus the size of the UTF-8 Path extra field */
      blocksize = SH( pOldUExtra + 2 );
      /* If the right length then go with it, else get rid of it and add a new extra field
       * to existing block. */
      if (blocksize == ULocalLen - ZIP_EF_HEADER_SIZE)
      {
        /* looks good */
        pUExtra = pOldUExtra;
      }
      else
      {
        newEFSize = pZEntry->ext - (blocksize + ZIP_EF_HEADER_SIZE) + ULocalLen;
        pUExtra = (char *) malloc( newEFSize );
        if( pUExtra == NULL )
          ziperr(ZE_MEM, "UTF-8 Path Extra Field");
        /* move all before UTF-8 Path EF */
        usTemp = (extent) (pOldUExtra - pZEntry->extra);
        pTemp = pUExtra;
        memcpy( pTemp, pZEntry->extra, usTemp );
        /* move all after old UTF-8 Path EF */
        pTemp = pUExtra + usTemp;
        pOldTemp = pOldUExtra + ZIP_EF_HEADER_SIZE + blocksize;
        usTemp = pZEntry->ext - usTemp - blocksize;
        memcpy( pTemp, pOldTemp, usTemp);
        /* replace extra fields */
        pZEntry->ext = newEFSize;
        free(pZEntry->extra);
        pZEntry->extra = pUExtra;
        pUExtra = pTemp + usTemp;
      }
    }
  }

  /*
   * Compute the Adler-16 checksum of iname
   */
/*
  chksum = adler16(ADLERVAL_INITIAL,
                   (uch *)(pZEntry->iname), strlen(pZEntry->iname));
*/

#ifdef WIN32_OEM
  if ((inameLocal = malloc(strlen(pZEntry->iname) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "write Unicode");
  }
  /* if oem translation done convert back for checksum */
  if ((pZEntry->vem & 0xff00) == 0) {
    /* get original */
    INTERN_TO_OEM(pZEntry->iname, inameLocal);
  } else {
    strcpy(inameLocal, pZEntry->iname);
  }
#else
# define inameLocal (pZEntry->iname)
#endif

  chksum = crc32(chksum, (uch *)(inameLocal), strlen(inameLocal));

#ifdef WIN32_OEM
  free(inameLocal);
#else
# undef inameLocal
#endif

  /* set/update UTF-8 Path extra field members */
  /* tag header */
  write_ushort_to_mem(UTF8_PATH_EF_TAG, pUExtra);
  /* data size */
  write_ushort_to_mem((ush) (ULocalLen - ZIP_EF_HEADER_SIZE), pUExtra + 2);
  /* version */
  *(pUExtra + 2 + 2) = 1;
  /* iname chksum */
  write_ulong_to_mem(chksum, pUExtra + 2 + 2 + 1);
  /* UTF-8 path */
  write_string_to_mem(pZEntry->uname, pUExtra + 2 + 2 + 1 + 4);

  return ZE_OK;
}

local int add_Unicode_Path_cen_extra_field(pZEntry)
  struct zlist far *pZEntry;
{
  char  *pUExtra;
  char  *pOldUExtra;
  char  *pOldTemp;
  char  *pTemp;
#ifdef WIN32_OEM
  char  *inameLocal;
#endif
  ush   newEFSize;
  ush   usTemp;
  ush   ULen = strlen(pZEntry->uname);
  ush   blocksize;
  ulg   chksum = CRCVAL_INITIAL;
  ush   UCenLen = ZIP_EF_HEADER_SIZE +  /* tag + EF Data Len */
                  1 +                   /* version */
                  4 +                   /* checksum */
                  ULen;                 /* UTF-8 path */

  /* malloc Unicode Path extra field? */
  /* after the below pUExtra should point to start of Unicode Path extra field */
  if (pZEntry->cext == 0 || pZEntry->cextra == NULL)
  {
    /* get new extra field */
    pUExtra = pZEntry->cextra = (char *) malloc(UCenLen);
    if (pZEntry->cextra == NULL) {
      ziperr( ZE_MEM, "UTF-8 Path cen extra field" );
    }
    pZEntry->cext = UCenLen;
  }
  else
  {
    /* check if we have a Unicode Path extra field ... */
    pOldUExtra = get_extra_field( UTF8_PATH_EF_TAG, pZEntry->cextra, pZEntry->cext );
    if (pOldUExtra == NULL)
    {
      /* ... we don't, so re-malloc enough memory for the old extra data plus */
      /* the size of the UTF-8 Path extra field */
      pUExtra = (char *) malloc( UCenLen + pZEntry->cext );
      if (pUExtra == NULL)
        ziperr( ZE_MEM, "UTF-8 Path Extra Field" );
      /* move old extra field and update pointer and length */
      memmove( pUExtra, pZEntry->cextra, pZEntry->cext);
      free( pZEntry->cextra );
      pZEntry->cextra = pUExtra;
      pUExtra += pZEntry->cext;
      pZEntry->cext += UCenLen;
    }
    else
    {
      /* ... we have. Sort out the existing UTF-8 Path extra field and remove it
       * from pZEntry->extra, re-malloc enough memory for the old extra data
       * left plus the size of the UTF-8 Path extra field */
      blocksize = SH( pOldUExtra + 2 );
      /* If the right length then go with it, else get rid of it and add a new extra field
       * to existing block. */
      if (blocksize == UCenLen - ZIP_EF_HEADER_SIZE)
      {
        /* looks good */
        pUExtra = pOldUExtra;
      }
      else
      {
        newEFSize = pZEntry->cext - (blocksize + ZIP_EF_HEADER_SIZE) + UCenLen;
        pUExtra = (char *) malloc( newEFSize );
        if( pUExtra == NULL )
          ziperr(ZE_MEM, "UTF-8 Path Extra Field");
        /* move all before UTF-8 Path EF */
        usTemp = (extent) (pOldUExtra - pZEntry->cextra);
        pTemp = pUExtra;
        memcpy( pTemp, pZEntry->cextra, usTemp );
        /* move all after old UTF-8 Path EF */
        pTemp = pUExtra + usTemp;
        pOldTemp = pOldUExtra + ZIP_EF_HEADER_SIZE + blocksize;
        usTemp = pZEntry->cext - usTemp - blocksize;
        memcpy( pTemp, pOldTemp, usTemp);
        /* replace extra fields */
        pZEntry->cext = newEFSize;
        free(pZEntry->cextra);
        pZEntry->cextra = pUExtra;
        pUExtra = pTemp + usTemp;
      }
    }
  }

  /*
   * Compute the CRC-32 checksum of iname
   */
#ifdef WIN32_OEM
  if ((inameLocal = malloc(strlen(pZEntry->iname) + 1)) == NULL) {
    ZIPERR(ZE_MEM, "write Unicode");
  }
  /* if oem translation done convert back for checksum */
  if ((pZEntry->vem & 0xff00) == 0) {
    /* get original */
    INTERN_TO_OEM(pZEntry->iname, inameLocal);
  } else {
    strcpy(inameLocal, pZEntry->iname);
  }
#else
# define inameLocal (pZEntry->iname)
#endif

  chksum = crc32(chksum, (uch *)(inameLocal), strlen(inameLocal));

#ifdef WIN32_OEM
  free(inameLocal);
#else
# undef inameLocal
#endif

  /*
   * Compute the Adler-16 checksum of iname
   */
/*
  chksum = adler16(ADLERVAL_INITIAL,
                   (uch *)(pZEntry->iname), strlen(pZEntry->iname));
*/

  /* set/update UTF-8 Path extra field members */
  /* tag header */
  write_ushort_to_mem(UTF8_PATH_EF_TAG, pUExtra);
  /* data size */
  write_ushort_to_mem((ush) (UCenLen - ZIP_EF_HEADER_SIZE), pUExtra + 2);
  /* version */
  *(pUExtra + 2 + 2) = 1;
  /* iname checksum */
  write_ulong_to_mem(chksum, pUExtra + 2 + 2 + 1);
  /* UTF-8 path */
  write_string_to_mem(pZEntry->uname, pUExtra + 2 + 2 + 1 + 4);

  return ZE_OK;
}
#endif /* def UNICODE_SUPPORT */


zoff_t ffile_size OF((FILE *));


/* 2004-12-06 SMS.
 * ffile_size() returns reliable file size or EOF.
 * May be used to detect large files in a small-file program.
 */
zoff_t ffile_size( file)
FILE *file;
{
  int sts;
  size_t siz;
  zoff_t ofs;
  char waste[ 4];

  /* Seek to actual EOF. */
  sts = zfseeko( file, 0, SEEK_END);
  if (sts != 0)
  {
    /* fseeko() failed.  (Unlikely.) */
    ofs = EOF;
  }
  else
  {
    /* Get apparent offset at EOF. */
    ofs = zftello( file);
    if (ofs < 0)
    {
      /* Offset negative (overflow).  File too big. */
      ofs = EOF;
    }
    else
    {
      /* Seek to apparent EOF offset.
         Won't be at actual EOF if offset was truncated.
      */
      sts = zfseeko( file, ofs, SEEK_SET);
      if (sts != 0)
      {
        /* fseeko() failed.  (Unlikely.) */
        ofs = EOF;
      }
      else
      {
        /* Read a byte at apparent EOF.  Should set EOF flag. */
        siz = fread( waste, 1, 1, file);
        if (feof( file) == 0)
        {
          /* Not at EOF, but should be.  File too big. */
          ofs = EOF;
        }
      }
    }
  }
  /* Seek to BOF.
   *
   * 2007-05-23 SMS.
   * Note that a problem in a prehistoric VAX C run-time library
   * requires that rewind() be used instead of fseek(), or else
   * the EOF flag is not cleared properly.
   */
  /* As WIN32 has this same problem (EOF not being cleared) when
   * NO_ZIP64_SUPPORT is set but LARGE_FILE_SUPPORT is set on a
   * small file, seems no reason not to always use rewind().
   * 8/5/07 EG
   */
#if 0
#ifdef VAXC
  sts = rewind( file);
#else /* def VAXC */
  sts = zfseeko( file, 0, SEEK_SET);
#endif /* def VAXC [else] */
#endif
  rewind(file);

  return ofs;
}


#ifndef UTIL

local void zipoddities(z)
struct zlist far *z;
{
    if ((z->vem >> 8) >= NUM_HOSTS)
    {
        sprintf(errbuf, "made by version %d.%d on system type %d: ",
                (ush)(z->vem & 0xff) / (ush)10, (ush)(z->vem & 0xff) % (ush)10,
                z->vem >> 8);
        zipwarn(errbuf, z->oname);
    }
    if (z->ver != 10 && z->ver != 11 && z->ver != 20)
    {
        sprintf(errbuf, "needs unzip %d.%d on system type %d: ",
                (ush)(z->ver & 0xff) / (ush)10,
                (ush)(z->ver & 0xff) % (ush)10, z->ver >> 8);
        zipwarn(errbuf, z->oname);
    }

    if ((fix == 2) && (z->flg != z->lflg))
    /* The comparision between central and local version of the
       "general purpose bit flag" cannot be used from scanzipf_regnew(),
       because in the "regular" zipfile processing, the local header reads
       have been postponed until the actual entry processing takes place.
       They have not yet been read when "zipoddities()" is called.
       This change was neccessary to support multivolume archives.
     */
    {
        sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ",
                z->lflg, z->flg);
        zipwarn(errbuf, z->oname);
    }
    else if (z->flg & ~0xf && (z->flg & ~0xf0) != UTF8_BIT)
    /* Only bit in high byte we support is the new UTF-8 bit */
    {
        sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg);
        zipwarn(errbuf, z->oname);
    }
    if (z->how > LAST_KNOWN_COMPMETHOD)    {
        sprintf(errbuf, "unknown compression method %u: ", z->how);
        zipwarn(errbuf, z->oname);
    }
    if (z->dsk)
    {
        sprintf(errbuf, "starts on disk %lu: ", z->dsk);
        zipwarn(errbuf, z->oname);
    }
    if (z->att!=ASCII && z->att!=BINARY && z->att!=__EBCDIC)
    {
        sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att);
        zipwarn(errbuf, z->oname);
    }
# if 0
/* This test is ridiculous, it produces an error message for almost every */
/* platform of origin other than MS-DOS, Unix, VMS, and Acorn!  Perhaps   */
/* we could test "if (z->dosflag && z->atx & ~0xffL)", but what for?      */
    if (((n = z->vem >> 8) != 3) && n != 2 && n != 13 && z->atx & ~0xffL)
    {
        sprintf(errbuf, "unknown external attributes = 0x%08lx: ", z->atx);
        zipwarn(errbuf, z->oname);
    }
# endif

    /* This test is just annoying, as Zip itself does not write the same
       extra fields to both the local and central headers.  It's much more
       complicated than this test implies.  3/17/05 */
#if 0
    if (z->ext || z->cext)
    {
# if 0
        if (z->ext && z->cext && z->extra != z->cextra)
        {
          sprintf(errbuf,
                  "local extra (%ld bytes) != central extra (%ld bytes): ",
                  (ulg)z->ext, (ulg)z->cext);
          if (noisy) fprintf(mesg, "\tzip info: %s%s\n", errbuf, z->oname);
        }
#   if (!defined(RISCOS) && !defined(CMS_MVS))
        /* in noisy mode, extra field sizes are always reported */
        else if (noisy)
#   else /* RISCOS || CMS_MVS */
/* avoid warnings for zipfiles created on the same type of OS system! */
/* or, was this warning really intended (eg. OS/2)? */
        /* Only give info if extra bytes were added by another system */
        else if (noisy && ((z->vem >> 8) != (OS_CODE >> 8)))
#   endif /* ?(RISCOS || CMS_MVS) */
# endif /* 0 */
        {
            fprintf(mesg, "zip info: %s has %ld bytes of %sextra data\n",
                    z->oname, z->ext ? (ulg)z->ext : (ulg)z->cext,
                    z->ext ? (z->cext ? "" : "local ") : "central ");
        }
    }
#endif
}


#if 0 /* scanzipf_fix() no longer used */
/*
 * scanzipf_fix is called with zip -F or zip -FF
 * read the file from front to back and pick up the pieces
 * NOTE: there are still checks missing to see if the header
 *       that was found is *VALID*
 *
 * Still much work to do so can handle more cases.  1/18/04 EG
 */
local int scanzipf_fix(f)
  FILE *f;                      /* zip file */
/*
   The name of the zip file is pointed to by the global "zipfile".  The globals
   zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
   Return an error code in the ZE_ class.
*/
{
    ulg a = 0L;                 /* attributes returned by filetime() */
    char b[CENHEAD];            /* buffer for central headers */
    ush flg;                    /* general purpose bit flag */
    int m;                      /* mismatch flag */
    extent n;                   /* length of name */
    uzoff_t p;                  /* current file offset */
    uzoff_t s;                  /* size of data, start of central */
    struct zlist far * far *x;  /* pointer last entry's link */
    struct zlist far *z;        /* current zip entry structure */

#ifndef ZIP64_SUPPORT

/* 2004-12-06 SMS.
 * Check for too-big file before doing any serious work.
 */
    if (ffile_size( f) == EOF)
      return ZE_ZIP64;

#endif /* ndef ZIP64_SUPPORT */


    /* Get any file attribute valid for this OS, to set in the central
     * directory when fixing the archive:
     */
# ifndef UTIL
    filetime(zipfile, &a, (zoff_t*)&s, NULL);
# endif
    x = &zfiles;                        /* first link */
    p = 0;                              /* starting file offset */
# ifdef HANDLE_AMIGA_SFX
    amiga_sfx_offset = 0L;
# endif

    /* Find start of zip structures */
    for (;;) {
      /* look for signature */
      while ((m = getc(f)) != EOF && m != 0x50)    /* 0x50 == 'P' */
      {
# ifdef HANDLE_AMIGA_SFX
        if (p == 0 && m == 0)
          amiga_sfx_offset = 1L;
        else if (amiga_sfx_offset) {
          if ((p == 1 && m != 0) || (p == 2 && m != 3)
                                 || (p == 3 && (uch) m != 0xF3))
            amiga_sfx_offset = 0L;
        }
# endif /* HANDLE_AMIGA_SFX */
        p++;
      }
      /* found a P */
      b[0] = (char) m;
      /* local - 11/2/03 EG */
      if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG)
        break;
      /* why search for ENDSIG if doing only local - 11/2/03 EG
      if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == ENDSIG)
        break;
      */
      /* back up */
      if (zfseeko(f, -3L, SEEK_CUR))
        return ferror(f) ? ZE_READ : ZE_EOF;
      /* move 1 byte forward */
      p++;
    }
    zipbeg = p;
# ifdef HANDLE_AMIGA_SFX
    if (amiga_sfx_offset && zipbeg >= 12 && (zipbeg & 3) == 0
        && fseek(f, -12L, SEEK_CUR) == 0 && fread(b, 12, 1, f) == 1
        && LG(b + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
      amiga_sfx_offset = zipbeg - 4;
    else
      amiga_sfx_offset = 0L;
# endif /* HANDLE_AMIGA_SFX */

    /* Read local headers */
    while (LG(b) == LOCSIG)
    {
      if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL ||
          zcount + 1 < zcount)
        return ZE_MEM;
      if (fread(b, LOCHEAD, 1, f) != 1) {
          farfree((zvoid far *)z);
          break;
      }

      z->ver = SH(LOCVER + b);
      z->vem = (ush)(dosify ? 20 : OS_CODE + Z_MAJORVER * 10 + Z_MINORVER);
      z->dosflag = dosify;
      flg = z->flg = z->lflg = SH(LOCFLG + b);
      z->how = SH(LOCHOW + b);
      z->tim = LG(LOCTIM + b);          /* time and date into one long */
      z->crc = LG(LOCCRC + b);
      z->siz = LG(LOCSIZ + b);
      z->len = LG(LOCLEN + b);
      n = z->nam = SH(LOCNAM + b);
      z->cext = z->ext = SH(LOCEXT + b);

      z->com = 0;
      z->dsk = 0;
      z->att = 0;
      z->atx = dosify ? a & 0xff : a;     /* Attributes from filetime() */
      z->mark = 0;
      z->trash = 0;

      /* attention: this one breaks the VC optimizer (Release Build) */
      /* may be fixed - 11/1/03 EG */
      s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */

      /* Initialize all fields pointing to malloced data to NULL */
      z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
      z->oname = NULL;
#ifdef UNICODE_SUPPORT
      z->uname = z->zuname = z->ouname = NULL;
#endif

      /* Link into list */
      *x = z;
      z->nxt = NULL;
      x = &z->nxt;

      /* Read file name and extra field and skip data */
      if (n == 0)
      {
        sprintf(errbuf, "%lu", (ulg)zcount + 1);
        zipwarn("zero-length name for entry #", errbuf);
# ifndef DEBUG
        return ZE_FORM;
# endif
      }
      if ((z->iname = malloc(n+1)) ==  NULL ||
          (z->ext && (z->extra = malloc(z->ext)) == NULL))
        return ZE_MEM;
      if (fread(z->iname, n, 1, f) != 1 ||
          (z->ext && fread(z->extra, z->ext, 1, f) != 1))
        return ferror(f) ? ZE_READ : ZE_EOF;

#  ifdef ZIP64_SUPPORT
      /* adjust/update siz,len and off (to come: dsk) entries */
      /* PKZIP does not care of the version set in a CDH: if  */
      /* there is a zip64 extra field assigned to a CDH PKZIP */
      /* uses it, we should do so, too.                       */
      zip64_entry = adjust_zip_local_entry(z);
      /* z->siz may be updated */
      s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */
#  endif

      if (s && zfseeko(f, (zoff_t)s, SEEK_CUR))
        return ferror(f) ? ZE_READ : ZE_EOF;
      /* If there is an extended local header, s is either 0 or
       * the correct compressed size.
       */
      z->iname[n] = '\0';               /* terminate name */
      z->zname = in2ex(z->iname);       /* convert to external name */
      if (z->zname == NULL)
        return ZE_MEM;
      z->name = z->zname;
      z->cextra = z->extra;
      if (noisy) fprintf(mesg, "zip: reading %s\n", z->zname);

      /* Save offset, update for next header */
      z->off = p;
      p += 4 + LOCHEAD + n + z->ext + s;
      zcount++;

      /* Skip extended local header if there is one */
      if ((flg & 8) != 0) {
        /* Skip the compressed data if compressed size is unknown.
         * For safety, we should use the central directory.
         */
        if (s == 0) {
          for (;;) {
            while ((m = getc(f)) != EOF && m != 0x50) ;  /* 0x50 == 'P' */
            b[0] = (char) m;
            if (fread(b+1, 15, 1, f) != 1 || LG(b) == EXTLOCSIG)
              break;
            if (zfseeko(f, -15L, SEEK_CUR))
              return ferror(f) ? ZE_READ : ZE_EOF;
          }
# ifdef ZIP64_SUPPORT
          if (zip64_entry) {        /* from extra field */
            /* all are 8 bytes */
            s = LG(4 + ZIP64_EXTSIZ + b);
          } else {
            s = LG(4 + EXTSIZ + b);
          }
# else
          s = LG(4 + EXTSIZ + b);
# endif
          p += s;
          if ((uzoff_t) zftello(f) != p+16L) {
            zipwarn("bad extended local header for ", z->zname);
            return ZE_FORM;
          }
        } else {
          /* compressed size non-zero, assume that it is valid: */
          Assert(p == zftello(f), "bad compressed size with extended header");

          if (zfseeko(f, p, SEEK_SET) || fread(b, 16, 1, f) != 1)
            return ferror(f) ? ZE_READ : ZE_EOF;
          if (LG(b) != EXTLOCSIG) {
            zipwarn("extended local header not found for ", z->zname);
            return ZE_FORM;
          }
        }
        /* overwrite the unknown values of the local header: */

        /* already in host format */
# ifdef ZIP64_SUPPORT
        z->crc = LG(4 + ZIP64_EXTCRC + b);
        z->siz = s;
        z->len = LG(4 + ZIP64_EXTLEN + b);
# else
        z->crc = LG(4 + EXTCRC + b);
        z->siz = s;
        z->len = LG(4 + EXTLEN + b);
# endif

        p += 16L;
      }
      else if (fix > 1) {
        /* Don't trust the compressed size */
        for (;;) {
          while ((m = getc(f)) != EOF && m != 0x50) p++; /* 0x50 == 'P' */
          b[0] = (char) m;
          if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == CENSIG)
            break;
          if (zfseeko(f, -3L, SEEK_CUR))
            return ferror(f) ? ZE_READ : ZE_EOF;
          p++;
        }
        s = p - (z->off + 4 + LOCHEAD + n + z->ext);
        if (s != z->siz) {
          fprintf(mesg, " compressed size %s, actual size %s for %s\n",
                  zip_fzofft(z->siz, NULL, "u"), zip_fzofft(s, NULL, "u"),
                  z->zname);
          z->siz = s;
        }
        /* next LOCSIG already read at this point, don't read it again: */
        continue;
      }

      /* Read next signature */
      if (fread(b, 4, 1, f) != 1)
          break;
    }

    s = p;                              /* save start of central */

    if (LG(b) != CENSIG && noisy) {
      fprintf(mesg, "zip warning: %s %s truncated.\n", zipfile,
              fix > 1 ? "has been" : "would be");

      if (fix == 1) {
        fprintf(mesg,
   "Retry with option -qF to truncate, with -FF to attempt full recovery\n");
        ZIPERR(ZE_FORM, NULL);
      }
    }

    cenbeg = s;

    if (zipbeg && noisy)
      fprintf(mesg, "%s: adjusting offsets for a preamble of %s bytes\n",
              zipfile, zip_fzofft(zipbeg, NULL, "u"));
    return ZE_OK;
} /* end of function scanzipf_fix() */
#endif /* never, scanzipf_fix() no longer used */

#endif /* !UTIL */

/*
 * read_local
 *
 * Read the local header assumed at in_file file pointer.
 * localz is the returned local header, z is the central directory entry.
 *
 * This is used by crypt.c.
 *
 * Return ZE code
 */
int readlocal(localz, z)
  struct zlist far **localz;
  struct zlist far *z;
{
  char buf[LOCHEAD + 1];
  struct zlist far *locz;

#ifndef UTIL
  ulg start_disk = 0;
  uzoff_t start_offset = 0;
  char *split_path;

  start_disk = z->dsk;
  start_offset = z->off;

  /* don't assume reading the right disk */

  if (start_disk != current_in_disk) {
    if (in_file) {
      fclose(in_file);
      in_file = NULL;
    }
  }

  current_in_disk = start_disk;

  /* disks are archive.z01, archive.z02, ..., archive.zip */
  split_path = get_in_split_path(in_path, current_in_disk);

  if (in_file == NULL) {
    while ((in_file = zfopen(split_path, FOPR)) == NULL) {
      /* could not open split */

      /* Ask for directory with split.  Updates in_path */
      if (ask_for_split_read_path(start_disk) != ZE_OK) {
        return ZE_ABORT;
      }
      free(split_path);
      split_path = get_in_split_path(in_path, start_disk);
    }
  }
#endif

  /* For utilities assume archive is on one disk for now */

  if (zfseeko(in_file, z->off, SEEK_SET) != 0) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("reading archive fseek: ", strerror(errno));
    return ZE_READ;
  }
  if (!at_signature(in_file, "PK\03\04")) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("Did not find entry for ", z->iname);
    return ZE_FORM;
  }

  /* read local header */
  if (fread(buf, LOCHEAD, 1, in_file) != 1) {
    int f = ferror(in_file);
    zipwarn("reading local entry: ", strerror(errno));
    fclose(in_file);
    return f ? ZE_READ : ZE_EOF;
  }

  /* Local Header
       local file header signature     4 bytes  (0x04034b50)
       version needed to extract       2 bytes
       general purpose bit flag        2 bytes
       compression method              2 bytes
       last mod file time              2 bytes
       last mod file date              2 bytes
       crc-32                          4 bytes
       compressed size                 4 bytes
       uncompressed size               4 bytes
       file name length                2 bytes
       extra field length              2 bytes

       file name (variable size)
       extra field (variable size)
   */

  if ((locz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
    zipwarn("reading entry", "");
    fclose(in_file);
    return ZE_MEM;
  }

  locz->ver = SH(LOCVER + buf);
  locz->lflg = SH(LOCFLG + buf);
  locz->how = SH(LOCHOW + buf);
  locz->tim = LG(LOCTIM + buf);          /* time and date into one long */
  locz->crc = LG(LOCCRC + buf);
  locz->nam = SH(LOCNAM + buf);
  locz->ext = SH(LOCEXT + buf);

  /* Initialize all fields pointing to malloced data to NULL */
  locz->zname = locz->name = locz->iname = locz->extra = NULL;
  locz->oname = NULL;
#ifdef UNICODE_SUPPORT
  locz->uname = NULL;
  locz->zuname = NULL;
  locz->ouname = NULL;
#endif

  /* Read file name, extra field and comment field */
  if ((locz->iname = malloc(locz->nam+1)) ==  NULL ||
      (locz->ext && (locz->extra = malloc(locz->ext)) == NULL))
    return ZE_MEM;
  if (fread(locz->iname, locz->nam, 1, in_file) != 1 ||
      (locz->ext && fread(locz->extra, locz->ext, 1, in_file) != 1))
    return ferror(in_file) ? ZE_READ : ZE_EOF;
  locz->iname[z->nam] = '\0';                  /* terminate name */
#ifdef UNICODE_SUPPORT
  if (unicode_mismatch != 3)
    read_Unicode_Path_local_entry(locz);
#endif
#ifdef WIN32
  {
    /* translate archive name from OEM if came from OEM-charset environment */
    unsigned hostver = (z->vem & 0xff);
    Ext_ASCII_TO_Native(locz->iname, (z->vem >> 8), hostver,
                        ((z->atx & 0xffff0000L) != 0), TRUE);
  }
#endif
  if ((locz->name = malloc(locz->nam+1)) ==  NULL)
    return ZE_MEM;
  strcpy(locz->name, locz->iname);

#ifdef ZIP64_SUPPORT
  zip64_entry = adjust_zip_local_entry(locz);
#endif

  /* Compare localz to z */
  if (locz->ver != z->ver) {
    sprintf(errbuf, "Local Version Needed (%d) does not match CD (%d): ", locz->ver, z->ver);
    zipwarn(errbuf, z->iname);
  }
  if (locz->lflg != z->flg) {
    zipwarn("Local Entry Flag does not match CD: ", z->iname);
  }
  if (locz->crc != z->crc) {
    zipwarn("Local Entry CRC does not match CD: ", z->iname);
  }

  /* as copying get uncompressed and compressed sizes from central directory */
  locz->len = z->len;
  locz->siz = z->siz;

  *localz = locz;

  return ZE_OK;
} /* end function readlocal() */

#if 0 /* following functions are not (no longer) used. */
/*
 * scanzipf_reg starts searching for the End Signature at the end of the file
 * The End Signature points to the Central Directory Signature which points
 * to the Local Directory Signature
 * XXX probably some more consistency checks are needed
 */
local int scanzipf_reg(f)
  FILE *f;                      /* zip file */
/*
   The name of the zip file is pointed to by the global "zipfile".  The globals
   zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
   Return an error code in the ZE_ class.
*/
{
    char b[CENHEAD];            /* buffer for central headers */
    extent n;                   /* length of name */
    struct zlist far * far *x;  /* pointer last entry's link */
    struct zlist far *z;        /* current zip entry structure */
    char *t;                    /* temporary pointer */
    char far *u;                /* temporary variable */
    int found;
    char *buf;                  /* temp buffer for reading zipfile */
# ifdef ZIP64_SUPPORT
    ulg u4;                     /* unsigned 4 byte variable */
    char bf[8];
    uzoff_t u8;                 /* unsigned 8 byte variable */
    uzoff_t censiz;             /* size of central directory */
    uzoff_t z64eocd;            /* Zip64 End Of Central Directory record byte offset */
# else
    ush flg;                    /* general purpose bit flag */
    int m;                      /* mismatch flag */
# endif
    zoff_t deltaoff = 0;


#ifndef ZIP64_SUPPORT

    /* 2004-12-06 SMS.
     * Check for too-big file before doing any serious work.
     */
    if (ffile_size( f) == EOF)
      return ZE_ZIP64;

#endif /* ndef ZIP64_SUPPORT */


    buf = malloc(4096 + 4);
    if (buf == NULL)
      return ZE_MEM;

#ifdef HANDLE_AMIGA_SFX
    amiga_sfx_offset = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0xF3030000);
    /* == 1 if this file is an Amiga executable (presumably UnZipSFX) */
#endif
    /* detect spanning signature */
    zfseeko(f, 0, SEEK_SET);
    read_split_archive = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0x08074b50L);
    found = 0;
    t = &buf[4096];
    t[1] = '\0';
    t[2] = '\0';
    t[3] = '\0';
    /* back up as much as 4k from end */
    /* zip64 support 08/31/2003 R.Nausedat */
    if (zfseeko(f, -4096L, SEEK_END) == 0) {
      zipbeg = (uzoff_t) (zftello(f) + 4096L);
      /* back up 4k blocks and look for End Of CD signature */
      while (!found && zipbeg >= 4096) {
        zipbeg -= 4096L;
        buf[4096] = t[1];
        buf[4097] = t[2];
        buf[4098] = t[3];
/*
 * XXX error check ??
 */
        fread(buf, 1, 4096, f);
        zfseeko(f, -8192L, SEEK_CUR);
        t = &buf[4095];
/*
 * XXX far pointer arithmetic in DOS
 */
        while (t >= buf) {
          /* Check for ENDSIG ("PK\5\6" in ASCII) */
          if (LG(t) == ENDSIG) {
            found = 1;
/*
 * XXX error check ??
 * XXX far pointer arithmetic in DOS
 */
            zipbeg += (uzoff_t) (t - buf);
            zfseeko(f, (zoff_t) zipbeg + 4L, SEEK_SET);
            break;
          }
          --t;
        }
      }
    }
    else
      /* file less than 4k bytes */
      zipbeg = 4096L;
/*
 * XXX warn: garbage at the end of the file ignored
 */
    if (!found && zipbeg > 0) {
      size_t s;

      zfseeko(f, 0L, SEEK_SET);
      clearerr(f);
      s = fread(buf, 1, (size_t) zipbeg, f);
      /* add 0 bytes at end */
      buf[s] = t[1];
      buf[s + 1] = t[2];
      buf[s + 2] = t[3];
      t = &buf[s - 1];
/*
 * XXX far pointer comparison in DOS
 */
      while (t >= buf) {
        /* Check for ENDSIG ("PK\5\6" in ASCII) */
        if (LG(t) == ENDSIG) {
          found = 1;
/*
 * XXX far pointer arithmetic in DOS
 */
          zipbeg = (ulg) (t - buf);
          zfseeko(f, (zoff_t) zipbeg + 4L, SEEK_SET);
          break;
        }
        --t;
      }
    }
    free(buf);
    if (!found) {
      zipwarn("missing end signature--probably not a zip file (did you", "");
      zipwarn("remember to use binary mode when you transferred it?)", "");
      return ZE_FORM;
    }

/*
 * Check for a Zip64 EOCD Locator signature - 12/10/04 EG
 */
#ifndef ZIP64_SUPPORT
    /* If Zip64 not enabled check if archive being read is Zip64 */
    /* back up 24 bytes (size of Z64 EOCDL and ENDSIG) */
    if (zfseeko(f, -24, SEEK_CUR) != 0) {
        perror("fseek");
        return ZE_FORM; /* XXX */
    }
    /* read Z64 EOCDL if there */
    if (fread(b, 20, 1, f) != 1) {
      return ZE_READ;
    }
    /* first 4 bytes are the signature if there */
    if (LG(b) == ZIP64_EOCDL_SIG) {
      zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
      zipwarn("PKZIP 4.5 or later needed - set ZIP64_SUPPORT in Zip 3", "");
      return ZE_ZIP64;
    }

    /* now should be back at the EOCD signature */
    if (fread(b, 4, 1, f) != 1) {
      zipwarn("unable to read after relative seek", "");
      return ZE_READ;
    }
    if (LG(b) != ENDSIG) {
      zipwarn("unable to relative seek in archive", "");
      return ZE_FORM;
    }
#if 0
    if (fseek(f, -4, SEEK_CUR) != 0) {
        perror("fseek");
        return ZE_FORM; /* XXX */
    }
#endif
#endif

    /* Read end header */
    if (fread(b, ENDHEAD, 1, f) != 1)
      return ferror(f) ? ZE_READ : ZE_EOF;
    if (SH(ENDDSK + b) || SH(ENDBEG + b) ||
        SH(ENDSUB + b) != SH(ENDTOT + b))
      zipwarn("multiple disk information ignored", "");
    zcomlen = SH(ENDCOM + b);
    if (zcomlen)
    {
      if ((zcomment = malloc(zcomlen)) == NULL)
        return ZE_MEM;
      if (fread(zcomment, zcomlen, 1, f) != 1)
      {
        free((zvoid *)zcomment);
        zcomment = NULL;
        return ferror(f) ? ZE_READ : ZE_EOF;
      }
#ifdef EBCDIC
      if (zcomment)
         memtoebc(zcomment, zcomment, zcomlen);
#endif /* EBCDIC */
    }
#ifdef ZIP64_SUPPORT
    /* account for Zip64 EOCD Record and Zip64 EOCD Locator */

    /* Z64 EOCDL should be just before EOCD (unless this is an empty archive) */
    cenbeg = zipbeg - ZIP64_EOCDL_OFS_SIZE;
    /* check for empty archive */
    /* changed cenbeg to uzoff_t so instead of cenbeg >= 0 use new check - 5/23/05 EG */
    if (zipbeg >= ZIP64_EOCDL_OFS_SIZE) {
      /* look for signature */
      if (zfseeko(f, cenbeg, SEEK_SET)) {
        zipwarn("end of file seeking Z64EOCDL", "");
        return ZE_FORM;
      }
      if (fread(bf, 4, 1, f) != 1) {
        ziperr(ZE_FORM, "read error");
      }
      u4 = LG(bf);
      if (u4 == ZIP64_EOCDL_SIG) {
        /* found Zip64 EOCD Locator */
        /* check for disk information */
        zfseeko(f, cenbeg + ZIP64_EOCDL_OFS_TOTALDISKS, SEEK_SET);
        if (fread(bf, 4, 1, f) != 1) {
          ziperr(ZE_FORM, "read error");
        }
        u4 = LG(bf);
        if (u4 != 1) {
          ziperr(ZE_FORM, "multiple disk archives not yet supported");
        }

        /* look for Zip64 EOCD Record */
        zfseeko(f, cenbeg + ZIP64_EOCDL_OFS_EOCD_START, SEEK_SET);
        if (fread(bf, 8, 1, f) != 1) {
         ziperr(ZE_FORM, "read error");
        }
        z64eocd = LLG(bf);
        if (zfseeko(f, z64eocd, SEEK_SET)) {
          ziperr(ZE_FORM, "error searching for Z64 EOCD Record");
        }
        if (fread(bf, 4, 1, f) != 1) {
         ziperr(ZE_FORM, "read error");
        }
        u4 = LG(bf);
        if (u4 != ZIP64_EOCD_SIG) {
          ziperr(ZE_FORM, "Z64 EOCD not found but Z64 EOCD Locator exists");
        }
        /* get size of CD */
        zfseeko(f, z64eocd + ZIP64_EOCD_OFS_SIZE, SEEK_SET);
        if (fread(bf, 8, 1, f) != 1) {
         ziperr(ZE_FORM, "read error");
        }
        censiz = LLG(bf);
        /* get start of CD */
        zfseeko(f, z64eocd + ZIP64_EOCD_OFS_CD_START, SEEK_SET);
        if (fread(bf, 8, 1, f) == (size_t) -1) {
         ziperr(ZE_FORM, "read error");
        }
        cenbeg = LLG(bf);
        u8 = z64eocd - cenbeg;
        deltaoff = adjust ? u8 - censiz : 0L;
      } else {
        /* assume no Locator and no Zip64 EOCD Record */
        censiz = LG(ENDSIZ + b);
        cenbeg = LG(b + ENDOFF);
        u8 = zipbeg - censiz;
        deltaoff = adjust ? u8 - censiz : 0L;
      }
    }
#else /* !ZIP64_SUPPORT */
/*
 * XXX assumes central header immediately precedes end header
 */
    /* start of central directory */
    cenbeg = zipbeg - LG(ENDSIZ + b);
/*
printf("start of central directory cenbeg %ld\n", cenbeg);
*/

    /* offset to first entry of archive */
    deltaoff = adjust ? cenbeg - LG(b + ENDOFF) : 0L;
#endif /* ?ZIP64_SUPPORT */

    if (zipbeg < ZIP64_EOCDL_OFS_SIZE) {
      /* zip file seems empty */
      return ZE_OK;
    }

    if (zfseeko(f, cenbeg, SEEK_SET) != 0) {
        perror("fseek");
        return ZE_FORM; /* XXX */
    }

    x = &zfiles;                        /* first link */

    if (fread(b, 4, 1, f) != 1)
      return ferror(f) ? ZE_READ : ZE_EOF;

    while (LG(b) == CENSIG) {
      /* Read central header. The portion of the central header that should
         be in common with local header is read raw, for later comparison.
         (this requires that the offset of ext in the zlist structure
         be greater than or equal to LOCHEAD) */
      if (fread(b, CENHEAD, 1, f) != 1)
        return ferror(f) ? ZE_READ : ZE_EOF;
      if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
        return ZE_MEM;
      z->vem = SH(CENVEM + b);
      for (u = (char far *)(&(z->ver)), n = 0; n < (CENNAM-CENVER); n++)
        u[n] = b[CENVER + n];
      z->nam = SH(CENNAM + b);          /* used before comparing cen vs. loc */
      z->cext = SH(CENEXT + b);         /* may be different from z->ext */
      z->com = SH(CENCOM + b);
      z->dsk = SH(CENDSK + b);
      z->att = SH(CENATT + b);
      z->atx = LG(CENATX + b);
      z->off = LG(CENOFF + b) + deltaoff;
      z->dosflag = (z->vem & 0xff00) == 0;

      /* Initialize all fields pointing to malloced data to NULL */
      z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
      z->oname = NULL;
#ifdef UNICODE_SUPPORT
      z->uname = NULL;      /* UTF-8 path */
      z->zuname = NULL;     /* Escaped local version of uname */
      z->ouname = NULL;     /* Display version of zuname */
#endif

      /* Link into list */
      *x = z;
      z->nxt = NULL;
      x = &z->nxt;

      /* Read file name, extra field and comment field */
      if (z->nam == 0)
      {
        sprintf(errbuf, "%lu", (ulg)zcount + 1);
        zipwarn("zero-length name for entry #", errbuf);
#ifndef DEBUG
        farfree((zvoid far *)z);
        return ZE_FORM;
#endif
      }
      if ((z->iname = malloc(z->nam+1)) ==  NULL ||
          (z->cext && (z->cextra = malloc(z->cext)) == NULL) ||
          (z->com && (z->comment = malloc(z->com)) == NULL))
        return ZE_MEM;
      if (fread(z->iname, z->nam, 1, f) != 1 ||
          (z->cext && fread(z->cextra, z->cext, 1, f) != 1) ||
          (z->com && fread(z->comment, z->com, 1, f) != 1))
        return ferror(f) ? ZE_READ : ZE_EOF;
      z->iname[z->nam] = '\0';                  /* terminate name */

#ifdef EBCDIC
      if (z->com)
         memtoebc(z->comment, z->comment, z->com);
#endif /* EBCDIC */

#ifdef ZIP64_SUPPORT
      /* zip64 support 08/31/2003 R.Nausedat                          */
      /* here, we have to read the len, siz etc values from the CD    */
      /* entry as we might have to adjust them regarding their        */
      /* correspronding zip64 extra fields.                           */
      /* also, we cannot compare the values from the CD entries with  */
      /* the values from the LH as they might be different.           */
      z->len = LG(CENLEN + b);
      z->siz = LG(CENSIZ + b);
      z->crc = LG(CENCRC + b);
      z->tim = LG(CENTIM + b);   /* time and date into one long */
      z->how = SH(CENHOW + b);
      z->flg = SH(CENFLG + b);
      z->ver = SH(CENVER + b);
      /* adjust/update siz,len and off (to come: dsk) entries */
      /* PKZIP does not care of the version set in a CDH: if  */
      /* there is a zip64 extra field assigned to a CDH PKZIP */
      /* uses it, we should do so, too.                       */
      adjust_zip_central_entry(z);
#endif /* ZIP64_SUPPORT */

      /* Update zipbeg offset, prepare for next header */
      if (z->off < zipbeg)
         zipbeg = z->off;
      zcount++;
      /* Read next signature */
      if (fread(b, 4, 1, f) != 1)
          return ferror(f) ? ZE_READ : ZE_EOF;
    }

    /* Point to start of header list and read local headers */
    z = zfiles;
    while (z != NULL) {
      /* Read next signature */
      if (zfseeko(f, z->off, SEEK_SET) != 0 || fread(b, 4, 1, f) != 1)
        return ferror(f) ? ZE_READ : ZE_EOF;
      if (LG(b) == LOCSIG) {
        if (fread(b, LOCHEAD, 1, f) != 1)
            return ferror(f) ? ZE_READ : ZE_EOF;

        z->lflg = SH(LOCFLG + b);
        n = SH(LOCNAM + b);
        z->ext = SH(LOCEXT + b);

        /* Compare name and extra fields */
        if (n != z->nam)
        {
#ifdef EBCDIC
          strtoebc(z->iname, z->iname);
#endif
          zipwarn("name lengths in local and central differ for ", z->iname);
          return ZE_FORM;
        }
        if ((t = malloc(z->nam)) == NULL)
          return ZE_MEM;
        if (fread(t, z->nam, 1, f) != 1)
        {
          free((zvoid *)t);
          return ferror(f) ? ZE_READ : ZE_EOF;
        }
        if (memcmp(t, z->iname, z->nam))
        {
          free((zvoid *)t);
#ifdef EBCDIC
          strtoebc(z->iname, z->iname);
#endif
          zipwarn("names in local and central differ for ", z->iname);
          return ZE_FORM;
        }
        free((zvoid *)t);
        if (z->ext)
        {
          if ((z->extra = malloc(z->ext)) == NULL)
            return ZE_MEM;
          if (fread(z->extra, z->ext, 1, f) != 1)
          {
            free((zvoid *)(z->extra));
            return ferror(f) ? ZE_READ : ZE_EOF;
          }
          if (z->ext == z->cext && memcmp(z->extra, z->cextra, z->ext) == 0)
          {
            free((zvoid *)(z->extra));
            z->extra = z->cextra;
          }
        }

#ifdef ZIP64_SUPPORT       /* zip64 support 09/02/2003 R.Nausedat */
        /*
        for now the below is left out if ZIP64_SUPPORT is defined as the fields
        len, siz and off in struct zlist are type of int64 if ZIP64_SUPPORT
        is defined. In either way, the values read from the central directory
        should be valid. comments are welcome
        */
#else /* !ZIP64_SUPPORT */
        /* Check extended local header if there is one */
        /* bit 3 */
        if ((z->lflg & 8) != 0)
        {
          char buf2[16];
          ulg s;                        /* size of compressed data */

          s = LG(LOCSIZ + b);
          if (s == 0)
            s = LG((CENSIZ-CENVER) + (char far *)(&(z->ver)));
          if (zfseeko(f, (z->off + (4+LOCHEAD) + z->nam + z->ext + s), SEEK_SET)
              || (fread(buf2, 16, 1, f) != 1))
            return ferror(f) ? ZE_READ : ZE_EOF;
          if (LG(buf2) != EXTLOCSIG)
          {
# ifdef EBCDIC
            strtoebc(z->iname, z->iname);
# endif
            zipwarn("extended local header not found for ", z->iname);
            return ZE_FORM;
          }
          /* overwrite the unknown values of the local header: */
          for (n = 0; n < 12; n++)
            b[LOCCRC+n] = buf2[4+n];
        }

        /* Compare local header with that part of central header (except
           for the reserved bits in the general purpose flags and except
           for the already checked entry name length */
        /* If I have read this right we are stepping through the z struct
           here as a byte array.  Need to fix this.  5/25/2005 EG */
        u = (char far *)(&(z->ver));
        flg = SH((CENFLG-CENVER) + u);          /* Save central flags word */
        u[CENFLG-CENVER+1] &= 0x1f;             /* Mask reserved flag bits */
        b[LOCFLG+1] &= 0x1f;
        for (m = 0, n = 0; n < LOCNAM; n++) {
          if (b[n] != u[n])
          {
            if (!m)
            {
              zipwarn("local and central headers differ for ", z->iname);
              m = 1;
            }
            if (noisy)
            {
              sprintf(errbuf, " offset %u--local = %02x, central = %02x",
                      (unsigned)n, (uch)b[n], (uch)u[n]);
              zipwarn(errbuf, "");
            }
          }
        }
        if (m && !adjust)
          return ZE_FORM;

        /* Complete the setup of the zlist entry by translating the remaining
         * central header fields in memory, starting with the fields with
         * highest offset. This order of the conversion commands takes into
         * account potential buffer overlaps caused by structure padding.
         */
        z->len = LG((CENLEN-CENVER) + u);
        z->siz = LG((CENSIZ-CENVER) + u);
        z->crc = LG((CENCRC-CENVER) + u);
        z->tim = LG((CENTIM-CENVER) + u);   /* time and date into one long */
        z->how = SH((CENHOW-CENVER) + u);
        z->flg = flg;                       /* may be different from z->lflg */
        z->ver = SH((CENVER-CENVER) + u);
#endif /* ?ZIP64_SUPPORT */

        /* Clear actions */
        z->mark = 0;
        z->trash = 0;
#ifdef UNICODE_SUPPORT
        if (unicode_mismatch != 3) {
          read_Unicode_Path_entry(z);
          if (z->uname) {
            /* match based on converted Unicode name */
            z->name = utf8_to_local_string(z->uname);
# ifdef EBCDIC
            /* z->zname is used for printing and must be coded in native charset */
            strtoebc(z->zname, z->name);
# else
            if ((z->zname = malloc(strlen(z->name) + 1)) == NULL) {
              ZIPERR(ZE_MEM, "scanzipf_reg");
            }
            strcpy(z->zname, z->name);
# endif
            z->oname = local_to_display_string(z->zname);
          } else {
            /* no UTF-8 path */
            if ((z->name = malloc(strlen(z->iname) + 1)) == NULL) {
              ZIPERR(ZE_MEM, "scanzipf_reg");
            }
            strcpy(z->name, z->iname);
            if ((z->zname = malloc(strlen(z->iname) + 1)) == NULL) {
              ZIPERR(ZE_MEM, "scanzipf_reg");
            }
            strcpy(z->zname, z->iname);
            z->oname = local_to_display_string(z->iname);
          }
        }
#else /* !UNICODE_SUPPORT */
# ifdef UTIL
/* We only need z->iname in the utils */
        z->name = z->iname;
#  ifdef EBCDIC
/* z->zname is used for printing and must be coded in native charset */
        if ((z->zname = malloc(z->nam+1)) ==  NULL)
          return ZE_MEM;
        strtoebc(z->zname, z->iname);
#  else
        z->zname = z->iname;
#  endif
# else /* !UTIL */
        z->zname = in2ex(z->iname);       /* convert to external name */
        if (z->zname == NULL)
          return ZE_MEM;
        z->name = z->zname;
# endif /* ?UTIL */
        if ((z->oname = malloc(strlen(z->zname) + 1)) == NULL) {
          ZIPERR(ZE_MEM, "scanzipf_reg");
        }
        strcpy(z->oname, z->zname);
#endif /* ?UNICODE_SUPPORT */
      }
      else {
#ifdef EBCDIC
        strtoebc(z->iname, z->iname);
#endif
        zipwarn("local header not found for ", z->iname);
        return ZE_FORM;
      }
#ifndef UTIL
      if (verbose && fix == 0)
        zipoddities(z);
#endif
      z = z->nxt;
    }

    if (zipbeg && noisy)
      fprintf(mesg, "%s: %s a preamble of %s bytes\n",
              zipfile, adjust ? "adjusting offsets for" : "found",
              zip_fzofft(zipbeg, NULL, "u"));
#ifdef HANDLE_AMIGA_SFX
    if (zipbeg < 12 || (zipbeg & 3) != 0 /* must be longword aligned */)
      amiga_sfx_offset = 0;
    else if (amiga_sfx_offset) {
      char buf2[16];
      if (!fseek(f, zipbeg - 12, SEEK_SET) && fread(buf2, 12, 1, f) == 1) {
        if (LG(buf2 + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
          /* could also check if LG(buf2) == 0xF2030000... no for now */
          amiga_sfx_offset = zipbeg - 4;
        else
          amiga_sfx_offset = 0L;
      }
    }
#endif /* HANDLE_AMIGA_SFX */
    return ZE_OK;
} /* end of function scanzipf_reg() */
#endif /* never */




/* find_next_signature
 *
 * Scan the file forward and look for the next PK signature.
 *
 * Return 1 if find one and leave file pointer pointing to next char
 * after signature and set sigbuf to signature.
 *
 * Return 0 if not.  Will be at EOF on return unless error.
 *
 */

local char sigbuf[4];   /* signature found */

#if 0 /* currently unused */
/* copy signature */
char *copy_sig(copyto, copyfrom)
  char *copyto;
  char *copyfrom;
{
  int i;

  for (i = 0; i < 4; i++) {
    copyto[i] = copyfrom[i];
  }
  return copyto;
}
#endif /* currently unused */


local int find_next_signature(f)
  FILE *f;
{
  int m;
  /*
  zoff_t here;
  */

  /* look for P K ? ? signature */

  m = getc(f);

  /*
  here = zftello(f);
  */

  while (m != EOF)
  {
    if (m == 0x50 /*'P' except EBCDIC*/) {
      /* found a P */
      sigbuf[0] = (char) m;

      if ((m = getc(f)) == EOF)
        break;
      if (m != 0x4b /*'K' except EBCDIC*/) {
        /* not a signature */
        ungetc(m, f);
      } else {
        /* found P K */
        sigbuf[1] = (char) m;

        if ((m = getc(f)) == EOF)
          break;
        if (m == 0x50 /*'P' except EBCDIC*/) {
          /* not a signature but maybe start of new one */
          ungetc(m, f);
          continue;
        } else if (m >= 16) {
          /* last 2 chars expect < 16 for signature */
          continue;
        }
        sigbuf[2] = (char) m;

        if ((m = getc(f)) == EOF)
          break;
        if (m == 0x50 /*'P' except EBCDIC*/) {
          /* not a signature but maybe start of new one */
          ungetc(m, f);
          continue;
        } else if (m >= 16) {
          /* last 2 chars expect < 16 */
          continue;
        }
        sigbuf[3] = (char) m;

        /* found possible signature */
        return 1;
      }
    }
    m = getc(f);
  }
  if (ferror(f)) {
    return 0;
  }

  /* found nothing */
  return 0;
}

/* find_signature
 *
 * Find signature.
 *
 * Return 1 if found and leave file pointing to next character
 * after signature.  Set sigbuf with signature.
 *
 * Return 0 if not found.
 */

local int find_signature(f, signature)
  FILE *f;
  ZCONST char *signature;
{
  int i;
  char sig[4];
  /*
  zoff_t here = zftello(f);
  */

  for (i = 0; i < 4; i++)
    sig[i] = signature[i];

  /* for EBCDIC */
  if (sig[0] == 'P')
    sig[0] = 0x50;
  if (sig[1] == 'K')
    sig[1] = 0x4b;

  while (!feof(f)) {
    if (!find_next_signature(f)) {
      return 0;
    } else {
      for (i = 0; i < 4; i++) {
        if (sig[i] != sigbuf[i]) {
          /* not a match */
          break;
        }
      }
      if (i == 4) {
        /* found it */
        return 1;
      }
    }
  }
  return 0;
}


/* is_signature
 *
 * Compare signatures
 *
 * Return 1 if the signatures match.
 */

local int is_signature(sig1, sig2)
  ZCONST char *sig1;
  ZCONST char *sig2;
{
  int i;
  char tsig1[4];
  char tsig2[4];

  for (i = 0; i < 4; i++) {
    tsig1[i] = sig1[i];
    tsig2[i] = sig2[i];
  }

  /* for EBCDIC */
  if (tsig1[0] == 'P')
    tsig1[0] = 0x50;
  if (tsig1[1] == 'K')
    tsig1[1] = 0x4b;

  if (tsig2[0] == 'P')
    tsig2[0] = 0x50;
  if (tsig2[1] == 'K')
    tsig2[1] = 0x4b;

  for (i = 0; i < 4; i++) {
    if (tsig1[i] != tsig2[i]) {
      /* not a match */
      break;
    }
  }
  if (i == 4) {
    /* found it */
    return 1;
  }
  return 0;
}


/* at_signature
 *
 * Is at signature in file
 *
 * Return 1 if at the signature and leave file pointing to next character
 * after signature.
 *
 * Return 0 if not.
 */

local int at_signature(f, signature)
  FILE *f;
  ZCONST char *signature;
{
  int i;
  extent m;
  char sig[4];
  char b[4];

  for (i = 0; i < 4; i++)
    sig[i] = signature[i];

  /* for EBCDIC */
  if (sig[0] == 'P')
    sig[0] = 0x50;
  if (sig[1] == 'K')
    sig[1] = 0x4b;

  m = fread(b, 1, 4, f);
  if (m != 4) {
    return 0;
  } else {
    for (i = 0; i < 4; i++) {
      if (sig[i] != b[i]) {
        /* not a match */
        break;
      }
    }
    if (i == 4) {
      /* found it */
      return 1;
    }
  }
  return 0;
}


#ifndef UTIL

local int scanzipf_fixnew()
/*
   Scan an assumed broke archive from the beginning, salvaging what can.

   Generally scanzipf_regnew() is used for reading archives normally and
   for fixing archives with a readable central directory using -F.  This
   scan is used by -FF and is for an archive that is unreadable by
   scanzipf_regnew().

   Start with the first file of the archive, either .z01 or .zip, and
   look for local entries.  Read local entries found and create zlist
   entries for them.  If we find central directory entries, read them
   and update the zlist created while reading local entries.

   The input path for the .zip file is in in_path.  If this is a multiple disk
   archive get the paths for splits from in_path as we go.  If a split is not in
   the same directory as the last split we ask the user where it is and update
   in_path.
 */
/*
   This is old:

   The name of the zip file is pointed to by the global "zipfile".  The globals
   zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
   Return an error code in the ZE_ class.
*/
{
  /* This function only reads the standard End-of-CentralDir record and the
     standard CentralDir-Entry records directly.  To conserve stack space,
     only a buffer of minimal size is declared.
   */
# if CENHEAD > ENDHEAD
#   define FIXSCAN_BUFSIZE  CENHEAD
# else
#   define FIXSCAN_BUFSIZE  ENDHEAD
# endif

  char    scbuf[FIXSCAN_BUFSIZE];  /* buffer big enough for headers */
  char   *split_path;
  ulg     eocdr_disk;
  uzoff_t eocdr_offset;

  uzoff_t current_offset = 0; /* offset before */
  uzoff_t offset = 0;         /* location after return from seek */

  int skip_disk = 0;          /* 1 if user asks to skip current disk */
  int skipped_disk = 0;       /* 1 if skipped start disk and start offset is useless */

  int r = 0;                  /* zipcopy return */
  uzoff_t s;                  /* size of data, start of central */
  struct zlist far * far *x;  /* pointer last entry's link */
  struct zlist far *z;        /* current zip entry structure */
  int plen;
  char *in_path_ext;
  int in_central_directory = 0; /* found a central directory record */
  struct zlist far *cz;
  uzoff_t cd_total_entries = 0; /* number of entries according to EOCDR */
  ulg     in_cd_start_disk;     /* central directory start disk */
  uzoff_t in_cd_start_offset;   /* offset of start of cd on cd start disk */


  total_disks = 1000000;

  /* open the zipfile */
  /* This must be .zip file, even if it doesn't exist */

  /* see if zipfile name ends in .zip */
  plen = strlen(in_path);

#ifdef VMS
  /* On VMS, adjust plen (and in_path_ext) to avoid the file version. */
  plen -= strlen(vms_file_version(in_path));
#endif /* def VMS */
  in_path_ext = zipfile + plen - 4;

  if (plen >= 4 &&
      in_path_ext[0] == '.' &&
      toupper(in_path_ext[1]) == 'Z' &&
      in_path_ext[2] >= '0' && in_path_ext[2] <= '9' &&
      in_path_ext[3] >= '0' && in_path_ext[3] <= '9' &&
      (plen == 4 || (in_path_ext[4] >= '0' && in_path_ext[4] <= '9'))) {
    /* This may be a split but not the end split */
    strcpy(errbuf, "if archive to fix is split archive, need to provide\n");
    strcat(errbuf, "      path of the last split with .zip extension,\n");
    strcat(errbuf, "      even if it doesn't exist (zip will ask for splits)");
    zipwarn(errbuf, "");
    return ZE_FORM;
  }

  if ((in_file = zfopen(in_path, FOPR)) == NULL) {
    zipwarn("could not open input archive: ", in_path);
  }
  else
  {

#ifndef ZIP64_SUPPORT
    /* 2004-12-06 SMS.
     * Check for too-big file before doing any serious work.
     */
    if (ffile_size( in_file) == EOF) {
      fclose(in_file);
      in_file = NULL;
      zipwarn("input file requires Zip64 support: ", in_path);
      return ZE_ZIP64;
    }
#endif /* ndef ZIP64_SUPPORT */

    /* look for End Of Central Directory Record */

    /* back up 64k (the max size of the EOCDR) from end */
    if (zfseeko(in_file, -0x40000L, SEEK_END) != 0) {
      /* assume file is less than 64 KB so backup to beginning */
      if (zfseeko(in_file, 0L, SEEK_SET) != 0) {
        fclose(in_file);
        in_file = NULL;
        zipwarn("unable to seek in input file ", in_path);
        return ZE_READ;
      }
    }


    /* find EOCD Record signature */
    if (!find_signature(in_file, "PK\05\06")) {
      /* No End Of Central Directory Record */
      strcpy(errbuf, "Missing end (EOCDR) signature - either this archive\n");
      strcat(errbuf, "                     is not readable or the end is damaged");
      zipwarn(errbuf, "");
    }
    else
    {
      /* at start of data after EOCDR signature */
      eocdr_offset = (uzoff_t) zftello(in_file);

      /* OK, it is possible this is not the last EOCDR signature (might be
         EOCDR signature from a stored archive in the last 64 KB) and so not
         the one we want.

         The below assumes the signature does not appear in the assumed
         ASCII text .ZIP file comment.  Even if something like UTF-8
         is stored in the comment, it's unlikely the binary \05 and \06
         will be in the comment text.
      */
      while (find_signature(in_file, "PK\05\06")) {
        eocdr_offset = (uzoff_t) zftello(in_file);
      }

      /* found EOCDR */
      /* format is
           end of central dir signature     4 bytes  (0x06054b50)
           number of this disk              2 bytes
           number of the disk with the
            start of the central directory  2 bytes
           total number of entries in the
            central directory on this disk  2 bytes
           total number of entries in
            the central directory           2 bytes
           size of the central directory    4 bytes
           offset of start of central
            directory with respect to
            the starting disk number        4 bytes
           .ZIP file comment length         2 bytes
           .ZIP file comment        (variable size)
       */

      if (zfseeko(in_file, eocdr_offset, SEEK_SET) != 0) {
        fclose(in_file);
        in_file = NULL;
        zipwarn("unable to seek in input file ", in_path);
        return ZE_READ;
      }

      /* read the EOCDR */
      s = fread(scbuf, 1, ENDHEAD, in_file);

      /* make sure we read enough bytes */
      if (s < ENDHEAD) {
        sprintf(errbuf, "End record (EOCDR) only %s bytes - assume truncated",
                  zip_fzofft(s, NULL, "u"));
        zipwarn(errbuf, "");
      }
      else
      {
        /* the first field should be number of this (the last) disk */
        eocdr_disk = (ulg)SH(scbuf);
        total_disks = eocdr_disk + 1;

        /* assume this is this disk - if Zip64 it may not be as the
           disk number may be bigger than this field can hold
        */
        current_in_disk = total_disks - 1;

        /* Central Directory disk, offset, and total entries */
        in_cd_start_disk = (ulg)SH(scbuf + 2);
        in_cd_start_offset = (uzoff_t)LG(scbuf + 12);
        cd_total_entries = (uzoff_t)SH(scbuf + 6);

        /* the in_cd_start_disk should always be less than the total_disks,
           unless the -1 flags are being used */
        if (total_disks < 0x10000 && in_cd_start_disk > total_disks) {
          zipwarn("End record (EOCDR) has bad disk numbers - ignoring EOCDR", "");
          total_disks = 0;
        }
        else
        {
          /* length of zipfile comment */
          zcomlen = SH(scbuf + ENDCOM);
          if (zcomlen)
          {
            if ((zcomment = malloc(zcomlen + 1)) == NULL)
              return ZE_MEM;
            if (fread(zcomment, zcomlen, 1, in_file) != 1)
            {
              free((zvoid *)zcomment);
              zcomment = NULL;
              zipwarn("zipfile comment truncated - ignoring", "");
            } else {
              zcomment[zcomlen] = '\0';
            }
#ifdef EBCDIC
            if (zcomment)
               memtoebc(zcomment, zcomment, zcomlen);
#endif /* EBCDIC */
          }
        }
        if (total_disks != 1)
          sprintf(errbuf, " Found end record (EOCDR) - says expect %lu splits", total_disks);
        else
          sprintf(errbuf, " Found end record (EOCDR) - says expect single disk archive");
        zipmessage(errbuf, "");
        if (zcomment)
          zipmessage("  Found archive comment", "");
      } /* good EOCDR */

    } /* found EOCDR */

    /* if total disks is other than 1 then this is not start disk */
    /* if the EOCDR is bad, total_disks is 0 */

    /* if total_disks = 0, then guess if this is a single-disk archive
       by seeing if starts with local header */

    if (total_disks == 0) {
      int issig;
      /* seek to top */
      if (zfseeko(in_file, 0, SEEK_SET) != 0) {
        fclose(in_file);
        in_file = NULL;
        zipwarn("unable to seek in input file ", in_path);
        return ZE_READ;
      }
      /* get next signature */
      issig = find_next_signature(in_file);
      if (issig) {
        current_in_offset = zftello(in_file);
        if (current_in_offset == 4 && is_signature(sigbuf, "PK\03\03")) {
          /* could be multi-disk aborted signature at top */
          /* skip */
          issig = find_next_signature(in_file);
        } else if (current_in_offset <= 4 && is_signature(sigbuf, "PK\03\03")) {
          /* multi-disk spanning signature */
          total_disks = 99999;
        }
      }
      if (issig && total_disks == 0) {
        current_in_offset = zftello(in_file);

        if (current_in_offset == 8 && is_signature(sigbuf, "PK\03\04")) {

          /* Local Header Record at top */

          printf("Is this a single-disk archive?  (y/n): ");
          fflush(stdout);

          if (fgets(errbuf, 100, stdin) != NULL) {
            if (errbuf[0] == 'y' || errbuf[0] == 'Y') {
              total_disks = 1;
              zipmessage("  Assuming single-disk archive", "");
            }
          }
        }
      }
    }
    if (!noisy)
      /* if quiet assume single-disk archive */
      total_disks = 1;

    if (total_disks == 1000000) {
      /* still don't know, so ask */
      printf("Is this a single-disk archive?  (y/n): ");
      fflush(stdout);

      if (fgets(errbuf, 100, stdin) != NULL) {
        if (errbuf[0] == 'y' || errbuf[0] == 'Y') {
          total_disks = 1;
          zipmessage("  Assuming single-disk archive", "");
        }
      }
    }
    if (total_disks == 1000000) {
      /* assume max */
      total_disks = 100000;
    }

  } /* .zip file exists */

  /* Skip reading the Zip64 EOCDL, Zip64 EOCDR, or central directory */

  /* Now read the archive starting with first disk.  Find local headers,
     create entry in zlist, then copy entry to new archive */

  /* Multi-volume file names end in .z01, .z02, ..., .z10, .zip for 11 disk archive */

  /* Unless quiet, always close the in_path disk and ask user for first disk,
     unless there is an End Of Central Directory record and that says there is
     only one disk.
     If quiet, assume the file pointed to is a single file archive to fix. */
  if (noisy && in_file) {
    fclose(in_file);
    in_file = NULL;
  }

  /* Read the archive disks - no idea how many disks there are
     since we can't trust the EOCDR and other end records
   */
  zipmessage("Scanning for entries...", "");

  for (current_in_disk = 0; current_in_disk < total_disks; current_in_disk++) {
    /* get the path for this disk */
    split_path = get_in_split_path(in_path, current_in_disk);

    /* if in_file is not NULL then in_file is already open */
    if (in_file == NULL) {
      /* open the split */
      while ((in_file = zfopen(split_path, FOPR)) == NULL) {
        int result;
        /* could not open split */

        /* Ask for directory with split.  Updates global variable in_path */
        result = ask_for_split_read_path(current_in_disk);
        if (result == ZE_ABORT) {
          zipwarn("could not find split: ", split_path);
          return ZE_ABORT;
        } else if (result == ZE_EOF) {
          zipmessage_nl("", 1);
          zipwarn("user ended reading - closing archive", "");
          return ZE_EOF;
        } else if (result == ZE_FORM) {
          /* user asked to skip this disk */
          zipmessage_nl("", 1);
          sprintf(errbuf, "skipping disk %lu ...\n", current_in_disk);
          zipwarn(errbuf, "");
          skip_disk = 1;
          break;
        }

        split_path = get_in_split_path(in_path, current_in_disk);
      }
      if (skip_disk) {
        /* skip this current disk - this works because central directory entries
           can't be split across splits */
        skip_disk = 0;
        skipped_disk = 1;
        continue;
      }
    }

    if (skipped_disk) {
      /* Not much to do here as between entries.  Entries are copied
         in zipcopy() and that has to handle missing disks while
         reading data for an entry.
       */
    }

    /* Main loop */
    /* Look for next signature and process it */
    while (find_next_signature(in_file)) {
      current_in_offset = zftello(in_file);

      if (is_signature(sigbuf, "PK\05\06")) {

        /* End Of Central Directory Record */

        sprintf(errbuf, "EOCDR found (%2lu %6s)...",
                current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
        zipmessage_nl(errbuf, 1);


      } else if (is_signature(sigbuf, "PK\06\06")) {

        /* Zip64 End Of Central Directory Record */

        sprintf(errbuf, "Zip64 EOCDR found (%2lu %6s)...",
                current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
        zipmessage_nl(errbuf, 1);


      } else if (is_signature(sigbuf, "PK\06\07")) {

        /* Zip64 End Of Central Directory Locator */

        sprintf(errbuf, "Zip64 EOCDL found (%2lu %6s)...",
                current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
        zipmessage_nl(errbuf, 1);


      } else if (is_signature(sigbuf, "PK\03\04")) {

        /* Local Header Record */


        if (verbose) {
          sprintf(errbuf, " Local (%2lu %6s):",
                  current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
          zipmessage_nl(errbuf, 0);
        }

        /* Create zlist entry.  Most will be filled in by zipcopy(). */

        if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
          zipwarn("reading central directory", "");
          return ZE_MEM;
        }

        z->vem = 0;
        z->ver = 0;
        z->flg = 0;
        z->how = 0;
        z->tim = 0;          /* time and date into one long */
        z->crc = 0;
        z->siz = 0;
        z->len = 0;
        z->nam = 0;          /* used before comparing cen vs. loc */
        z->cext = 0;         /* may be different from z->ext */
        z->com = 0;
        z->dsk = 0;
        z->att = 0;
        z->atx = 0;
        z->off = 0;
        z->dosflag = 0;

        /* Initialize all fields pointing to malloced data to NULL */
        z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
        z->oname = NULL;
#ifdef UNICODE_SUPPORT
        z->uname = z->zuname = z->ouname = NULL;
#endif

        /* Attempt to copy entry */

        r = zipcopy(z);

        if (in_central_directory) {
          sprintf(errbuf, "Entry after central directory found (%2lu %6s)...",
                  current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
          zipmessage_nl(errbuf, 1);
          in_central_directory = 0;
        }

        if (r == ZE_EOF)
          /* user said no more splits */
          break;
        else if (r == ZE_OK) {
          zcount++;
          files_total++;
          bytes_total += z->siz;

          /* Link into list */
          if (zfiles == NULL)
            /* first link */
            x = &zfiles;
          /* Link into list */
          *x = z;
          z->nxt = NULL;
          x = &z->nxt;
        }

      } else if (is_signature(sigbuf, "PK\01\02")) {

        /* Central directory header */


        /* sort the zlist */
        if (in_central_directory == 0) {
          zipmessage("Central Directory found...", "");
          /* If one or more files, sort by name */
          if (zcount)
          {
            struct zlist far * far *x;    /* pointer into zsort array */
            struct zlist far *z;          /* pointer into zfiles linked list */
            int i = 0;
            extent zl_size = zcount * sizeof(struct zlist far *);

            if (zl_size / sizeof(struct zlist far *) != zcount ||
                (x = zsort = (struct zlist far **)malloc(zl_size)) == NULL)
              return ZE_MEM;
            for (z = zfiles; z != NULL; z = z->nxt)
              x[i++] = z;
            qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);

            /* Skip Unicode searching */
          }
        }

        if (verbose) {
          sprintf(errbuf, " Cen   (%2lu %6s): ",
                  current_in_disk + 1, zip_fzofft(current_in_offset - 4, NULL, "u"));
          zipmessage_nl(errbuf, 0);
        }

        in_central_directory = 1;

        /* Read central directory entry */

        /* central directory signature */

        /* The format of a central directory record
          central file header signature   4 bytes  (0x02014b50)
          version made by                 2 bytes
          version needed to extract       2 bytes
          general purpose bit flag        2 bytes
          compression method              2 bytes
          last mod file time              2 bytes
          last mod file date              2 bytes
          crc-32                          4 bytes
          compressed size                 4 bytes
          uncompressed size               4 bytes
          file name length                2 bytes
          extra field length              2 bytes
          file comment length             2 bytes
          disk number start               2 bytes
          internal file attributes        2 bytes
          external file attributes        4 bytes
          relative offset of local header 4 bytes

          file name (variable size)
          extra field (variable size)
          file comment (variable size)
         */

        if (fread(scbuf, CENHEAD, 1, in_file) != 1) {
          zipwarn("reading central directory: ", strerror(errno));
          zipwarn("bad archive - error reading central directory", "");
          zipwarn("skipping this entry...", "");
          continue;
        }

        if ((cz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
          zipwarn("reading central directory", "");
          return ZE_MEM;
        }

        cz->vem = SH(CENVEM + scbuf);
        cz->ver = SH(CENVER + scbuf);
        cz->flg = SH(CENFLG + scbuf);
        cz->how = SH(CENHOW + scbuf);
        cz->tim = LG(CENTIM + scbuf);   /* time and date into one long */
        cz->crc = LG(CENCRC + scbuf);
        cz->siz = LG(CENSIZ + scbuf);
        cz->len = LG(CENLEN + scbuf);
        cz->nam = SH(CENNAM + scbuf);   /* used before comparing cen vs. loc */
        cz->cext = SH(CENEXT + scbuf);  /* may be different from z->ext */
        cz->com = SH(CENCOM + scbuf);
        cz->dsk = SH(CENDSK + scbuf);
        cz->att = SH(CENATT + scbuf);
        cz->atx = LG(CENATX + scbuf);
        cz->off = LG(CENOFF + scbuf);
        cz->dosflag = (cz->vem & 0xff00) == 0;

        /* Initialize all fields pointing to malloced data to NULL */
        cz->zname = cz->name = cz->iname = cz->extra = cz->cextra = NULL;
        cz->comment = cz->oname = NULL;
#ifdef UNICODE_SUPPORT
        cz->uname = cz->zuname = cz->ouname = NULL;
#endif

        /* Read file name, extra field and comment field */
        if (cz->nam == 0)
        {
          sprintf(errbuf, "%lu", (ulg)zcount + 1);
          zipwarn("zero-length name for entry #", errbuf);
          zipwarn("skipping this entry...", "");
          continue;
        }
        if ((cz->iname = malloc(cz->nam+1)) ==  NULL ||
            (cz->cext && (cz->cextra = malloc(cz->cext + 1)) == NULL) ||
            (cz->com && (cz->comment = malloc(cz->com + 1)) == NULL))
          return ZE_MEM;
        if (fread(cz->iname, cz->nam, 1, in_file) != 1 ||
            (cz->cext && fread(cz->cextra, cz->cext, 1, in_file) != 1) ||
            (cz->com && fread(cz->comment, cz->com, 1, in_file) != 1)) {
          zipwarn("error reading entry:  ", strerror(errno));
          zipwarn("skipping this entry...", "");
          continue;
        }
        cz->iname[cz->nam] = '\0';                  /* terminate name */

        /* Look up this name in zlist from local entries */
        z = zsearch(cz->iname);


        if (z && z->tim == cz->tim) {

          /* Apparently as iname and date and time match this central
             directory entry goes with this zlist entry */

          if (verbose) {
            /* cen dir name matches a local name */
            sprintf(errbuf, "updating: %s", cz->iname);
            zipmessage_nl(errbuf, 0);
          }

          if (z->crc != cz->crc) {
            sprintf(errbuf, "local (%lu) and cen (%lu) crc mismatch", z->crc, cz->crc);
            zipwarn(errbuf, "");
          }

          z->vem = cz->vem;
         /* z->ver = cz->ver; */
         /* z->flg = cz->flg; */
         /* z->how = cz->how; */
         /* z->tim = cz->tim; */          /* time and date into one long */
         /* z->crc = cz->crc; */
         /* z->siz = cz->siz; */
         /* z->len = cz->len; */
         /* z->nam = cz->nam; */          /* used before comparing cen vs. loc */
          z->cext = cz->cext;             /* may be different from z->ext */
          z->com = cz->com;
          z->cextra = cz->cextra;
          z->comment = cz->comment;
         /* z->dsk = cz->dsk; */
          z->att = cz->att;
          z->atx = cz->atx;
         /* z->off = cz->off; */
          z->dosflag = cz->dosflag;

#ifdef UNICODE_SUPPORT
          if (unicode_mismatch != 3 && z->uname == NULL) {
            if (z->flg & UTF8_BIT) {
              /* path is UTF-8 */
              if ((z->uname = malloc(strlen(z->iname) + 1)) == NULL) {
                ZIPERR(ZE_MEM, "reading archive");
              }
              strcpy(z->uname, z->iname);
            } else {
              /* check for UTF-8 path extra field */
              read_Unicode_Path_entry(z);
            }
          }
#endif

#ifdef WIN32
          /* Input path may be OEM */
          {
            unsigned hostver = (z->vem & 0xff);
            Ext_ASCII_TO_Native(z->iname, (z->vem >> 8), hostver,
                                ((z->atx & 0xffff0000L) != 0), FALSE);
          }
#endif

#ifdef EBCDIC
          if (z->com)
             memtoebc(z->comment, z->comment, z->com);
#endif /* EBCDIC */
#ifdef WIN32
          /* Comment may be OEM */
          {
            unsigned hostver = (z->vem & 0xff);
            Ext_ASCII_TO_Native(z->comment, (z->vem >> 8), hostver,
                                ((z->atx & 0xffff0000L) != 0), FALSE);
          }
#endif

#ifdef ZIP64_SUPPORT
          /* zip64 support 08/31/2003 R.Nausedat                          */
          /* here, we have to read the len, siz etc values from the CD    */
          /* entry as we might have to adjust them regarding their        */
          /* correspronding zip64 extra fields.                           */
          /* also, we cannot compare the values from the CD entries with  */
          /* the values from the LH as they might be different.           */

          /* adjust/update siz,len and off (to come: dsk) entries */
          /* PKZIP does not care of the version set in a CDH: if  */
          /* there is a zip64 extra field assigned to a CDH PKZIP */
          /* uses it, we should do so, too.                       */
  /*
          adjust_zip_central_entry(z);
   */
#endif

        /* Update zipbeg beginning of archive offset, prepare for next header */
/*
          if (z->dsk == 0 && (!zipbegset || z->off < zipbeg)) {
            zipbeg = z->off;
            zipbegset = 1;
          }
          zcount++;
 */

#ifndef UTIL
          if (verbose)
            zipoddities(z);
#endif

          current_offset = zftello(y);

          if (zfseeko(y, z->off, SEEK_SET) != 0) {
            fclose(in_file);
            in_file = NULL;
            zipwarn("writing archive seek: ", strerror(errno));
            return ZE_WRITE;
          }

          if (putlocal(z, PUTLOCAL_REWRITE) != ZE_OK)
            zipwarn("Error rewriting local header", "");

          if (zfseeko(y, current_offset, SEEK_SET) != 0) {
            fclose(in_file);
            in_file = NULL;
            zipwarn("write archive seek: ", strerror(errno));
            return ZE_WRITE;
          }
          offset = zftello(y);
          if (current_offset != offset) {
            fclose(in_file);
            in_file = NULL;
            zipwarn("seek after local: ", strerror(errno));
            return ZE_WRITE;
          }

          if (verbose)
            zipmessage_nl("", 1);

        } else {
          /* cen dir name does not match local name */
          sprintf(errbuf, "no local entry: %s", cz->iname);
          zipmessage_nl(errbuf, 1);
        }

      } else if (zfiles == NULL && is_signature(sigbuf, "PK\07\010")) {

        /* assume spanning signature at top of archive */
        if (total_disks == 1) {
          zipmessage("  Found spanning marker, but did not expect split (multi-disk) archive...", "");

        } else if (total_disks > 1) {
          zipmessage("  Found spanning marker - expected as this is split (multi-disk) archive...", "");

        } else {
          zipmessage("  Found spanning marker - could be split archive...", "");

        }

      } else {

        /* this signature shouldn't be here */
        int c;
        char errbuftemp[40];

        strcpy(errbuf, "unexpected signature ");
        for (c = 0; c < 4; c++) {
          sprintf(errbuftemp, "%02x ", sigbuf[c]);
          strcat(errbuf, errbuftemp);
        }
        sprintf(errbuftemp, "on disk %lu at %s\n", current_in_disk,
                                 zip_fzofft(current_in_offset - 4, NULL, "u"));
        strcat(errbuf, errbuftemp);
        zipwarn(errbuf, "");
        zipwarn("skipping this signature...", "");
      }


    } /* while reading file */

    /* close disk and do next disk */
    if (in_file)
      fclose(in_file);
    in_file = NULL;
    free(split_path);

    if (r == ZE_EOF)
      /* user says no more splits */
      break;

  } /* for each disk */

  return ZE_OK;

} /* end of function scanzipf_fixnew() */

#endif /* !UTIL */






/* ---------------------- */
/* New regular scan       */

/*
 * scanzipf_regnew is similar to the orignal scanzipf_reg in that it
 * reads the end of the archive and goes from there.  Unlike that
 * scan this one stops after reading the central directory and does
 * not read the local headers.  After the directory scan for new
 * files is done in zip.c the zlist created here is used to read
 * the old archive entries there.  The local headers are read using
 * readlocal() in zipcopy().
 *
 * This scan assumes the zip file is well structured.  If not it may
 * fail and the new scanzipf_fixnew should be used.
 *
 * 2006-2-4, 2007-12-10 EG
 */

local int scanzipf_regnew()
/*
   The input path for the .zip file is in in_path.  If a split archive,
   the path for each split is created from the current disk number
   and in_path.  If a split is not in the same directory as the last
   split we ask the user where it is and update in_path.
 */
/*
   This is old but more or less still applies:

   The name of the zip file is pointed to by the global "zipfile".  The globals
   zipbeg, cenbeg, zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
   Return an error code in the ZE_ class.
*/
{
  /* In this function, a local buffer is used to read in the following Zip
     structures:
      End-of-CentralDir record (EOCDR) (ENDHEAD)
      Zip64-End-of-CentralDir-Record locator (Zip64 EOCDL) (EC64LOC)
      Zip64-End-of-CentralDir record (Zip64 EOCDR) (EC64REC)
      CentralDir-Entry record (CENHEAD)
     To conserve valuable stack space, this buffer is sized to the largest
     of these structures.
   */
# if CENHEAD > ENDHEAD
#   define SCAN_BUFSIZE CENHEAD   /* CENHEAD should be the larger struct */
# else
#   define SCAN_BUFSIZE ENDHEAD
# endif

#ifdef ZIP64_SUPPORT
# if EC64REC > SCAN_BUFSIZE
#   undef SCAN_BUFSIZE
#   define SCAN_BUFSIZE EC64REC   /* EC64 record should be largest struct */
# endif
# if EC64LOC > SCAN_BUFSIZE
#   undef SCAN_BUFSIZE
#   define SCAN_BUFSIZE EC64LOC
# endif
#endif

  char    scbuf[SCAN_BUFSIZE];  /* buffer just enough for all header types */
  char   *split_path;
  ulg     eocdr_disk;
  uzoff_t eocdr_offset;
# ifdef ZIP64_SUPPORT
  ulg     z64eocdr_disk;
  uzoff_t z64eocdr_offset;
  uzoff_t z64eocdr_size;
  ush     version_made;
  ush     version_needed = 0;
  zoff_t zip64_eocdr_start;
  zoff_t z64eocdl_offset;
# endif /* def ZIP64_SUPPORT */
  uzoff_t cd_total_entries;        /* num of entries as read from (Zip64) EOCDR */
  ulg     in_cd_start_disk;        /* central directory start disk */
  uzoff_t in_cd_start_offset;      /* offset of start of cd on cd start disk */
  uzoff_t adjust_offset = 0;       /* bytes before first entry (size of sfx prefix) */
  uzoff_t cd_total_size = 0;       /* total size of cd */


  int first_CD = 1;           /* looking for first CD entry */
  int zipbegset = 0;

  int skip_disk = 0;          /* 1 if user asks to skip current disk */
  int skipped_disk = 0;       /* 1 if skipped start disk and start offset is useless */

  uzoff_t s;                  /* size of data, start of central */
  struct zlist far * far *x;  /* pointer last entry's link */
  struct zlist far *z;        /* current zip entry structure */


  /* open the zipfile */
  if ((in_file = zfopen(in_path, FOPR)) == NULL) {
    zipwarn("could not open input archive", in_path);
    return ZE_OPEN;
  }

#ifndef ZIP64_SUPPORT
  /* 2004-12-06 SMS.
   * Check for too-big file before doing any serious work.
   */
  if (ffile_size( in_file) == EOF) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("input file requires Zip64 support: ", in_path);
    return ZE_ZIP64;
  }
#endif /* ndef ZIP64_SUPPORT */

  /* look for End Of Central Directory Record */

  /* In a valid Zip archive, the EOCDR can be at most (64k-1 + ENDHEAD + 4)
     bytes (=65557 bytes) from the end of the file.
     We back up 128k, to allow some junk being appended to a Zip file.
   */
  if ((zfseeko(in_file, -0x20000L, SEEK_END) != 0) ||
      /* Some fseek() implementations (e.g. MSC 8.0 16-bit) fail to signal
         an error when seeking before the beginning of the file.
         As work-around, we check the position returned by zftello()
         for the error value -1.
       */
      (zftello(in_file) == (zoff_t)-1L)) {
    /* file is less than 128 KB so back up to beginning */
    if (zfseeko(in_file, 0L, SEEK_SET) != 0) {
      fclose(in_file);
      in_file = NULL;
      zipwarn("unable to seek in input file ", in_path);
      return ZE_READ;
    }
  }

  /* find EOCD Record signature */
  if (!find_signature(in_file, "PK\05\06")) {
    /* No End Of Central Directory Record */
    fclose(in_file);
    in_file = NULL;
    if (fix == 1) {
      zipwarn("bad archive - missing end signature", "");
      zipwarn("(If downloaded, was binary mode used?  If not, the", "");
      zipwarn(" archive may be scrambled and not recoverable)", "");
      zipwarn("Can't use -F to fix (try -FF)", "");
    } else{
      zipwarn("missing end signature--probably not a zip file (did you", "");
      zipwarn("remember to use binary mode when you transferred it?)", "");
      zipwarn("(if you are trying to read a damaged archive try -F)", "");
    }
    return ZE_FORM;
  }

  /* at start of data after EOCDR signature */
  eocdr_offset = (uzoff_t) zftello(in_file);

  /* OK, it is possible this is not the last EOCDR signature (might be
     EOCDR signature from a stored archive in the last 128 KB) and so not
     the one we want.

     The below assumes the signature does not appear in the assumed ASCII text
     .ZIP file comment.
  */
  while (find_signature(in_file, "PK\05\06")) {
    /* previous one was not the one */
    eocdr_offset = (uzoff_t) zftello(in_file);
  }

  /* found EOCDR */
  /* format is
       end of central dir signature     4 bytes  (0x06054b50)
       number of this disk              2 bytes
       number of the disk with the
        start of the central directory  2 bytes
       total number of entries in the
        central directory on this disk  2 bytes
       total number of entries in
        the central directory           2 bytes
       size of the central directory    4 bytes
       offset of start of central
        directory with respect to
        the starting disk number        4 bytes
       .ZIP file comment length         2 bytes
       .ZIP file comment        (variable size)
   */

  if (zfseeko(in_file, eocdr_offset, SEEK_SET) != 0) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("unable to seek in input file ", in_path);
    return ZE_READ;
  }

  /* read the EOCDR */
  s = fread(scbuf, 1, ENDHEAD, in_file);

  /* the first field should be number of this (the last) disk */
  eocdr_disk = (ulg)SH(scbuf);
  total_disks = eocdr_disk + 1;

  /* Assume EOCDR disk is this disk.  If a lot of disks, the Zip64 field
     may be needed and this EOCDR field could be set to the Zip64 flag
     value as the disk number may be bigger than this field can hold.
  */
  current_in_disk = total_disks - 1;

  /* Central Directory disk, offset, and total entries */
  in_cd_start_disk = (ulg)SH(scbuf + ENDBEG);
  in_cd_start_offset = (uzoff_t)LG(scbuf + ENDOFF);
  cd_total_entries = (uzoff_t)SH(scbuf + ENDTOT);
  cd_total_size = (uzoff_t)LG(scbuf + ENDSIZ);

  /* length of zipfile comment */
  zcomlen = SH(scbuf + ENDCOM);
  if (zcomlen)
  {
    if ((zcomment = malloc(zcomlen + 1)) == NULL)
      return ZE_MEM;
    if (fread(zcomment, zcomlen, 1, in_file) != 1)
    {
      free((zvoid *)zcomment);
      zcomment = NULL;
      return ferror(in_file) ? ZE_READ : ZE_EOF;
    }
    zcomment[zcomlen] = '\0';
#ifdef EBCDIC
    if (zcomment)
       memtoebc(zcomment, zcomment, zcomlen);
#endif /* EBCDIC */
  }

  if (cd_total_entries == 0) {
    /* empty archive */

    fclose(in_file);
    in_file = NULL;
    return ZE_OK;
  }

  /* if total disks is other than 1 then multi-disk archive */
  if (total_disks != 1) {
    /* zipfile name must end in .zip for split archives */
    int plen = strlen(in_path);
    char *in_path_ext;

    if (adjust) {
      zipwarn("Adjusting split archives not yet supported", "");
      return ZE_FORM;
    }

#ifdef VMS
    /* On VMS, adjust plen (and in_path_ext) to avoid the file version. */
    plen -= strlen(vms_file_version(in_path));
#endif /* def VMS */
    in_path_ext = zipfile + plen - 4;

    if (plen < 4 ||
        in_path_ext[0] != '.' ||
        toupper(in_path_ext[1]) != 'Z' ||
        toupper(in_path_ext[2]) != 'I' ||
        toupper(in_path_ext[3]) != 'P') {
      zipwarn("archive name must end in .zip for splits", "");
      fclose(in_file);
      in_file = NULL;
      return ZE_PARMS;
    }
  }

  /* if input or output are split archives, must be different archives */
  if ((total_disks != 1 || split_method) && !show_files &&
      strcmp(in_path, out_path) == 0) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("cannot update a split archive (use --out option)", "");
    return ZE_PARMS;
  }

  /* if fixing archive, input and output must be different archives */
  if (fix == 1 && strcmp(in_path, out_path) == 0) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("must use --out when fixing an archive", "");
    return ZE_PARMS;
  }


  /* Get sfx offset if adjusting. Above we made sure not split archive. */
  /* Also check for an offset if fix and single disk archive. */
  if ((fix == 1 && total_disks == 1) || adjust) {
    zoff_t cd_start;
# ifdef ZIP64_SUPPORT
    zoff_t zip64_eocdr_start;
# endif

    /* First attempt.  If the CD start offset and size are valid in the EOCDR
       (meaning they are not the Zip64 flag values that say the actual values
       are in the Zip64 EOCDR), we can use them to get the offset */
    if (in_cd_start_offset != 0xFFFFFFFF && cd_total_size != 0xFFFFFFFF) {
      /* Search for start of central directory */
      /* There still might be a Zip64 EOCDR.  This assumes if there is
         a Zip64 EOCDR, it's version 1 and 52 bytes */
      cd_start = eocdr_offset - cd_total_size - 24 - 56;
      if (zfseeko(in_file, cd_start, SEEK_SET) != 0) {
        fclose(in_file);
        in_file = NULL;
        if (fix == 1) {
          zipwarn("could not seek back to start of central directory: ", strerror(errno));
          zipwarn("(try -FF)", "");
        } else {
          zipwarn("reading archive fseek: ", strerror(errno));
        }
        return ZE_FORM;
      }
      if (find_signature(in_file, "PK\01\02")) {
        /* Should now be after first central directory header signature in archive */
        adjust_offset = zftello(in_file) - 4 - in_cd_start_offset;
      } else {
        zipwarn("central dir not where expected - could not adjust offsets", "");
        zipwarn("(try -FF)", "");
        return ZE_FORM;
      }
    } else {

      /* Second attempt.  We need the Zip64 EOCDL to get the offset */

      /*
       * Check for a Zip64 EOCD Locator signature
       */

      /* Format of Z64EOCD Locator is
           zip64 end of central dir locator
            signature                       4 bytes  (0x07064b50)
           number of the disk with the
            start of the zip64 end of
            central directory               4 bytes
           relative offset of the zip64
            end of central directory record 8 bytes
           total number of disks            4 bytes
       */

      /* back up 20 bytes from EOCDR to Z64 EOCDL */
      if (zfseeko(in_file, eocdr_offset - 24, SEEK_SET) != 0) {
        fclose(in_file);
        in_file = NULL;
        if (fix == 1) {
          zipwarn("could not seek back to Zip64 EOCDL: ", strerror(errno));
          zipwarn("(try -FF)", "");
        } else {
          zipwarn("reading archive fseek: ", strerror(errno));
        }
        return ZE_FORM;
      }
      if (at_signature(in_file, "PK\06\07"))
#ifndef ZIP64_SUPPORT
      {
        fclose(in_file);
        in_file = NULL;
        zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
        zipwarn("Need PKZIP 4.5 or later compatible zip", "");
        zipwarn("Set ZIP64_SUPPORT in Zip 3", "");
        return ZE_ZIP64;
      }
#else /* ZIP64_SUPPORT */
      {
        z64eocdl_offset = zftello(in_file) - 4;

        /* read Z64 EOCDL */
        if (fread(scbuf, EC64LOC, 1, in_file) != 1) {
          fclose(in_file);
          in_file = NULL;
          zipwarn("reading archive: ", strerror(errno));
          return ZE_READ;
        }
        /* now should be back at the EOCD signature */
        if (!at_signature(in_file, "PK\05\06")) {
          fclose(in_file);
          in_file = NULL;
          zipwarn("unable to read EOCD after seek: ", in_path);
          return ZE_READ;
        }

        /* read disk and offset to Zip64 EOCDR and total disks */
        z64eocdr_disk = LG(scbuf);
        z64eocdr_offset = LLG(scbuf + 4);
        total_disks = LG(scbuf + 12);

        /* For now no split archives */
        if (total_disks != 1) {
          zipwarn("Adjusting split archives not supported:  ", in_path);
          zipwarn("(try -FF)", "");
          return ZE_FORM;
        }

        /* go to the Zip64 EOCDR */
        if (zfseeko(in_file, z64eocdr_offset, SEEK_SET) != 0) {
          fclose(in_file);
          in_file = NULL;
          zipwarn("reading archive fseek: ", strerror(errno));
          return ZE_FORM;
        }
        /* Should be at Zip64 EOCDR signature */
        if (at_signature(in_file, "PK\06\06")) {
          /* apparently no offset */

        } else {
          /* Wasn't there, so calculate based on Zip64 EOCDL offset */

          zip64_eocdr_start = z64eocdl_offset - 24 - 56;
          if (zfseeko(in_file, zip64_eocdr_start, SEEK_SET) != 0) {
            fclose(in_file);
            in_file = NULL;
            if (fix == 1) {
              zipwarn("could not seek back to Zip64 EOCDR: ", strerror(errno));
              zipwarn("(try -FF)", "");
            } else {
              zipwarn("reading archive fseek: ", strerror(errno));
            }
            return ZE_FORM;
          }
          if (find_next_signature(in_file) && is_signature(sigbuf, "PK\06\06")) {
            /* Should now be after Zip64 EOCDR signature in archive */
            adjust_offset = zftello(in_file) - 4 - z64eocdr_offset;
          } else {
            zipwarn("Could not determine offset of entries", "");
            zipwarn("(try -FF)", "");
            return ZE_FORM;
          }
        }
      }
#endif
    }
    if (noisy) {
      if (adjust_offset) {
        sprintf(errbuf, "Zip entry offsets appear off by %s bytes - correcting...",
                        zip_fzofft(adjust_offset, NULL, NULL));
      } else {
        sprintf(errbuf, "Zip entry offsets do not need adjusting");
      }
      zipmessage(errbuf, "");
    }
  }


  /*
   * Check for a Zip64 EOCD Locator signature
   */

  /* Format of Z64EOCD Locator is
       zip64 end of central dir locator
        signature                       4 bytes  (0x07064b50)
       number of the disk with the
        start of the zip64 end of
        central directory               4 bytes
       relative offset of the zip64
        end of central directory record 8 bytes
       total number of disks            4 bytes
   */

  /* back up 20 bytes from EOCDR to Z64 EOCDL */
  if (zfseeko(in_file, eocdr_offset - 24, SEEK_SET) != 0) {
    fclose(in_file);
    in_file = NULL;
    if (fix == 1) {
      zipwarn("bad archive - could not seek back to Zip64 EOCDL: ", strerror(errno));
      zipwarn("(try -FF)", "");
    } else {
      zipwarn("reading archive fseek: ", strerror(errno));
    }
    return ZE_FORM;
  }
  if (at_signature(in_file, "PK\06\07"))
#ifndef ZIP64_SUPPORT
  {
    fclose(in_file);
    in_file = NULL;
    zipwarn("found Zip64 signature - this may be a Zip64 archive", "");
    zipwarn("Need PKZIP 4.5 or later compatible zip", "");
    zipwarn("Set ZIP64_SUPPORT in Zip 3", "");
    return ZE_ZIP64;
  }
#else /* ZIP64_SUPPORT */
  {
    z64eocdl_offset = zftello(in_file) - 4;
    /* read Z64 EOCDL */
    if (fread(scbuf, EC64LOC, 1, in_file) != 1) {
      fclose(in_file);
      in_file = NULL;
      zipwarn("reading archive: ", strerror(errno));
      return ZE_READ;
    }
    /* now should be back at the EOCD signature */
    if (!at_signature(in_file, "PK\05\06")) {
      fclose(in_file);
      in_file = NULL;
      zipwarn("unable to read EOCD after seek: ", in_path);
      return ZE_READ;
    }

    /* read disk and offset to Zip64 EOCDR and total disks */
    z64eocdr_disk = LG(scbuf);
    z64eocdr_offset = LLG(scbuf + 4) + adjust_offset;
    total_disks = LG(scbuf + 12);

    /* set the current disk */
    current_in_disk = total_disks - 1;

    /* Now need to read the Zip64 EOCD Record to get version needed
       to extract */

    if (z64eocdr_disk != total_disks - 1) {
      /* Zip64 EOCDR not on this disk */

      /* done with this disk (since apparently there are no CD entries
         on it) */
      fclose(in_file);
      in_file = NULL;

      /* get the path for the disk with the Zip64 EOCDR */
      split_path = get_in_split_path(in_path, z64eocdr_disk);

      while ((in_file = zfopen(split_path, FOPR)) == NULL) {
        /* could not open split */

        /* Ask where this split is.  This call also updates global in_path. */
        if (ask_for_split_read_path(z64eocdr_disk) != ZE_OK) {
          return ZE_ABORT;
        }
        free(split_path);
        split_path = get_in_split_path(in_path, z64eocdr_disk);
      }
      free(split_path);
    }

    current_in_disk = z64eocdr_disk;

    /* go to the Zip64 EOCDR */
    if (zfseeko(in_file, z64eocdr_offset, SEEK_SET) != 0) {
      fclose(in_file);
      in_file = NULL;
      zipwarn("reading archive fseek: ", strerror(errno));
      return ZE_FORM;
    }
    /* Should be at Zip64 EOCDR signature */
    if (!at_signature(in_file, "PK\06\06")) {
      /* Wasn't there, so calculate based on Zip64 EOCDL offset */
      zip64_eocdr_start = z64eocdl_offset - 24 - 56;
      if (zfseeko(in_file, zip64_eocdr_start, SEEK_SET) != 0) {
        fclose(in_file);
        in_file = NULL;
        if (fix == 1) {
          zipwarn("bad archive - could not seek back to Zip64 EOCDR: ", strerror(errno));
          zipwarn("(try -FF)", "");
        } else {
          zipwarn("reading archive fseek: ", strerror(errno));
        }
        return ZE_FORM;
      }
      if (find_next_signature(in_file) && is_signature(sigbuf, "PK\06\06")) {
        /* Should now be after Zip64 EOCDR signature in archive */
        adjust_offset = zftello(in_file) - 4 - z64eocdr_offset;
        zipwarn("Zip64 EOCDR not found where expected - compensating", "");
        zipwarn("(try -A to adjust offsets)", "");
      } else {
        fclose(in_file);
        in_file = NULL;
        if (fix == 1) {
          zipwarn("bad archive - Zip64 EOCDR not found in split:  ", in_path);
          zipwarn("(try -FF)", "");
        } else {
          zipwarn("Zip64 End Of Central Directory Record not found:  ", in_path);
        }
        return ZE_FORM;
      }
    }

    /*
     * Read the Z64 End Of Central Directory Record
     */

    /* The format of the Z64 EOCDR is
        zip64 end of central dir
         signature                       4 bytes  (0x06064b50)
        size of zip64 end of central
         directory record                8 bytes
        version made by                  2 bytes
        version needed to extract        2 bytes
        number of this disk              4 bytes
        number of the disk with the
         start of the central directory  4 bytes
        total number of entries in the
         central directory on this disk  8 bytes
        total number of entries in the
         central directory               8 bytes
        size of the central directory    8 bytes
        offset of start of central
         directory with respect to
         the starting disk number        8 bytes
        (version 2 of the Zip64 EOCDR has more after this)
        zip64 extensible data sector    (variable size)
     */

    /* read the first 52 bytes of the Zip64 EOCDR (we don't support
       version 2, which supports PKZip licensed features)
    */
    s = fread(scbuf, 1, EC64REC, in_file);
    if (s < EC64REC) {
      if (fix == 1) {
        zipwarn("bad archive - Zip64 EOCDR bad or truncated", "");
        zipwarn("(try -FF)", "");
      } else {
        zipwarn("Zip64 EOCD Record bad or truncated", "");
      }
      fclose(in_file);
      in_file = NULL;
      return ZE_FORM;
    }
    z64eocdr_size = LLG(scbuf);
    version_made = SH(scbuf + 8);
    version_needed = SH(scbuf + 10);
    in_cd_start_disk = LG(scbuf + 16);
    cd_total_entries = LLG(scbuf + 28);
    in_cd_start_offset = LLG(scbuf + 44) + adjust_offset;

    if (version_needed > 46) {
      int major = version_needed / 10;
      int minor = version_needed - (major * 10);
      sprintf(errbuf, "This archive requires version %d.%d", major, minor);
      zipwarn(errbuf, "");
      zipwarn("Zip currently only supports up to version 4.6 archives", "");
      zipwarn("(up to 4.5 if bzip2 is not compiled in)", "");
      if (fix == 1)
        zipwarn("If -F fails try -FF to try to salvage something", "");
      else if (fix == 2)
        zipwarn("Attempting to salvage what can", "");
      else {
        zipwarn("Try -F to attempt to read anyway", "");
        fclose(in_file);
        in_file = NULL;
        return ZE_FORM;
      }
    }
  }
#endif /* ?ZIP64_SUPPORT */

  /* Now read the central directory and create the zlist */

  /* Multi-volume file names end in .z01, .z02, ..., .z10, .zip for 11 disk archive */

  in_cd_start_offset += adjust_offset;
  cenbeg = in_cd_start_offset;
  zipbegset = 0;
  zipbeg = 0;
  first_CD = 1;

  /* if the central directory starts on other than this disk, close this disk */
  if (current_in_disk != in_cd_start_disk) {
    /* close current disk */
    fclose(in_file);
    in_file = NULL;
  }

  /* Read the disks with the central directory in order - usually the
     central directory fits on the last disk, but it doesn't have to.
   */
  for (current_in_disk = in_cd_start_disk;
       current_in_disk < total_disks;
       current_in_disk++) {
    /* get the path for this disk */
    if (current_in_disk == total_disks - 1) {
      /* last disk is archive.zip */
      if ((split_path = malloc(strlen(in_path) + 1)) == NULL) {
        zipwarn("reading archive: ", in_path);
        return ZE_MEM;
      }
      strcpy(split_path, in_path);
    } else {
      /* other disks are archive.z01, archive.z02, ... */
      split_path = get_in_split_path(in_path, current_in_disk);
    }

    /* if in_file is not NULL then in_file is already open */
    if (in_file == NULL) {
      /* open the split */
      while ((in_file = zfopen(split_path, FOPR)) == NULL) {
        int result;
        /* could not open split */

        /* Ask for directory with split.  Updates global variable in_path */
        result = ask_for_split_read_path(current_in_disk);
        if (result == ZE_ABORT) {
          zipwarn("could not find split: ", split_path);
          return ZE_ABORT;
        } else if (result == ZE_FORM) {
          /* user asked to skip this disk */
          sprintf(errbuf, "skipping disk %lu ...\n", current_in_disk);
          zipwarn(errbuf, "");
          skip_disk = 1;
          break;
        }

        if (current_in_disk == total_disks - 1) {
          /* last disk is archive.zip */
          if ((split_path = malloc(strlen(in_path) + 1)) == NULL) {
            zipwarn("reading archive: ", in_path);
            return ZE_MEM;
          }
          strcpy(split_path, in_path);
        } else {
          /* other disks are archive.z01, archive.z02, ... */
          split_path = get_in_split_path(zipfile, current_in_disk);
        }
      }
      if (skip_disk) {
        /* skip this current disk - this works because central directory entries
           can't be split across splits */
        skip_disk = 0;
        skipped_disk = 1;
        continue;
      }
    }

    if (skipped_disk) {
      /* skipped start CD disk so start searching for CD signature at start of disk */
      first_CD = 0;
    } else {
      /* seek to the first CD entry */
      if (first_CD) {
        if (zfseeko(in_file, in_cd_start_offset, SEEK_SET) != 0) {
          fclose(in_file);
          in_file = NULL;
          zipwarn("unable to seek in input file ", split_path);
          return ZE_READ;
        }
        first_CD = 0;
        x = &zfiles;                        /* first link */
      }
    }

    /* Main loop */
    /* Look for next signature and process it */
    while (find_next_signature(in_file)) {
      current_in_offset = zftello(in_file);

      if (is_signature(sigbuf, "PK\05\06")) {
        /* End Of Central Directory Record */
        /*
          fprintf(mesg, "EOCDR signature at %d / %I64d\n",
                  current_in_disk, current_in_offset - 4);
        */
        break;

      } else if (is_signature(sigbuf, "PK\06\06")) {
        /* Zip64 End Of Central Directory Record */
        /*
          fprintf(mesg, "Zip64 EOCDR signature at %d / %I64d\n",
                  current_in_disk, current_in_offset - 4);
        */
        break;

      } else if (!is_signature(sigbuf, "PK\01\02")) {
        /* Not Central Directory Record */

        /* this signature shouldn't be here */
        if (fix == 1) {
          int c;
          char errbuftemp[40];

          strcpy(errbuf, "bad archive - unexpected signature ");
          for (c = 0; c < 4; c++) {
            sprintf(errbuftemp, "%02x ", sigbuf[c]);
            strcat(errbuf, errbuftemp);
          }
          sprintf(errbuftemp, "on disk %lu at %s\n", current_in_disk,
                                   zip_fzofft(current_in_offset - 4, NULL, "u"));
          strcat(errbuf, errbuftemp);
          zipwarn(errbuf, "");
          zipwarn("skipping this signature...", "");
          continue;
        } else {
          sprintf(errbuf, "unexpected signature on disk %lu at %s\n",
                  current_in_disk, zip_fzofft(current_in_offset - 4, NULL, "u"));
          zipwarn(errbuf, "");
          zipwarn("archive not in correct format: ", split_path);
          zipwarn("(try -F to attempt recovery)", "");
          fclose(in_file);
          in_file = NULL;
          return ZE_FORM;
        }
      }

      /* central directory signature */
      if (verbose && fix == 1) {
        fprintf(mesg, "central directory header signature on disk %lu at %s\n",
                current_in_disk, zip_fzofft(current_in_offset - 4, NULL, "u"));
      }

      /* The format of a central directory record
        central file header signature   4 bytes  (0x02014b50)
        version made by                 2 bytes
        version needed to extract       2 bytes
        general purpose bit flag        2 bytes
        compression method              2 bytes
        last mod file time              2 bytes
        last mod file date              2 bytes
        crc-32                          4 bytes
        compressed size                 4 bytes
        uncompressed size               4 bytes
        file name length                2 bytes
        extra field length              2 bytes
        file comment length             2 bytes
        disk number start               2 bytes
        internal file attributes        2 bytes
        external file attributes        4 bytes
        relative offset of local header 4 bytes

        file name (variable size)
        extra field (variable size)
        file comment (variable size)
       */

      if (fread(scbuf, CENHEAD, 1, in_file) != 1) {
        zipwarn("reading central directory: ", strerror(errno));
        if (fix == 1) {
          zipwarn("bad archive - error reading central directory", "");
          zipwarn("skipping this entry...", "");
          continue;
        } else {
          return ferror(in_file) ? ZE_READ : ZE_EOF;
        }
      }

      if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
        zipwarn("reading central directory", "");
        return ZE_MEM;
      }

      z->vem = SH(CENVEM + scbuf);
      z->ver = SH(CENVER + scbuf);
      z->flg = SH(CENFLG + scbuf);
      z->how = SH(CENHOW + scbuf);
      z->tim = LG(CENTIM + scbuf);      /* time and date into one long */
      z->crc = LG(CENCRC + scbuf);
      z->siz = LG(CENSIZ + scbuf);
      z->len = LG(CENLEN + scbuf);
      z->nam = SH(CENNAM + scbuf);      /* used before comparing cen vs. loc */
      z->cext = SH(CENEXT + scbuf);     /* may be different from z->ext */
      z->com = SH(CENCOM + scbuf);
      z->dsk = SH(CENDSK + scbuf);
      z->att = SH(CENATT + scbuf);
      z->atx = LG(CENATX + scbuf);
      z->off = LG(CENOFF + scbuf);      /* adjust_offset is added below */
      z->dosflag = (z->vem & 0xff00) == 0;

      /* Initialize all fields pointing to malloced data to NULL */
      z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
      z->oname = NULL;
#ifdef UNICODE_SUPPORT
      z->uname = z->zuname = z->ouname = NULL;
#endif

      /* Read file name, extra field and comment field */
      if (z->nam == 0)
      {
        sprintf(errbuf, "%lu", (ulg)zcount + 1);
        zipwarn("zero-length name for entry #", errbuf);
        if (fix == 1) {
          zipwarn("skipping this entry...", "");
          continue;
        }
#ifndef DEBUG
        return ZE_FORM;
#endif
      }
      if ((z->iname = malloc(z->nam+1)) ==  NULL ||
          (z->cext && (z->cextra = malloc(z->cext)) == NULL) ||
          (z->com && (z->comment = malloc(z->com)) == NULL))
        return ZE_MEM;
      if (fread(z->iname, z->nam, 1, in_file) != 1 ||
          (z->cext && fread(z->cextra, z->cext, 1, in_file) != 1) ||
          (z->com && fread(z->comment, z->com, 1, in_file) != 1)) {
        if (fix == 1) {
          zipwarn("error reading entry:  ", strerror(errno));
          zipwarn("skipping this entry...", "");
          continue;
        }
        return ferror(in_file) ? ZE_READ : ZE_EOF;
      }
      z->iname[z->nam] = '\0';                  /* terminate name */
#ifdef UNICODE_SUPPORT
      if (unicode_mismatch != 3) {
        if (z->flg & UTF8_BIT) {
          char *iname;
          /* path is UTF-8 */
          if ((z->uname = malloc(strlen(z->iname) + 1)) == NULL) {
            zipwarn("could not allocate memory: scanzipf_reg", "");
            return ZE_MEM;
          }
          strcpy(z->uname, z->iname);
          /* Create a local name.  If UTF-8 system this should also be UTF-8 */
          iname = utf8_to_local_string(z->uname);
          if (iname) {
            free(z->iname);
            z->iname = iname;
          }
          else
            zipwarn("illegal UTF-8 name: ", z->uname);
        } else {
          /* check for UTF-8 path extra field */
          read_Unicode_Path_entry(z);
        }
      }
#endif

#ifdef WIN32
      /* Input path may be OEM */
      {
        unsigned hostver = (z->vem & 0xff);
        Ext_ASCII_TO_Native(z->iname, (z->vem >> 8), hostver,
                            ((z->atx & 0xffff0000L) != 0), FALSE);
      }
#endif

#ifdef EBCDIC
      if (z->com)
         memtoebc(z->comment, z->comment, z->com);
#endif /* EBCDIC */
#ifdef WIN32
      /* Comment may be OEM */
      {
        unsigned hostver = (z->vem & 0xff);
        Ext_ASCII_TO_Native(z->comment, (z->vem >> 8), hostver,
                            ((z->atx & 0xffff0000L) != 0), FALSE);
      }
#endif

#ifdef ZIP64_SUPPORT
      /* zip64 support 08/31/2003 R.Nausedat                          */
      /* here, we have to read the len, siz etc values from the CD    */
      /* entry as we might have to adjust them regarding their        */
      /* correspronding zip64 extra fields.                           */
      /* also, we cannot compare the values from the CD entries with  */
      /* the values from the LH as they might be different.           */

      /* adjust/update siz,len and off (to come: dsk) entries */
      /* PKZIP does not care of the version set in a CDH: if  */
      /* there is a zip64 extra field assigned to a CDH PKZIP */
      /* uses it, we should do so, too.                       */
      adjust_zip_central_entry(z);
#endif
      /* if adjusting for sfx prefix, add the offset */
      if ((fix ==1 && total_disks == 1) || adjust) z->off += adjust_offset;

      /* Update zipbeg beginning of archive offset, prepare for next header */
      if (z->dsk == 0 && (!zipbegset || z->off < zipbeg)) {
        zipbeg = z->off;
        zipbegset = 1;
      }
      zcount++;

      /* Clear actions */
      z->mark = 0;
      z->trash = 0;
#if defined(UNICODE_SUPPORT) && !defined(UTIL)
      z->zname = in2ex(z->iname);       /* convert to external name */
      if (z->zname == NULL)
        return ZE_MEM;
      if ((z->name = malloc(strlen(z->zname) + 1)) == NULL) {
        zipwarn("could not allocate memory: scanzipf_reg", "");
        return ZE_MEM;
      }
      strcpy(z->name, z->zname);
      z->oname = local_to_display_string(z->iname);

# ifdef WIN32
      z->namew = NULL;
      z->inamew = NULL;
      z->znamew = NULL;
# endif

      if (unicode_mismatch != 3) {
        if (z->uname) {
          /* create zuname which is alternate zname for matching based on
             converted Unicode name */
          char *name;

          /* Convert UTF-8 to current local character set */
          name = utf8_to_local_string(z->uname);

          if (name == NULL) {
            /*
            zipwarn("illegal UTF-8 name: ", z->uname);
            */
            /* not able to convert name, so use iname */
            if ((name = malloc(strlen(z->iname) + 1)) == NULL) {
              zipwarn("could not allocate memory: scanzipf_reg", "");
              return ZE_MEM;
            }
            strcpy(name, z->iname);
          }

# ifdef EBCDIC
          /* z->zname is used for printing and must be coded in native charset */
          strtoebc(z->zuname, name);
# else /* !EBCDIC */
          if ((z->zuname = malloc(strlen(name) + 1)) == NULL) {
            zipwarn("could not allocate memory: scanzipf_reg", "");
            return ZE_MEM;
          }
          strcpy(z->zuname, name);
          /* For output to terminal */
          if (unicode_escape_all) {
            char *ouname;
            /* Escape anything not 7-bit ASCII */
            ouname = utf8_to_escape_string(z->uname);
            if (ouname)
              z->ouname = ouname;
            else {
              if ((z->ouname = malloc(strlen(name) + 1)) == NULL) {
                zipwarn("could not allocate memory: scanzipf_reg", "");
                return ZE_MEM;
              }
              strcpy(z->ouname, name);
            }
          } else {
            if ((z->ouname = malloc(strlen(name) + 1)) == NULL) {
              zipwarn("could not allocate memory: scanzipf_reg", "");
              return ZE_MEM;
            }
            strcpy(z->ouname, name);
          }
#  ifdef WIN32

          if (!no_win32_wide) {
            z->inamew = utf8_to_wchar_string(z->uname);
            z->znamew = in2exw(z->inamew); /* convert to external name */
            if (z->znamew == NULL)
              return ZE_MEM;
          }

          local_to_oem_string(z->ouname, z->ouname);
          /* For matching.  There seems to be something lost
             in the translation from displaying a name in a
             console window using zip -su on Win32 and using
             that name in a command line to match what's in
             the archive.  This is klugy though.
          */
          if ((z->wuname = malloc(strlen(z->ouname) + 1)) == NULL) {
            zipwarn("could not allocate memory: scanzipf_reg", "");
            return ZE_MEM;
          }
          strcpy(z->wuname, z->ouname);
          oem_to_local_string(z->wuname, z->wuname);
#  endif /* WIN32 */
# endif /* ?EBCDIC */
        } else {
          /* no uname */
# ifdef WIN32
          if (!no_win32_wide) {
            z->inamew = local_to_wchar_string(z->iname);
            z->znamew = in2exw(z->inamew); /* convert to external name */
            if (z->znamew == NULL)
              return ZE_MEM;
          }
# endif
        }
      }
#else /* !(UNICODE_SUPPORT && !UTIL) */
# ifdef UTIL
/* We only need z->iname in the utils */
      z->name = z->iname;
#  ifdef EBCDIC
/* z->zname is used for printing and must be coded in native charset */
      if ((z->zname = malloc(z->nam+1)) ==  NULL) {
        zipwarn("could not allocate memory: scanzipf_reg", "");
        return ZE_MEM;
      }
      strtoebc(z->zname, z->iname);
#  else
      z->zname = z->iname;
#  endif
# else /* !UTIL */
      z->zname = in2ex(z->iname);       /* convert to external name */
      if (z->zname == NULL)
        return ZE_MEM;
      z->name = z->zname;
# endif /* ?UTIL */
      if ((z->oname = malloc(strlen(z->zname) + 1)) == NULL) {
        zipwarn("could not allocate memory: scanzipf_reg", "");
        return ZE_MEM;
      }
      strcpy(z->oname, z->zname);
#endif /* ?(UNICODE_SUPPORT && !UTIL) */

#ifndef UTIL
      if (verbose && fix == 0)
        zipoddities(z);
#endif

      /* Link into list */
      *x = z;
      z->nxt = NULL;
      x = &z->nxt;

    } /* while reading file */

    /* close disk and do next disk */
    fclose(in_file);
    in_file = NULL;
    free(split_path);

    if (!is_signature(sigbuf, "PK\01\02")) {
      /* if the last signature is not a CD signature and we get here then
         hit either the  Zip64 EOCDR or the EOCDR and done */
      break;
    }

  } /* for each disk */

  if (zcount != cd_total_entries) {
    sprintf(errbuf, "expected %s entries but found %s",
      zip_fzofft(cd_total_entries, NULL, "u"),
      zip_fzofft(zcount, NULL, "u"));
    zipwarn(errbuf, "");
    return ZE_FORM;
  }

  return ZE_OK;

} /* end of function scanzipf_regnew() */








/* ---------------------- */




/*
 * readzipfile initializes the global variables that hold the zipfile
 * directory info and opens the zipfile. For the actual zipfile scan,
 * the subroutine scanzipf_reg() or scanzipf_fix() is called,
 * depending on the mode of operation (regular processing, or zipfix mode).
 */
int readzipfile()
/*
   The name of the zip file is pointed to by the global "zipfile".
   The globals zipbeg, zfiles, zcount, and zcomlen are initialized.
   Return an error code in the ZE_ class.
*/
{
  FILE *f;              /* zip file */
  int retval;           /* return code */
  int readable;         /* 1 if zipfile exists and is readable */

  /* Initialize zip file info */
  zipbeg = 0;
  zfiles = NULL;                        /* Points to first header */
  zcount = 0;                           /* number of files */
  zcomlen = 0;                          /* zip file comment length */
  retval = ZE_OK;
  f = NULL;                             /* shut up some compilers */
  zipfile_exists = 0;

  /* If zip file exists, read headers and check structure */
#ifdef VMS
  if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-"))
    return ZE_OK;
  {
    int rtype;

    if ((VMSmunch(zipfile, GET_RTYPE, (char *)&rtype) == RMS$_NORMAL) &&
        (rtype == FAT$C_VARIABLE)) {
      fprintf(mesg,
     "\n     Error:  zipfile is in variable-length record format.  Please\n\
     run \"bilf b %s\" to convert the zipfile to fixed-length\n\
     record format.\n\n", zipfile);
      return ZE_FORM;
    }
  }
  readable = ((f = zfopen(zipfile, FOPR)) != NULL);
#else /* !VMS */
  readable = (zipfile != NULL && *zipfile && strcmp(zipfile, "-"));
  if (readable) {
    readable = ((f = zfopen(zipfile, FOPR)) != NULL);
  }
#endif /* ?VMS */

  /* skip check if streaming */
  if (!readable) {
    if (!zip_to_stdout && fix != 2 && strcmp(in_path, out_path)) {
      /* If -O used then in_path must exist */
      if (fix == 1)
        zipwarn("No .zip file found\n        ",
                "(If all you have are splits (.z01, .z02, ...) and no .zip, try -FF)");
      ZIPERR(ZE_OPEN, zipfile);
    }
  } else {
    zipfile_exists = 1;
  }

#ifdef MVS
  /* Very nasty special case for MVS.  Just because the zipfile has been
   * opened for reading does not mean that we can actually read the data.
   * Typical JCL to create a zipfile is
   *
   * //ZIPFILE  DD  DISP=(NEW,CATLG),DSN=prefix.ZIP,
   * //             SPACE=(CYL,(10,10))
   *
   * That creates a VTOC entry with an end of file marker (DS1LSTAR) of zero.
   * Alas the VTOC end of file marker is only used when the file is opened in
   * append mode.  When a file is opened in read mode, the "other" end of file
   * marker is used, a zero length data block signals end of file when reading.
   * With a brand new file which has not been written to yet, it is undefined
   * what you read off the disk.  In fact you read whatever data was in the same
   * disk tracks before the zipfile was allocated.  You would be amazed at the
   * number of application programmers who still do not understand this.  Makes
   * for interesting and semi-random errors, GIGO.
   *
   * Newer versions of SMS will automatically write a zero length block when a
   * file is allocated.  However not all sites run SMS or they run older levels
   * so we cannot rely on that.  The only safe thing to do is close the file,
   * open in append mode (we already know that the file exists), close it again,
   * reopen in read mode and try to read a data block.  Opening and closing in
   * append mode will write a zero length block where DS1LSTAR points, making
   * sure that the VTOC and internal end of file markers are in sync.  Then it
   * is safe to read data.  If we cannot read one byte of data after all that,
   * it is a brand new zipfile and must not be read.
   */
  if (readable)
  {
    char c;
    fclose(f);
    /* append mode */
    if ((f = zfopen(zipfile, "ab")) == NULL) {
      ZIPERR(ZE_OPEN, zipfile);
    }
    fclose(f);
    /* read mode again */
    if ((f = zfopen(zipfile, FOPR)) == NULL) {
      ZIPERR(ZE_OPEN, zipfile);
    }
    if (fread(&c, 1, 1, f) != 1) {
      /* no actual data */
      readable = 0;
      fclose(f);
    }
    else{
      fseek(f, 0, SEEK_SET);  /* at least one byte in zipfile, back to the start */
    }
  }
#endif /* MVS */

  /* ------------------------ */
  /* new file read */



#ifndef UTIL
  if (fix == 2) {
    scanzipf_fixnew();
  }
  else
#endif
  if (readable)
  {
    /* close file as the new scan opens the splits as needed */
    fclose(f);
# ifndef UTIL
    retval = (fix == 2 && !adjust) ? scanzipf_fixnew() : scanzipf_regnew();
# else
    retval = scanzipf_regnew();
# endif
  }

  if (fix != 2 && readable)
  {
    /* If one or more files, sort by name */
    if (zcount)
    {
      struct zlist far * far *x;    /* pointer into zsort array */
      struct zlist far *z;          /* pointer into zfiles linked list */
      extent zl_size = zcount * sizeof(struct zlist far *);

      if (zl_size / sizeof(struct zlist far *) != zcount ||
          (x = zsort = (struct zlist far **)malloc(zl_size)) == NULL)
        return ZE_MEM;
      for (z = zfiles; z != NULL; z = z->nxt)
        *x++ = z;
      qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);

#ifdef UNICODE_SUPPORT
      /* sort by zuname (local conversion of UTF-8 name) */
      if (zl_size / sizeof(struct zlist far *) != zcount ||
          (x = zusort = (struct zlist far **)malloc(zl_size)) == NULL)
        return ZE_MEM;
      for (z = zfiles; z != NULL; z = z->nxt)
        *x++ = z;
      qsort((char *)zusort, zcount, sizeof(struct zlist far *), zuqcmp);
#endif
    }
  }

  /* ------------------------ */

  return retval;
} /* end of function readzipfile() */


int putlocal(z, rewrite)
  struct zlist far *z;    /* zip entry to write local header for */
  int rewrite;            /* did seek to rewrite */
/* Write a local header described by *z to file *f.  Return an error code
   in the ZE_ class. */
{
  /* If any of compressed size (siz), uncompressed size (len), offset(off), or
     disk number (dsk) is larger than can fit in the below standard fields then a
     Zip64 flag value is stored and a Zip64 extra field is created.
     Only siz and len are in the local header while all can be in the central
     directory header.

     For the local header if the extra field is created must store both
     uncompressed and compressed sizes.

     This assumes that for large entries the compressed size won't need a
     Zip64 extra field if the uncompressed size did not.  This assumption should
     only fail for a large file of nearly totally uncompressable data.

     If streaming stdin in and use_descriptors is set then always create a Zip64
     extra field flagging the data descriptor as being in Zip64 format.  This is
     needed as don't know if need Zip64 or not when need to set Zip64 flag in
     local header.

     If rewrite is set then don't count bytes written for splits
   */
  char *block = NULL;   /* mem block to write to */
  extent offset = 0;    /* offset into block */
  extent blocksize = 0; /* size of block */
#ifdef UNICODE_SUPPORT
  ush nam = z->nam;     /* size of name to write to header */
  int use_uname = 0;    /* write uname to header */
#endif
#ifdef ZIP64_SUPPORT
  int streaming_in = 0; /* streaming stdin */
  int was_zip64 = 0;

  /* If input is stdin then streaming stdin.  No problem with that.

     The problem is updating the local header data in the output once the sizes
     and crc are known.  If the output is not seekable, then need data descriptors
     and also need to assume Zip64 will be needed as don't know yet.  Even if the
     output is seekable, if the input is streamed need to write the Zip64 extra field
     before writing the data or there won't be room for it later if we need it.
  */
  streaming_in = (strcmp(z->name, "-") == 0);

  if (!rewrite) {
    zip64_entry = 0;
    /* initial local header */
    if (z->siz > ZIP_UWORD32_MAX || z->len > ZIP_UWORD32_MAX ||
      force_zip64 == 1 || (force_zip64 != 0 && streaming_in))
    {
      /* assume Zip64 */
      if (force_zip64 == 0) {
        zipwarn("Entry too big:", z->oname);
        ZIPERR(ZE_BIG, "Large entry support disabled with -fz- but needed");
      }
      zip64_entry = 1;        /* header of this entry has a field needing Zip64 */
      if (z->ver < ZIP64_MIN_VER)
        z->ver = ZIP64_MIN_VER;
      was_zip64 = 1;
    }
  } else {
    /* rewrite */
    was_zip64 = zip64_entry;
    zip64_entry = 0;
    if (z->siz > ZIP_UWORD32_MAX || z->len > ZIP_UWORD32_MAX ||
      force_zip64 == 1 || (force_zip64 != 0 && streaming_in))
    {
      /* Zip64 entry */
      zip64_entry = 1;
    }
    if (force_zip64 == 0 && zip64_entry) {
      /* tried to force into standard entry but needed Zip64 entry */
      zipwarn("Entry too big:", z->oname);
      ZIPERR(ZE_BIG, "Large entry support disabled with -fz- but entry needs");
    }
    /* Normally for a large archive if the input file is less than 4 GB then
       the compressed or stored version should be less than 4 GB.  If this
       assumption is wrong this catches it.  This is a problem even if not
       streaming as the Zip64 extra field was not written and now there's no
       room for it. */
    if (was_zip64 == 0 && zip64_entry == 1) {
      /* guessed wrong and need Zip64 */
      zipwarn("Entry too big:", z->oname);
      if (force_zip64 == 0) {
        ZIPERR(ZE_BIG, "Compressed/stored entry unexpectedly large - do not use -fz-");
      } else {
        ZIPERR(ZE_BIG, "Poor compression resulted in unexpectedly large entry - try -fz");
      }
    }
    if (zip64_entry) {
      /* Zip64 entry still */
      /* this archive needs Zip64 (version 4.5 unzipper) */
      zip64_archive = 1;
      if (z->ver < ZIP64_MIN_VER)
        z->ver = ZIP64_MIN_VER;
    } else {
      /* it turns out we do not need Zip64 */
      zip64_entry = 0;
    }
    if (was_zip64 && zip64_entry != 1) {
      z->ver = 20;
    }
  }


#endif /* ZIP64_SUPPORT */

  /* Instead of writing to the file as we go, to do splits we have to write it
     to memory and see if it will fit before writing the entire local header.
     If the local header doesn't fit we need to save it for the next disk.
   */

#ifdef ZIP64_SUPPORT
  if (zip64_entry || was_zip64)
    /* update extra field */
    add_local_zip64_extra_field( z );
#endif /* ZIP64_SUPPORT */

#ifdef UNICODE_SUPPORT
# if 0
  /* if UTF-8 bit is set on an existing entry, assume it should be */
  /* clear the UTF-8 flag */
  z->flg &= ~UTF8_BIT;
  z->lflg &= ~UTF8_BIT;
# endif

  if (z->uname) {
    /* need UTF-8 name */
    if (utf8_force || using_utf8) {
      z->lflg |= UTF8_BIT;
      z->flg |= UTF8_BIT;
    }
    if (z->flg & UTF8_BIT) {
      /* If this flag is set, then restore UTF-8 as path name */
      use_uname = 1;
      nam = strlen(z->uname);
    } else {
      /* use extra field */
      add_Unicode_Path_local_extra_field(z);
    }
  } else {
    /* clear UTF-8 bit as not needed */
    z->flg &= ~UTF8_BIT;
    z->lflg &= ~UTF8_BIT;
  }
#endif

  append_ulong_to_mem(LOCSIG, &block, &offset, &blocksize);     /* local file header signature */
  append_ushort_to_mem(z->ver, &block, &offset, &blocksize);    /* version needed to extract */
  append_ushort_to_mem(z->lflg, &block, &offset, &blocksize);   /* general purpose bit flag */
  append_ushort_to_mem(z->how, &block, &offset, &blocksize);    /* compression method */
  append_ulong_to_mem(z->tim, &block, &offset, &blocksize);     /* last mod file date time */
  append_ulong_to_mem(z->crc, &block, &offset, &blocksize);     /* crc-32 */
#ifdef ZIP64_SUPPORT        /* zip64 support 09/02/2003 R.Nausedat */
                            /* changes 10/5/03 EG */
  if (zip64_entry) {
    append_ulong_to_mem(0xFFFFFFFF, &block, &offset, &blocksize);	/* compressed size */
    append_ulong_to_mem(0xFFFFFFFF, &block, &offset, &blocksize);	/* uncompressed size */
  } else {
    append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize);/* compressed size */
    append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize);/* uncompressed size */
  }
#else
  append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize);    /* compressed size */
  append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize);    /* uncompressed size */
#endif
#ifdef UNICODE_SUPPORT
  append_ushort_to_mem(nam, &block, &offset, &blocksize);   /* file name length */
#else
  append_ushort_to_mem(z->nam, &block, &offset, &blocksize);   /* file name length */
#endif

  append_ushort_to_mem(z->ext, &block, &offset, &blocksize);    /* extra field length */

#ifdef UNICODE_SUPPORT
  if (use_uname) {
    /* path is UTF-8 */
    append_string_to_mem(z->uname, nam, &block, &offset, &blocksize);
  } else
#endif
#ifdef WIN32_OEM
  /* store name in OEM character set in archive */
  if ((z->vem & 0xff00) == 0)
  {
    char *oem;

    if ((oem = malloc(strlen(z->iname) + 1)) == NULL)
      ZIPERR(ZE_MEM, "putlocal oem");
    INTERN_TO_OEM(z->iname, oem);
    append_string_to_mem(oem, z->nam, &block, &offset, &blocksize); /* file name */
    free(oem);
  } else {
    append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize); /* file name */
  }
#else
  append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize); /* file name */
#endif
  if (z->ext) {
    append_string_to_mem(z->extra, z->ext, &block, &offset, &blocksize); /* extra field */
  }

  /* write the header */
  if (rewrite == PUTLOCAL_REWRITE) {
    /* use fwrite as seeked back and not extending the archive */
    /* also if split_method 1 write to file with local header */
    if (split_method == 1) {
      if (fwrite(block, 1, offset, current_local_file) != offset) {
        free(block);
        return ZE_TEMP;
      }
      /* now can close the split if local header on previous split */
      if (current_local_disk != current_disk) {
        close_split(current_local_disk, current_local_file, current_local_tempname);
        current_local_file = NULL;
        free(current_local_tempname);
      }
    } else {
      /* not doing splits */
      if (fwrite(block, 1, offset, y) != offset) {
        free(block);
        return ZE_TEMP;
      }
    }
  } else {
    /* do same if archive not split or split_method 2 with descriptors */
    /* use bfwrite which counts bytes for splits */
    if (bfwrite(block, 1, offset, BFWRITE_LOCALHEADER) != offset) {
      free(block);
      return ZE_TEMP;
    }
  }
  free(block);
  return ZE_OK;
}

int putextended(z)
  struct zlist far *z;    /* zip entry to write local header for */
  /* This is the data descriptor.
   * Write an extended local header described by *z to file *f.
   * Return an error code in the ZE_ class. */
{
  /* write to mem block then write to file 3/10/2005 */
  char *block = NULL;   /* mem block to write to */
  extent offset = 0;    /* offset into block */
  extent blocksize = 0; /* size of block */

  append_ulong_to_mem(EXTLOCSIG, &block, &offset, &blocksize);  /* extended local signature */
  append_ulong_to_mem(z->crc, &block, &offset, &blocksize);     /* crc-32 */
#ifdef ZIP64_SUPPORT
  if (zip64_entry) {
    /* use Zip64 entries */
    append_int64_to_mem(z->siz, &block, &offset, &blocksize);   /* compressed size */
    append_int64_to_mem(z->len, &block, &offset, &blocksize);   /* uncompressed size */
    /* This is rather klugy as the AppNote handles this poorly.  Typically
       we don't know at this point if we are writing a Zip64 archive or not,
       unless a file has needed Zip64.  This is particularly annoying here
       when deciding the size of the data descriptor (extended local header)
       fields as the appnote says the uncompressed and compressed sizes
       should be 8 bytes if the archive is Zip64 and 4 bytes if not.

       One interpretation is the version of the archive is determined from
       the Version Needed To Extract field in the Zip64 End Of Central Directory
       record and so either an archive should start as Zip64 and write all data
       descriptors with 8-byte fields or store everything until all the files
       are processed and then write everything to the archive as changing the
       sizes of the data descriptors is messy and just not feasible when
       streaming to standard output.  This is not easily workable and others
       use the different interpretation below.

       This was the old thought:
       We always write a standard data descriptor.  If the file has a large
       uncompressed or compressed size we set the field to the max field
       value, which we are defining as flagging the field as having a Zip64
       value that doesn't fit.  As the CRC happens before the variable size
       fields the CRC is still valid and can be used to check the file.  We
       always use deflate if streaming so signatures should not appear in
       the data and all local header signatures should be valid, allowing a
       streaming unzip to find entries by local header signatures, if max size
       values in the data descriptor sizes ignore them, and extract the file and
       check it using the CRC.  If not streaming the central directory is available
       so just use those values which are correct.

       After discussions with other groups this is the current thinking:

       Apparent industry interpretation for data descriptors:
       Data descriptor size is determined for each entry.  If the local header
       version needed to extract is 45 or higher then the entry can use Zip64
       data descriptors but more checking is needed.  If Zip64 extra field is
       present then assume data descriptor is Zip64 and local version needed
       to extract should be 45 or higher.  If standard data descriptor then
       local size fields are set to 0 and correct sizes are in standard data descriptor.
       If Zip64 data descriptor then local sizes are set to -1, Zip64 extra field
       sizes are set to 0, and the correct sizes are in the Zip64 data descriptor.

       So do this:
       If an entry is standard and the archive is updatable then seek back and
       update the local header.  No change.

       If an entry is zip64 and the archive is updatable assume the Zip64 extra
       field was created and update it.  No change.

       If data descriptors are needed then assume the archive is Zip64.  This is
       a change and means if ZIP64_SUPPORT is enabled that any non-updatable archive
       will be in Zip64 format and use Zip64 data descriptors.  This should be
       compatible with other zippers that depend on the current (though not perfect)
       AppNote description.

       If anyone has some ideas on this I'd like to hear them.

       3/20/05 EG

       Only assume need Zip64 if the input size is unknown.  If the input size is
       known we can assume Zip64 if the input is larger than 4 GB and assume not
       otherwise.  If the output is seekable we still need to create the Zip64
       extra field if the input size is unknown so we can seek back and update it.
       12/28/05 EG
       Updated 5/21/06 EG
    */
  } else {
    /* for encryption */
    append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize);  /* compressed size */
    append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize);  /* uncompressed size */
  }
#else
  append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize);    /* compressed size */
  append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize);    /* uncompressed size */
#endif
  /* write the header */
  if (bfwrite(block, 1, offset, BFWRITE_HEADER) != offset) {
    free(block);
    return ZE_TEMP;
  }
  free(block);
  return ZE_OK;
}

int putcentral(z)
  struct zlist far *z;    /* zip entry to write central header for */
/* Write a central header described by *z to file *f.  Return an error code
   in the ZE_ class. */
/* output now uses bfwrite which writes global y */
{
  /* If any of compressed size (siz), uncompressed size (len), offset(off), or
     disk number (dsk) is larger than can fit in the below standard fields then a
     Zip64 flag value is stored and a Zip64 extra field is created.
     Only siz and len are in the local header while all are in the central directory
     header.

     For the central directory header just store the fields required.  All previous fields
     must be stored though.  So can store none (no extra field), just uncompressed size
     (len), len then siz, len then siz then off, or len then siz then off then dsk, in
     those orders.  10/6/03 EG
   */

  /* write to mem block then write to file 3/10/2005 EG */
  char *block = NULL;   /* mem block to write to */
  extent offset = 0;    /* offset into block */
  extent blocksize = 0; /* size of block */
  uzoff_t off = 0;      /* offset to start of local header */
  ush nam = z->nam;     /* size of name to write to header */
#ifdef UNICODE_SUPPORT
  int use_uname = 0;    /* write uname to header */
#endif

#ifdef ZIP64_SUPPORT        /* zip64 support 09/02/2003 R.Nausedat */
  int iRes;
#endif

#ifdef UNICODE_SUPPORT
  if (z->uname) {
    if (utf8_force) {
      z->flg |= UTF8_BIT;
    }
    if (z->flg & UTF8_BIT) {
      /* If this flag is set, then restore UTF-8 as path name */
      use_uname = 1;
      nam = strlen(z->uname);
    } else {
      add_Unicode_Path_cen_extra_field(z);
    }
  } else {
    /* clear UTF-8 bit as not needed */
    z->flg &= ~UTF8_BIT;
    z->lflg &= ~UTF8_BIT;
  }
#endif

  off = z->off;

#ifdef ZIP64_SUPPORT        /* zip64 support 09/02/2003 R.Nausedat */
  if (z->siz > ZIP_UWORD32_MAX || z->len > ZIP_UWORD32_MAX ||
      z->off > ZIP_UWORD32_MAX || z->dsk > ZIP_UWORD16_MAX || (force_zip64 == 1))
  {
    iRes = add_central_zip64_extra_field(z);
    if( iRes != ZE_OK )
      return iRes;
  }

  append_ulong_to_mem(CENSIG, &block, &offset, &blocksize);     /* central file header signature */
  append_ushort_to_mem(z->vem, &block, &offset, &blocksize);    /* version made by */
  append_ushort_to_mem(z->ver, &block, &offset, &blocksize);    /* version needed to extract */
  append_ushort_to_mem(z->flg, &block, &offset, &blocksize);    /* general purpose bit flag */
  append_ushort_to_mem(z->how, &block, &offset, &blocksize);    /* compression method */
  append_ulong_to_mem(z->tim, &block, &offset, &blocksize);     /* last mod file date time */
  append_ulong_to_mem(z->crc, &block, &offset, &blocksize);     /* crc-32 */
  if (z->siz > ZIP_UWORD32_MAX)
  {
    /* instead of z->siz */
    append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize); /* compressed size */
  }
  else
  {
    append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize); /* compressed size */
  }
  /* if forcing Zip64 just force first ef field */
  if (z->len > ZIP_UWORD32_MAX || (force_zip64 == 1))
  {
    /* instead of z->len */
    append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize); /* uncompressed size */
  }
  else
  {
    append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize); /* uncompressed size */
  }
  append_ushort_to_mem(nam, &block, &offset, &blocksize);       /* file name length */
  append_ushort_to_mem(z->cext, &block, &offset, &blocksize);   /* extra field length */
  append_ushort_to_mem(z->com, &block, &offset, &blocksize);    /* file comment length */

  if (z->dsk > ZIP_UWORD16_MAX)
  {
    /* instead of z->dsk */
    append_ushort_to_mem((ush)ZIP_UWORD16_MAX, &block, &offset, &blocksize); /* Zip64 flag */
  }
  else
  {
    append_ushort_to_mem((ush)z->dsk, &block, &offset, &blocksize);	/* disk number start */
  }
  append_ushort_to_mem(z->att, &block, &offset, &blocksize);    /* internal file attributes */
  append_ulong_to_mem(z->atx, &block, &offset, &blocksize);     /* external file attributes */
  if (off > ZIP_UWORD32_MAX)
  {
    /* instead of z->off */
    append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize); /* Zip64 flag */
  }
  else
  {
    append_ulong_to_mem((ulg)off, &block, &offset, &blocksize); /* offset of local header */
  }

#else /* !ZIP64_SUPPORT */

  append_ulong_to_mem(CENSIG, &block, &offset, &blocksize);     /* central file header signature */
  append_ushort_to_mem(z->vem, &block, &offset, &blocksize);    /* version made by */
  append_ushort_to_mem(z->ver, &block, &offset, &blocksize);    /* version needed to extract */
  append_ushort_to_mem(z->flg, &block, &offset, &blocksize);    /* general purpose bit flag */
  append_ushort_to_mem(z->how, &block, &offset, &blocksize);    /* compression method */
  append_ulong_to_mem(z->tim, &block, &offset, &blocksize);     /* last mod file date time */
  append_ulong_to_mem(z->crc, &block, &offset, &blocksize);     /* crc-32 */
  append_ulong_to_mem((ulg)z->siz, &block, &offset, &blocksize);  /* compressed size */
  append_ulong_to_mem((ulg)z->len, &block, &offset, &blocksize);  /* uncompressed size */
  append_ushort_to_mem(nam, &block, &offset, &blocksize);       /* file name length */
  append_ushort_to_mem(z->cext, &block, &offset, &blocksize);   /* extra field length */
  append_ushort_to_mem(z->com, &block, &offset, &blocksize);    /* file comment length */
  append_ushort_to_mem((ush)z->dsk, &block, &offset, &blocksize); /* disk number start */
  append_ushort_to_mem(z->att, &block, &offset, &blocksize);    /* internal file attributes */
  append_ulong_to_mem(z->atx, &block, &offset, &blocksize);     /* external file attributes */
  append_ulong_to_mem((ulg)off, &block, &offset, &blocksize);   /* relative offset of local header */

#endif /* ZIP64_SUPPORT */

#ifdef EBCDIC
  if (z->com)
    memtoasc(z->comment, z->comment, z->com);
#endif /* EBCDIC */

#ifdef UNICODE_SUPPORT
  if (use_uname) {
    /* path is UTF-8 */
    append_string_to_mem(z->uname, nam, &block, &offset, &blocksize);
  } else
#endif
#ifdef WIN32_OEM
  /* store name in OEM character set in archive */
  if ((z->vem & 0xff00) == 0)
  {
    char *oem;

    if ((oem = malloc(strlen(z->iname) + 1)) == NULL)
      ZIPERR(ZE_MEM, "putcentral oem");
    INTERN_TO_OEM(z->iname, oem);
    append_string_to_mem(oem, z->nam, &block, &offset, &blocksize);
    free(oem);
  } else {
    append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize);
  }
#else
  append_string_to_mem(z->iname, z->nam, &block, &offset, &blocksize);
#endif

  if (z->cext) {
    append_string_to_mem(z->cextra, z->cext, &block, &offset, &blocksize);
  }
  if (z->com) {
#ifdef WIN32_OEM
    /* store comment in OEM character set in archive */
    if ((z->vem & 0xff00) == 0)
    {
      char *oem;

      if ((oem = malloc(strlen(z->comment) + 1)) == NULL)
        ZIPERR(ZE_MEM, "putcentral oem comment");
      INTERN_TO_OEM(z->comment, oem);
      append_string_to_mem(oem, z->com, &block, &offset, &blocksize);
      free(oem);
    } else {
      append_string_to_mem(z->comment, z->com, &block, &offset, &blocksize);
    }
#else
    append_string_to_mem(z->comment, z->com, &block, &offset, &blocksize);
#endif
  }

  /* write the header */
  if (bfwrite(block, 1, offset, BFWRITE_CENTRALHEADER) != offset) {
    free(block);
    return ZE_TEMP;
  }
  free(block);

  return ZE_OK;
}


/* Write the end of central directory data to file y.  Return an error code
   in the ZE_ class. */

int putend( OFT( uzoff_t) n,
            OFT( uzoff_t) s,
            OFT( uzoff_t) c,
            OFT( extent) m,
            OFT( char *) z
          )
#ifdef NO_PROTO
  uzoff_t n;                /* number of entries in central directory */
  uzoff_t s;                /* size of central directory */
  uzoff_t c;                /* offset of central directory */
  extent m;                 /* length of zip file comment (0 if none) */
  char *z;                  /* zip file comment if m != 0 */
#endif /* def NO_PROTO */
{
#ifdef ZIP64_SUPPORT        /* zip64 support 09/05/2003 R.Nausedat */
  ush vem;          /* version made by */
  int iNeedZip64 = 0;

  char *block = NULL;   /* mem block to write to */
  extent offset = 0;    /* offset into block */
  extent blocksize = 0; /* size of block */

  /* we have to create a zip64 archive if we have more than 64k - 1 entries,      */
  /* if the CD is > 4 GB or if the offset to the CD > 4 GB. even if the CD start  */
  /* is < 4 GB and CD start + CD size > 4GB we do not need a zip64 archive since  */
  /* the offset entry in the CD tail is still valid.  [note that there are other  */
  /* reasons for needing a Zip64 archive though, such as an uncompressed          */
  /* size > 4 GB for an entry but the entry compresses below 4 GB, so the archive */
  /* is Zip64 but the CD does not need Zip64.]                                    */
  /* order of the zip/zip64 records in a zip64 archive:                           */
  /* central directory                                                            */
  /* zip64 end of central directory record                                        */
  /* zip64 end of central directory locator                                       */
  /* end of central directory record                                              */

  /* check zip64_archive instead of force_zip64 3/19/05 */

  zip64_eocd_disk = current_disk;
  zip64_eocd_offset = bytes_this_split;

  if( n > ZIP_UWORD16_MAX || s > ZIP_UWORD32_MAX || c > ZIP_UWORD32_MAX ||
      zip64_archive )
  {
    ++iNeedZip64;
    /* write zip64 central dir tail:  */
    /*                                    */
    /* 4 bytes   zip64 end of central dir signature (0x06064b50) */
    append_ulong_to_mem((ulg)ZIP64_CENTRAL_DIR_TAIL_SIG, &block, &offset, &blocksize);
    /* 8 bytes   size of zip64 end of central directory record */
    /* a fixed size unless the end zip64 extensible data sector is used. - 3/19/05 EG */
    /* also note that AppNote 6.2 creates version 2 of this record for
       central directory encryption - 3/19/05 EG */
    append_int64_to_mem((zoff_t)ZIP64_CENTRAL_DIR_TAIL_SIZE, &block, &offset, &blocksize);

    /* 2 bytes   version made by */
    vem = OS_CODE + Z_MAJORVER * 10 + Z_MINORVER;
    append_ushort_to_mem(vem, &block, &offset, &blocksize);

    /* APPNOTE says that zip64 archives should have at least version 4.5
       in the "version needed to extract" field */
    /* 2 bytes   version needed to extract */
    append_ushort_to_mem(ZIP64_MIN_VER, &block, &offset, &blocksize);

    /* 4 bytes   number of this disk */
    append_ulong_to_mem(current_disk, &block, &offset, &blocksize);
    /* 4 bytes   number of the disk with the start of the central directory */
    append_ulong_to_mem(cd_start_disk, &block, &offset, &blocksize);
    /* 8 bytes   total number of entries in the central directory on this disk */
    append_int64_to_mem(cd_entries_this_disk, &block, &offset, &blocksize);
    /* 8 bytes   total number of entries in the central directory */
    append_int64_to_mem(n, &block, &offset, &blocksize);
    /* 8 bytes   size of the central directory */
    append_int64_to_mem(s, &block, &offset, &blocksize);
    /* 8 bytes   offset of start of central directory with respect to the starting disk number */
    append_int64_to_mem(cd_start_offset, &block, &offset, &blocksize);
    /* zip64 extensible data sector    (variable size), we don't use it... */

    /* write zip64 end of central directory locator:  */
    /*                                                    */
    /* 4 bytes   zip64 end of central dir locator  signature (0x07064b50) */
    append_ulong_to_mem(ZIP64_CENTRAL_DIR_TAIL_END_SIG, &block, &offset, &blocksize);
    /* 4 bytes   number of the disk with the start of the zip64 end of central directory */
    append_ulong_to_mem(zip64_eocd_disk, &block, &offset, &blocksize);
    /* 8 bytes   relative offset of the zip64 end of central directory record, that is */
    /* offset of CD + CD size */
    append_int64_to_mem(zip64_eocd_offset, &block, &offset, &blocksize);
    /* PUTLLG(l64Temp, f); */
    /* 4 bytes   total number of disks */
    append_ulong_to_mem(current_disk + 1, &block, &offset, &blocksize);
  }

  /* end of central dir signature */
  append_ulong_to_mem(ENDSIG, &block, &offset, &blocksize);
    /* mv archives to come :)         */
    /* for now use n for all          */
    /* 2 bytes    number of this disk */
  if (current_disk < 0xFFFF)
    append_ushort_to_mem((ush)current_disk, &block, &offset, &blocksize);
  else
    append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  /* 2 bytes    number of the disk with the start of the central directory */
  if (cd_start_disk == (ulg)-1)
    cd_start_disk = 0;
  if (cd_start_disk < 0xFFFF)
    append_ushort_to_mem((ush)cd_start_disk, &block, &offset, &blocksize);
  else
    append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  /* 2 bytes    total number of entries in the central directory on this disk */
  if (cd_entries_this_disk < 0xFFFF)
    append_ushort_to_mem((ush)cd_entries_this_disk, &block, &offset, &blocksize);
  else
    append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  /* 2 bytes    total number of entries in the central directory */
  if (total_cd_entries < 0xFFFF)
    append_ushort_to_mem((ush)total_cd_entries, &block, &offset, &blocksize);
  else
    append_ushort_to_mem((ush)0xFFFF, &block, &offset, &blocksize);
  if( s > ZIP_UWORD32_MAX )
    /* instead of s */
    append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize);
  else
    /* 4 bytes    size of the central directory */
    append_ulong_to_mem((ulg)s, &block, &offset, &blocksize);
  if(force_zip64 == 1 || cd_start_offset > ZIP_UWORD32_MAX)
    /* instead of cd_start_offset */
    append_ulong_to_mem(ZIP_UWORD32_MAX, &block, &offset, &blocksize);
  else
    /* 4 bytes    offset of start of central directory with respect to the starting disk number */
    append_ulong_to_mem((ulg)cd_start_offset, &block, &offset, &blocksize);

#else /* !ZIP64_SUPPORT */
  char *block = NULL;   /* mem block to write to */
  extent offset = 0;    /* offset into block */
  extent blocksize = 0; /* size of block */

  /* end of central dir signature */
  append_ulong_to_mem(ENDSIG, &block, &offset, &blocksize);
  /* 2 bytes    number of this disk */
  append_ushort_to_mem((ush)current_disk, &block, &offset, &blocksize);
  /* 2 bytes    number of the disk with the start of the central directory */
  append_ushort_to_mem((ush)cd_start_disk, &block, &offset, &blocksize);
  /* 2 bytes    total number of entries in the central directory on this disk */
  append_ushort_to_mem((ush)cd_entries_this_disk, &block, &offset, &blocksize);
  /* 2 bytes    total number of entries in the central directory */
  append_ushort_to_mem((ush)n, &block, &offset, &blocksize);
  /* 4 bytes    size of the central directory */
  append_ulong_to_mem((ulg)s, &block, &offset, &blocksize);
  /* 4 bytes    offset of start of central directory with respect to the starting disk number */
  append_ulong_to_mem((ulg)cd_start_offset, &block, &offset, &blocksize);
#endif /* ZIP64_SUPPORT */

  /* size of comment */
  append_ushort_to_mem((ush)m, &block, &offset, &blocksize);
  /* Write the comment, if any */
#ifdef EBCDIC
  memtoasc(z, z, m);
#endif
  if (m) {
    /* PKWare defines the archive comment to be ASCII only so no OEM conversion */
    append_string_to_mem(z, m, &block, &offset, &blocksize);
  }

  /* write the block */
  if (bfwrite(block, 1, offset, BFWRITE_HEADER) != offset) {
    free(block);
    return ZE_TEMP;
  }
  free(block);

#ifdef HANDLE_AMIGA_SFX
  if (amiga_sfx_offset && zipbeg /* -J zeroes this */) {
    s = zftello(y);
    while (s & 3) s++, putc(0, f);   /* final marker must be longword aligned */
    PUTLG(0xF2030000 /* 1010 in Motorola byte order */, f);
    c = (s - amiga_sfx_offset - 4) / 4;  /* size of archive part in longwords */
    if (zfseeko(y, amiga_sfx_offset, SEEK_SET) != 0)
      return ZE_TEMP;
    c = ((c >> 24) & 0xFF) | ((c >> 8) & 0xFF00)
         | ((c & 0xFF00) << 8) | ((c & 0xFF) << 24);     /* invert byte order */
    PUTLG(c, y);
    zfseeko(y, 0, SEEK_END);                                  /* just in case */
  }
#endif

  return ZE_OK;
} /* end function putend() */



/* Note: a zip "entry" includes a local header (which includes the file
   name), an encryption header if encrypting, the compressed data
   and possibly an extended local header. */

int zipcopy(z)
  struct zlist far *z;    /* zip entry to copy */
/* Copy the zip entry described by *z from in_file to y.  Return an
   error code in the ZE_ class.  Also update tempzn by the number of bytes
   copied. */
/* Now copies to global output file y */
/* Handle entries that span disks */
/* If fix == 2, assume in_file is pointing to a local header and fill
   in z from local header */
{
  uzoff_t n;            /* holds local header offset */
  ulg e = 0;            /* extended local header size */
  ulg start_disk = 0;
  uzoff_t start_offset = 0;
  char *split_path;
  char buf[LOCHEAD + 1];
  struct zlist far *localz;
  int r;


  Trace((stderr, "zipcopy %s\n", z->zname));

  /* if fix == 2 assume in_file open and pointing at local header */
  if (fix != 2) {
    start_disk = z->dsk;
    start_offset = z->off;

    /* don't assume reading the right disk */

    /* if start not on current disk then close current disk */
    if (start_disk != current_in_disk) {
      if (in_file) {
        fclose(in_file);
        in_file = NULL;
      }
    }

    current_in_disk = start_disk;

    /* disks are archive.z01, archive.z02, ..., archive.zip */
    split_path = get_in_split_path(in_path, current_in_disk);

    if (in_file == NULL) {
      while ((in_file = zfopen(split_path, FOPR)) == NULL) {
        /* could not open split */

        if (!noisy) {
          ZIPERR(ZE_OPEN, split_path);
        }

        /* Ask for directory with split.  Updates global in_path */
        r = ask_for_split_read_path(start_disk);
        if (r == ZE_ABORT) {
          /* user abort */
          return ZE_ABORT;
        } else if ((fix == 1 || fix == 2) && r == ZE_FORM) {
          /* user asks to skip this disk */
          return ZE_FORM;
        }
        free(split_path);
        split_path = get_in_split_path(in_path, start_disk);
      }
    }

    if (zfseeko(in_file, start_offset, SEEK_SET) != 0) {
      fclose(in_file);
      in_file = NULL;
      zipwarn("reading archive fseek: ", strerror(errno));
      return ZE_READ;
    }
  } /* fix != 2 */

  if (fix != 2 && !at_signature(in_file, "PK\03\04")) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("Did not find entry for ", z->iname);
    return ZE_FORM;
  }

  /* read local header */
  if (fread(buf, LOCHEAD, 1, in_file) != 1) {
    int f = ferror(in_file);
    zipwarn("reading local entry: ", strerror(errno));
    if (fix != 2)
      fclose(in_file);
    return f ? ZE_READ : ZE_EOF;
  }

  /* Local Header
       local file header signature     4 bytes  (0x04034b50)
       version needed to extract       2 bytes
       general purpose bit flag        2 bytes
       compression method              2 bytes
       last mod file time              2 bytes
       last mod file date              2 bytes
       crc-32                          4 bytes
       compressed size                 4 bytes
       uncompressed size               4 bytes
       file name length                2 bytes
       extra field length              2 bytes

       file name (variable size)
       extra field (variable size)
   */

  if ((localz = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL) {
    zipwarn("reading entry", "");
    if (fix != 2)
      fclose(in_file);
    return ZE_MEM;
  }

  localz->ver = SH(LOCVER + buf);
  localz->lflg = SH(LOCFLG + buf);
  localz->how = SH(LOCHOW + buf);
  localz->tim = LG(LOCTIM + buf);          /* time and date into one long */
  localz->crc = LG(LOCCRC + buf);
  localz->nam = SH(LOCNAM + buf);
  localz->ext = SH(LOCEXT + buf);
  if (fix == 2) {
    localz->siz = LG(LOCSIZ + buf);
    localz->len = LG(LOCLEN + buf);
  }

  if (fix == 2) {
    /* Do some sanity checks to make reasonably sure this is a local header */
    ush os = localz->ver >> 8;
    ush pkver = localz->ver - os;

    /* OS - currently 0 - 18 (AppNote 6.3) and 30 (ATHEOS) */
    if (os > 40) {
      sprintf(errbuf, "Illegal host system mapping in local header:  %d", os);
      zipwarn(errbuf, "");
      zipwarn("Skipping:  ", z->iname);
      return ZE_FORM;
    }
    /* PK Version - currently 10 - 62 (AppNote 6.2.2) */
    /* If PKZip central directory encryption is used (62), the local header
       values could be masked values.  Specifically, as of AppNote 6.2.2
       the time, crc-32, and uncompressed file size are masked and the
       file name is also replaced with a hex entry count.  Should
       still be able to recover the entries, but they may be unreadable
       without the 62 support fields. */
    if (pkver > 100) {
      sprintf(errbuf, "Illegal PK version mapping in local header:  %d", pkver);
      zipwarn(errbuf, "");
      zipwarn("Skipping:  ", z->iname);
      return ZE_FORM;
    }
    /* Currently compression method is defined as 0 - 19 and 98 (AppNote 6.3) */
    /* We can still copy an entry we can't read, but something over 200 is
       probably illegal */
    if (localz->how > 200) {
      sprintf(errbuf, "Unrecognized compression method in local header:  %d", localz->how);
      zipwarn(errbuf, "");
      zipwarn("Skipping:  ", z->iname);
      return ZE_FORM;
    }

    /* It's hard to make guesses on the other fields.  Suggestions welcome. */
  }

  /* Initialize all fields pointing to malloced data to NULL */
  localz->zname = localz->name = localz->iname = localz->extra = NULL;
  localz->oname = NULL;
#ifdef UNICODE_SUPPORT
  localz->uname = NULL;
#endif

  /* Read file name, extra field and comment field */
  if ((localz->iname = malloc(localz->nam+1)) ==  NULL ||
      (localz->ext && (localz->extra = malloc(localz->ext)) == NULL))
    return ZE_MEM;
  if (fread(localz->iname, localz->nam, 1, in_file) != 1 ||
      (localz->ext && fread(localz->extra, localz->ext, 1, in_file) != 1))
    return ferror(in_file) ? ZE_READ : ZE_EOF;
  localz->iname[localz->nam] = '\0';                  /* terminate name */
  if ((localz->name = malloc(localz->nam+1)) ==  NULL)
    return ZE_MEM;
  strcpy(localz->name, localz->iname);

#ifdef ZIP64_SUPPORT
  zip64_entry = adjust_zip_local_entry(localz);
#endif

  localz->vem = 0;
  if (fix != 2) {
    /* Need vem to determine if iname is Win32 OEM name */
    localz->vem = z->vem;

#ifdef UNICODE_SUPPORT
    if (unicode_mismatch != 3) {
      if (z->flg & UTF8_BIT) {
        char *iname;
        /* path is UTF-8 */
        localz->uname = localz->iname;
        iname = utf8_to_local_string(localz->uname);
        if (iname == NULL) {
          /* a bad UTF-8 character in name likely - go with (probably messed up) uname */
          if ((localz->iname = malloc(strlen(localz->uname) + 1)) == NULL) {
            return ZE_MEM;
          }
          strcpy(localz->iname, localz->uname);
        } else {
          /* go with local character set iname */
          localz->iname = iname;
        }
      } else {
        /* check for UTF-8 path extra field */
        read_Unicode_Path_local_entry(localz);
      }
    }
#endif

#ifdef WIN32_OEM
      /* If fix == 2 and reading local headers first, vem is not in the local
         header so we don't know when to do OEM translation, as the ver field
         is set to MSDOS (0) by all unless something specific is needed.
         However, if local header has a Unicode path extra field, we can get
         the real file name from there. */
    if ((z->vem & 0xff00) == 0)
      /* assume archive name is OEM if from DOS */
      oem_to_local_string(localz->iname, localz->iname);
#endif
  }

  if (fix == 2) {
# ifdef WIN32
#  ifdef UNICODE_SUPPORT
    localz->namew = NULL;
    localz->inamew = NULL;
    localz->znamew = NULL;
    z->namew = NULL;
    z->inamew = NULL;
    z->znamew = NULL;
#  endif
# endif
    /* set z from localz */
    z->flg = localz->lflg;
    z->len = localz->len;
    z->siz = localz->siz;

  } else {
    /* Compare localz to z */
    if (localz->ver != z->ver) {
      zipwarn("Local Version Needed To Extract does not match CD: ", z->iname);
    }
    if (localz->lflg != z->flg) {
      zipwarn("Local Entry Flag does not match CD: ", z->iname);
    }
    if (!(z->flg & 8)) {
      if (localz->crc != z->crc) {
        zipwarn("Local Entry CRC does not match CD: ", z->iname);
      }
    }
    if (fix != 3 && strcmp(localz->iname, z->iname) != 0) {
      zipwarn("Local Entry name does not match CD: ", z->iname);
    }

    /* as copying get uncompressed and compressed sizes from central directory */
    localz->len = z->len;
    localz->siz = z->siz;
  }

#if 0
  if (fix > 1) {
    if (zfseeko(in_file, z->off + n, SEEK_SET)) /* seek to compressed data */
      return ferror(in_file) ? ZE_READ : ZE_EOF;

    if (fix > 2) {
      /* Update length of entry's name, it may have been changed.  This is
         needed to support the ZipNote ability to rename archive entries. */
      z->nam = strlen(z->iname);
      n = (uzoff_t)((LOCHEAD) + (ulg)z->nam + (ulg)z->ext);
    }

    /* do not trust the old compressed size */
    if (putlocal(z, PUTLOCAL_WRITE) != ZE_OK)
      return ZE_TEMP;

    z->off = tempzn;
    tempzn += n;
    n = z->siz;
  } else {
    if (zfseeko(in_file, z->off, SEEK_SET))     /* seek to local header */
      return ferror(in_file) ? ZE_READ : ZE_EOF;

    z->off = tempzn;
    n += z->siz;
  }
#endif

  /* from zipnote */
  if (fix == 3) {
    /* Update length of entry's name, as it may have been changed.  This is
       needed to support the ZipNote ability to rename archive entries. */
    localz->nam = z->nam = strlen(z->iname);
    /* update local name */
    free(localz->iname);
    if ((localz->iname = malloc(strlen(z->iname) + 1)) == NULL) {
      zipwarn("out of memory in zipcopy", "");
      return ZE_MEM;
    }
    strcpy(localz->iname, z->iname);
  }

  /* update disk and offset */
  z->dsk = current_disk;
  z->off = bytes_this_split;

  /* copy the compressed data and the extended local header if there is one */

  /* copy the compressed data.  We recreate the local header as the local
     header can't be split and putlocal ensures it won't.  Also, since we
     use siz and len from the central directory, we don't need the extended
     local header if there is one, unless the file is encrypted as then the
     extended header is used to indicate crypt head uses file time instead
     of crc as the password check.

     If fix = 2 then we don't have the central directory yet so keep
     any data descriptors. */

  if (fix != 2 && !(z->flg & 1)) {
    /* Not encrypted */
    localz->flg = z->flg &= ~8;
    z->lflg = localz->lflg &= ~8;
  }

  e = 0;
  if (z->lflg & 8) {
#ifdef ZIP64_SUPPORT
    if (zip64_entry)
      e = 24;
    else
#endif
      e = 16;
  }
  /* 4 is signature */
  n = 4 + (uzoff_t)((LOCHEAD) + (ulg)(localz->nam) + (ulg)(localz->ext));

  n += e + z->siz;
  tempzn += n;

  /* Output name */
  if (fix == 2) {
    if ((z->oname = malloc(strlen(localz->iname) + 1)) == NULL) {
      return ZE_MEM;
    }
    strcpy(z->oname, localz->iname);
#ifndef UTIL
# ifdef WIN32
    /* Win9x console always uses OEM character coding, and
       WinNT console is set to OEM charset by default, too */
    _INTERN_OEM(z->oname);
# endif
#endif
    sprintf(errbuf, " copying: %s ", z->oname);
    zipmessage_nl(errbuf, 0);
  }

  if (fix == 2)
    z->crc = localz->crc;
  else
    localz->crc = z->crc;

  if (putlocal(localz, PUTLOCAL_WRITE) != ZE_OK)
      return ZE_TEMP;

  /*
  if (zfseeko(in_file, start_offset, SEEK_SET) != 0) {
    fclose(in_file);
    in_file = NULL;
    zipwarn("reading archive fseek: ", strerror(errno));
    return ZE_READ;
  }
  */

  /* copy the data */
  if (fix == 2 && localz->lflg & 8)
    /* read to data descriptor */
    r = bfcopy((uzoff_t) -2);
  else
    r = bfcopy(localz->siz);

  if (r == ZE_ABORT) {
      if (localz->ext) free(localz->extra);
      if (localz->nam) free(localz->iname);
      if (localz->nam) free(localz->name);
#ifdef UNICODE_SUPPORT
      if (localz->uname) free(localz->uname);
#endif
      free(localz);
      ZIPERR(ZE_ABORT, "Could not find split");
  }

  if (r == ZE_EOF || skip_this_disk) {
      /* missing disk */
      zipwarn("aborting: ", z->oname);

      if (r == ZE_OK)
        r = ZE_FORM;

      if (fix == 2) {
#ifdef DEBUG
        zoff_t here = zftello(y);
#endif

        /* fix == 2 skips right to next disk */
        skip_this_disk = 0;

        /* seek back in output to start of this entry so can overwrite */
        if (zfseeko(y, current_local_offset, SEEK_SET) != 0) {
          ZIPERR(ZE_WRITE, "seek failed on output file");
        }
        bytes_this_split = current_local_offset;
        tempzn = current_local_offset;
      }

      /* tell scan to skip this entry */
      if (localz->ext) free(localz->extra);
      if (localz->nam) free(localz->iname);
      if (localz->nam) free(localz->name);
#ifdef UNICODE_SUPPORT
      if (localz->uname) free(localz->uname);
#endif
      free(localz);
      return r;
  }

  if (fix == 2 && z->flg & 8) {
    /* this entry should have a data descriptor */
    /* only -FF needs to read the descriptor as other modes
       rely on the central directory */
    if (des_good) {
      /* found an apparently good data descriptor */
      localz->crc = des_crc;
      localz->siz = des_csize;
      localz->len = des_usize;
    } else {
      /* no end to this entry found */
      zipwarn("no end of stream entry found: ", z->oname);
      zipwarn("rewinding and scanning for later entries", "");

      /* seek back in output to start of this entry so can overwrite */
      if (zfseeko(y, current_local_offset, SEEK_SET) != 0){

      }

      /* tell scan to skip this entry */
      if (localz->ext) free(localz->extra);
      if (localz->nam) free(localz->iname);
      if (localz->nam) free(localz->name);
#ifdef UNICODE_SUPPORT
      if (localz->uname) free(localz->uname);
#endif
      free(localz);
      return ZE_FORM;
    }
  }

  if (z->flg & 8) {
    putextended(localz);
  }

  /* now can close the split if local header on previous split */
  if (split_method == 1 && current_local_disk != current_disk) {
    close_split(current_local_disk, current_local_file, current_local_tempname);
    current_local_file = NULL;
    free(current_local_tempname);
  }

  /* update local header and close start split */
  /* to use this need to seek back, do this, then come back
  if (putlocal(localz, PUTLOCAL_REWRITE) != ZE_OK)
    r = ZE_TEMP;
  */

  if (fix == 2) {
    z->ver = localz->ver;
    z->how = localz->how;
    z->tim = localz->tim;
    z->crc = localz->crc;
    z->lflg = localz->lflg;
    z->flg = localz->lflg;
    z->len = localz->len;
    z->siz = localz->siz;
    z->nam = localz->nam;
    z->ext = localz->ext;
    z->extra = localz->extra;
    /* copy local extra fields to central directory for now */
    z->cext = localz->ext;
    z->cextra = NULL;
    if (localz->ext) {
      if ((z->cextra = malloc(localz->ext + 1)) == NULL) {
      return ZE_MEM;
      }
      strcpy(z->cextra, localz->extra);
    }
    z->com = 0;
    z->att = 0;
    z->atx = 0;
    z->name = localz->name;
    z->iname = localz->iname;
#ifdef UNICODE_SUPPORT
    z->uname = localz->uname;
#endif
    if ((z->zname = malloc(localz->nam + 1)) == NULL) {
      return ZE_MEM;
    }
    strcpy(z->zname, z->iname);
  } else {
    if (localz->ext) free(localz->extra);
    if (localz->nam) free(localz->iname);
    if (localz->nam) free(localz->name);
#ifdef UNICODE_SUPPORT
    if (localz->uname) free(localz->uname);
#endif
    free(localz);
  }

  if (fix == 2) {
    sprintf(errbuf, " (%s bytes)", zip_fzofft(z->siz, NULL, "u"));
    zipmessage_nl(errbuf, 1);

    if (r == ZE_READ) {
      zipwarn("entry truncated: ", z->oname);
      sprintf(errbuf, "expected compressed/stored size %s, actual %s",
              zip_fzofft(localz->siz, NULL, "u"), zip_fzofft(bytes_this_entry, NULL, "u"));
      zipwarn(errbuf, "");
    }
  }

  return r;
}



#ifndef UTIL

#ifdef USE_EF_UT_TIME

local int ef_scan_ut_time(ef_buf, ef_len, ef_is_cent, z_utim)
char *ef_buf;                   /* buffer containing extra field */
extent ef_len;                  /* total length of extra field */
int ef_is_cent;                 /* flag indicating "is central extra field" */
iztimes *z_utim;                /* return storage: atime, mtime, ctime */
/* This function scans the extra field for EF_TIME or EF_IZUNIX blocks
 * containing Unix style time_t (GMT) values for the entry's access, creation
 * and modification time.
 * If a valid block is found, all time stamps are copied to the iztimes
 * structure.
 * The presence of an EF_TIME or EF_IZUNIX2 block results in ignoring
 * all data from probably present obsolete EF_IZUNIX blocks.
 * If multiple blocks of the same type are found, only the information from
 * the last block is used.
 * The return value is the EF_TIME Flags field (simulated in case of an
 * EF_IZUNIX block) or 0 in case of failure.
 */
{
  int flags = 0;
  unsigned eb_id;
  extent eb_len;
  int have_new_type_eb = FALSE;

  if (ef_len == 0 || ef_buf == NULL)
    return 0;

  Trace((stderr,"\nef_scan_ut_time: scanning extra field of length %u\n",
         (unsigned)ef_len));
  while (ef_len >= EB_HEADSIZE) {
    eb_id = SH(EB_ID + ef_buf);
    eb_len = SH(EB_LEN + ef_buf);

    if (eb_len > (ef_len - EB_HEADSIZE)) {
      /* Discovered some extra field inconsistency! */
      Trace((stderr,"ef_scan_ut_time: block length %u > rest ef_size %u\n",
             (unsigned)eb_len, (unsigned)(ef_len - EB_HEADSIZE)));
      break;
    }

    switch (eb_id) {
      case EF_TIME:
        flags &= ~0x00ff;       /* ignore previous IZUNIX or EF_TIME fields */
        have_new_type_eb = TRUE;
        if ( eb_len >= EB_UT_MINLEN && z_utim != NULL) {
           unsigned eb_idx = EB_UT_TIME1;
           Trace((stderr,"ef_scan_ut_time: Found TIME extra field\n"));
           flags |= (ef_buf[EB_HEADSIZE+EB_UT_FLAGS] & 0x00ff);
           if ((flags & EB_UT_FL_MTIME)) {
              if ((eb_idx+4) <= eb_len) {
                 z_utim->mtime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
                 eb_idx += 4;
                 Trace((stderr,"  Unix EF modtime = %ld\n", z_utim->mtime));
              } else {
                 flags &= ~EB_UT_FL_MTIME;
                 Trace((stderr,"  Unix EF truncated, no modtime\n"));
              }
           }
           if (ef_is_cent) {
              break;            /* central version of TIME field ends here */
           }
           if (flags & EB_UT_FL_ATIME) {
              if ((eb_idx+4) <= eb_len) {
                 z_utim->atime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
                 eb_idx += 4;
                 Trace((stderr,"  Unix EF acctime = %ld\n", z_utim->atime));
              } else {
                 flags &= ~EB_UT_FL_ATIME;
              }
           }
           if (flags & EB_UT_FL_CTIME) {
              if ((eb_idx+4) <= eb_len) {
                 z_utim->ctime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
                 /* eb_idx += 4; */  /* superfluous for now ... */
                 Trace((stderr,"  Unix EF cretime = %ld\n", z_utim->ctime));
              } else {
                 flags &= ~EB_UT_FL_CTIME;
              }
           }
        }
        break;

      case EF_IZUNIX2:
        if (!have_new_type_eb) {
           flags &= ~0x00ff;    /* ignore any previous IZUNIX field */
           have_new_type_eb = TRUE;
        }
        break;

      case EF_IZUNIX:
        if (eb_len >= EB_UX_MINLEN) {
           Trace((stderr,"ef_scan_ut_time: Found IZUNIX extra field\n"));
           if (have_new_type_eb) {
              break;            /* Ignore IZUNIX extra field block ! */
           }
           z_utim->atime = LG((EB_HEADSIZE+EB_UX_ATIME) + ef_buf);
           z_utim->mtime = LG((EB_HEADSIZE+EB_UX_MTIME) + ef_buf);
           Trace((stderr,"  Unix EF access time = %ld\n",z_utim->atime));
           Trace((stderr,"  Unix EF modif. time = %ld\n",z_utim->mtime));
           flags |= (EB_UT_FL_MTIME | EB_UT_FL_ATIME);  /* signal success */
        }
        break;

      case EF_THEOS:
/*      printf("Not implemented yet\n"); */
        break;

      default:
        break;
    }
    /* Skip this extra field block */
    ef_buf += (eb_len + EB_HEADSIZE);
    ef_len -= (eb_len + EB_HEADSIZE);
  }

  return flags;
}

int get_ef_ut_ztime(z, z_utim)
struct zlist far *z;
iztimes *z_utim;
{
  int r;

#ifdef IZ_CHECK_TZ
  if (!zp_tz_is_valid) return 0;
#endif

  /* First, scan local extra field. */
  r = ef_scan_ut_time(z->extra, z->ext, FALSE, z_utim);

  /* If this was not successful, try central extra field, but only if
     it is really different. */
  if (!r && z->cext > 0 && z->cextra != z->extra)
    r = ef_scan_ut_time(z->cextra, z->cext, TRUE, z_utim);

  return r;
}

#endif /* USE_EF_UT_TIME */


local void cutpath(p, delim)
char *p;                /* path string */
int delim;              /* path component separator char */
/* Cut the last path component off the name *p in place.
 * This should work on both internal and external names.
 */
{
  char *r;              /* pointer to last path delimiter */

#ifdef VMS                      /* change [w.x.y]z to [w.x]y.DIR */
  if ((r = MBSRCHR(p, ']')) != NULL)
  {
    *r = 0;
    if ((r = MBSRCHR(p, '.')) != NULL)
    {
      *r = ']';
      strcat(r, ".DIR;1");     /* this assumes a little padding--see PAD */
    } else {
      *p = 0;
    }
  } else {
    if ((r = MBSRCHR(p, delim)) != NULL)
      *r = 0;
    else
      *p = 0;
  }
#else /* !VMS */
  if ((r = MBSRCHR(p, delim)) != NULL)
    *r = 0;
  else
    *p = 0;
#endif /* ?VMS */
}

int trash()
/* Delete the compressed files and the directories that contained the deleted
   files, if empty.  Return an error code in the ZE_ class.  Failure of
   destroy() or deletedir() is ignored. */
{
  extent i;             /* counter on deleted names */
  extent n;             /* number of directories to delete */
  struct zlist far **s; /* table of zip entries to handle, sorted */
  struct zlist far *z;  /* current zip entry */

  /* Delete marked names and count directories */
  n = 0;
  for (z = zfiles; z != NULL; z = z->nxt)
    if (z->mark == 1 || z->trash)
    {
      z->mark = 1;
      if (z->iname[z->nam - 1] != (char)0x2f) { /* don't unlink directory */
        if (verbose)
          fprintf(mesg, "zip diagnostic: deleting file %s\n", z->name);
        if (destroy(z->name)) {
          zipwarn("error deleting ", z->name);
        }
        /* Try to delete all paths that lead up to marked names. This is
         * necessary only with the -D option.
         */
        if (!dirnames) {
          cutpath(z->name, '/');  /* XXX wrong ??? */
          /* Below apparently does not work for Russian OEM but
             '/' should be same as 0x2f for ascii and most ports so
             changed it.  Did not trace through the mappings but
             maybe 0x2F is mapped differently on OEM_RUSS - EG 2/28/2003 */
          /* CS, 5/14/2005: iname is the byte array read from and written
             to the zip archive; it MUST be ASCII (compatible)!!!
             If something goes wrong with OEM_RUSS, there is a charcode
             mapping error between external name (z->name) and iname somewhere
             in the in2ex & ex2in code. The charcode translation should be
             checked.
             This code line is changed back to the original code. */
          /* CS, 6/12/2005: What is handled here is the difference between
             ASCII charsets and non-ASCII charsets like the family of EBCDIC
             charsets.  On these systems, the slash character '/' is not coded
             as 0x2f but as 0x61 (the ASCII 'a'). The iname struct member holds
             the name as stored in the Zip file, which are ASCII or translated
             into ASCII for new entries, whereas the "name" struct member hold
             the external name, coded in the native charset of the system
             (EBCDIC on EBCDIC systems) */
          /* cutpath(z->iname, '/'); */ /* QQQ ??? */
          cutpath(z->iname, 0x2f); /* 0x2f = ascii['/'] */
          z->nam = strlen(z->iname);
          if (z->nam > 0) {
            z->iname[z->nam - 1] = (char)0x2f;
            z->iname[z->nam++] = '\0';
          }
          if (z->nam > 0) n++;
        }
      } else {
        n++;
      }
    }

  /* Construct the list of all marked directories. Some may be duplicated
   * if -D was used.
   */
  if (n)
  {
    if ((s = (struct zlist far **)malloc(n*sizeof(struct zlist far *))) ==
        NULL)
      return ZE_MEM;
    n = 0;
    for (z = zfiles; z != NULL; z = z->nxt) {
      if (z->mark && z->nam > 0 && z->iname[z->nam - 1] == (char)0x2f /* '/' */
          && (n == 0 || strcmp(z->name, s[n-1]->name) != 0)) {
        s[n++] = z;
      }
    }
    /* Sort the files in reverse order to get subdirectories first.
     * To avoid problems with strange naming conventions as in VMS,
     * we sort on the internal names, so x/y/z will always be removed
     * before x/y. On VMS, x/y/z > x/y but [x.y.z] < [x.y]
     */
    qsort((char *)s, n, sizeof(struct zlist far *), rqcmp);

    for (i = 0; i < n; i++) {
      char *p = s[i]->name;
      if (*p == '\0') continue;
      if (p[strlen(p) - 1] == '/') { /* keep VMS [x.y]z.dir;1 intact */
        p[strlen(p) - 1] = '\0';
      }
      if (i == 0 || strcmp(s[i]->name, s[i-1]->name) != 0) {
        if (verbose) {
          fprintf(mesg, "deleting directory %s (if empty)                \n",
                  s[i]->name);
        }
        deletedir(s[i]->name);
      }
    }
    free((zvoid *)s);
  }
  return ZE_OK;
}

#endif /* !UTIL */