summaryrefslogtreecommitdiff
path: root/xdelta3/xdelta3-main.h
blob: 0a2592ebaacabca6a2f0997ab9c769b66a2f9b14 (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
/* xdelta 3 - delta compression tools and library
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007,
 * Joshua P. MacDonald
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* This is all the extra stuff you need for convenience to users in a command line
 * application.  It contains these major components:
 *
 * 1. VCDIFF tools
 * 2. external compression support (this is POSIX-specific).
 * 3. a general read/write loop that handles all of the Xdelta decode/encode/VCDIFF-print
 *    functions
 * 4. command-line interpreter
 * 5. an Xdelta application header which stores default filename, external compression settings
 * 6. output/error printing
 * 7. basic file support and OS interface
 */

/* TODO list:
 * 1. do exact gzip-like filename, stdout handling.  make a .xz extension, refuse
 *    to encode to stdout without -cf, etc.
 * 2. Allow the user to add a comment string to the app header without disturbing the default
 *    behavior.
 */

/* On error handling and printing:
 *
 * The xdelta library sets stream->msg to indicate what condition caused an internal
 * failure, but many failures originate here and are printed here.  The return convention
 * is 0 for success, as throughout Xdelta code, but special attention is required here for
 * the operating system calls with different error handling.  See the main_file_* routines.
 * All errors in this file have a message printed at the time of occurance.  Since some of
 * these calls occur within calls to the library, the error may end up being printed again
 * with a more general error message.
 */

/******************************************************************************************/

#ifndef XD3_POSIX
#define XD3_POSIX 0
#endif
#ifndef XD3_STDIO
#define XD3_STDIO 0
#endif
#ifndef XD3_WIN32
#define XD3_WIN32 0
#endif

/* Combines xd3_strerror() and strerror() */
const char* xd3_mainerror(int err_num);

/* XPRINTX (used by main) prefixes an "xdelta3: " to the output. */
#define XPR fprintf
#define NT stderr, "xdelta3: "

#define VC fprintf
#define UT stdout,

/* If none are set, default to posix. */
#if (XD3_POSIX + XD3_STDIO + XD3_WIN32) == 0
#undef XD3_POSIX
#define XD3_POSIX 1
#endif

/* Handle externally-compressed inputs. */
#ifndef EXTERNAL_COMPRESSION
#define EXTERNAL_COMPRESSION 1
#endif

#define PRINTHDR_SPECIAL -4378291

/* The number of soft-config variables.  */
#define XD3_SOFTCFG_VARCNT 10

/* this is used as in XPR(NT XD3_LIB_ERRMSG (stream, ret)) to print an error message
 * from the library. */
#define XD3_LIB_ERRMSG(stream, ret) "%s: %s\n", xd3_errstring (stream), xd3_mainerror (ret)

#include <stdio.h>  /* fprintf */

#if XD3_POSIX
#include <unistd.h> /* close, read, write... */
#include <sys/types.h>
#include <fcntl.h>
#endif

#ifndef _WIN32
#include <unistd.h> /* lots */
#include <sys/time.h> /* gettimeofday() */
#include <sys/stat.h> /* stat() and fstat() */
#else
#define strtoll _strtoi64
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIFEXITED
#   define WIFEXITED(stat)  (((*((int *) &(stat))) & 0xff) == 0)
#endif
#ifndef WEXITSTATUS
#   define WEXITSTATUS(stat) (((*((int *) &(stat))) >> 8) & 0xff)
#endif
#ifndef S_ISREG
//#   ifdef S_IFREG
//#       define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
//#   else
#       define S_ISREG(m) 1
//#   endif
#endif /* !S_ISREG */
#endif

/******************************************************************************************
 ENUMS and TYPES
 ******************************************************************************************/

/* These flags (mainly pertaining to main_read() operations) are set in the
 * main_file->flags variable.  All are related to with external decompression support.
 *
 * RD_FIRST causes the external decompression check when the input is first read.
 *
 * RD_NONEXTERNAL disables external decompression for reading a compressed input, in the
 * case of Xdelta inputs.  Note: Xdelta is supported as an external compression type,
 * which makes is the reason for this flag.  An example to justify this is: to create a
 * delta between two files that are VCDIFF-compressed.  Two external Xdelta decoders are
 * run to supply decompressed source and target inputs to the Xdelta encoder. */
typedef enum
{
  RD_FIRST        = (1 << 0),
  RD_NONEXTERNAL  = (1 << 1),
  RD_EXTERNAL_V1  = (1 << 2),
} xd3_read_flags;

/* main_file->mode values */
typedef enum
{
  XO_READ  = 0,
  XO_WRITE = 1,
} main_file_modes;

/* Main commands.  For example, CMD_PRINTHDR is the "xdelta printhdr" command. */
typedef enum
{
  CMD_NONE = 0,
  CMD_PRINTHDR,
  CMD_PRINTHDRS,
  CMD_PRINTDELTA,
#if XD3_ENCODER
  CMD_ENCODE,
#endif
  CMD_DECODE,
  CMD_TEST,
  CMD_CONFIG,
} xd3_cmd;

#if XD3_ENCODER
#define CMD_DEFAULT CMD_ENCODE
#define IS_ENCODE(cmd) (cmd == CMD_ENCODE)
#else
#define CMD_DEFAULT CMD_DECODE
#define IS_ENCODE(cmd) (0)
#endif

typedef struct _main_file        main_file;
typedef struct _main_extcomp     main_extcomp;
typedef struct _main_blklru      main_blklru;
typedef struct _main_blklru_list main_blklru_list;

/* The main_file object supports abstract system calls like open, close, read, write, seek,
 * stat.  The program uses these to represent both seekable files and non-seekable files.
 * Source files must be seekable, but the target input and any output file do not require
 * seekability.
 */
struct _main_file
{
#if XD3_STDIO
  FILE               *file;
#elif XD3_POSIX
  int                 file;
#elif XD3_WIN32
  HANDLE              file;  
#endif

  int                 mode;          /* XO_READ and XO_WRITE */
  const char         *filename;      /* File name or /dev/stdin, /dev/stdout, /dev/stderr. */
  char               *filename_copy; /* File name or /dev/stdin, /dev/stdout, /dev/stderr. */
  const char         *realname;      /* File name or /dev/stdin, /dev/stdout, /dev/stderr. */
  const main_extcomp *compressor; /* External compression struct. */
  int                 flags;      /* RD_FIRST, RD_NONEXTERNAL, ... */
  xoff_t              nread;      /* for input position */
  xoff_t              nwrite;     /* for output position */
};

/* Various strings and magic values used to detect and call external compression.  See
 * below for examples. */
struct _main_extcomp
{
  const char    *recomp_cmdname;
  const char    *recomp_options;

  const char    *decomp_cmdname;
  const char    *decomp_options;

  const char    *ident;
  const char    *magic;
  int            magic_size;
  int            flags;
};

/* This file implements a small LRU of source blocks.  For encoding purposes,
 * we prevent paging in blocks we've already scanned in the source (return
 * XD3_NOTAVAIL). */
struct _main_blklru_list
{
  main_blklru_list  *next;
  main_blklru_list  *prev;
};

struct _main_blklru
{
  uint8_t         *blk;
  xoff_t           blkno;
  main_blklru_list  link;
};

#define LRU_SIZE 32U
#define XD3_MINSRCWINSZ XD3_ALLOCSIZE

/* ... represented as a list (no cache index). */
XD3_MAKELIST(main_blklru_list,main_blklru,link);

/* Program options: various command line flags and options. */
static int         option_stdout             = 0;
static int         option_force              = 0;
static int         option_verbose            = 0;
static int         option_quiet              = 0;
static int         option_level              = 5;
static int         option_use_appheader      = 1;
static uint8_t*    option_appheader          = NULL;
static int         option_use_secondary      = /* until-standardized, leave this off */ 0;
static char*       option_secondary          = NULL;
static int         option_use_checksum       = 1;
static int         option_use_altcodetable   = 0;
static char*       option_smatch_config      = NULL;
static int         option_no_compress        = 0;
static int         option_no_output          = 0; /* go through the motions, but do not open or write output */
static const char *option_source_filename    = NULL;

static usize_t     option_winsize            = XD3_DEFAULT_WINSIZE;
static usize_t     option_srcwinsz           = XD3_DEFAULT_SRCWINSZ;
static int         option_srcwinsz_set       = 0;

/* This controls the number of times main repeats itself, only for profiling. */
static int option_profile_cnt = 0;

/* These variables are supressed to avoid their use w/o support.  main() warns
 * appropriately. */
#if EXTERNAL_COMPRESSION
static int         option_decompress_inputs  = 1;
static int         option_recompress_outputs = 1;
#endif

/* This is for comparing "printdelta" output without attention to
 * copy-instruction modes. */
#if VCDIFF_TOOLS
static int         option_print_cpymode = 1;
#endif

/* Static variables */
IF_DEBUG(static int main_mallocs = 0;)

static char*          program_name = NULL;
static uint8_t*       appheader_used = NULL;
static uint8_t*       main_bdata = NULL;

/* The LRU: obviously this is shared by all callers. */
static int               lru_size = 0;
static main_blklru      *lru = NULL;  /* array of lru_size elts */
static main_blklru_list  lru_list;
static main_blklru_list  lru_free;
static int               do_not_lru = 0;  /* set to avoid lru, instead discard oldest */

static int lru_hits   = 0;
static int lru_misses = 0;
static int lru_filled = 0;

/* Hacks for VCDIFF tools */
static int allow_fake_source = 0;

/* This array of compressor types is compiled even if EXTERNAL_COMPRESSION is false just so
 * the program knows the mapping of IDENT->NAME. */
static main_extcomp extcomp_types[] =
{
  /* The entry for xdelta3/1 must be 0/1 because the program_names are set there. */
  { "xdelta3",  "-cfq",  "xdelta3",    "-dcfq",  "X", "\xd6\xc3\xc4", 3, RD_NONEXTERNAL },
  { "bzip2",    "-cf",   "bzip2",      "-dcf",   "B", "BZh",          3, 0 },
  { "gzip",     "-cf",   "gzip",       "-dcf",   "G", "\037\213",     2, 0 },
  { "compress", "-cf",   "uncompress", "-cf",    "Z", "\037\235",     2, 0 },

  /* TODO: add commandline support for magic-less formats */
  /*{ "lzma",     "-cf",   "lzma",       "-dcf",   "M", "]\000",        2, 0 },*/
};

static void main_get_appheader (xd3_stream *stream, main_file *ifile,
				main_file *output, main_file *sfile);

static int main_help (void);

static int
main_version (void)
{
  /* $Format: "  P(RINT \"VERSION=3.$Xdelta3Version$\\n\");" $ */
  P(RINT "VERSION=3.0k\n");
  return EXIT_SUCCESS;
}

static int
main_config (void)
{
  main_version ();

  P(RINT "EXTERNAL_COMPRESSION=%d\n", EXTERNAL_COMPRESSION);
  P(RINT "GENERIC_ENCODE_TABLES=%d\n", GENERIC_ENCODE_TABLES);
  P(RINT "GENERIC_ENCODE_TABLES_COMPUTE=%d\n", GENERIC_ENCODE_TABLES_COMPUTE);
  P(RINT "REGRESSION_TEST=%d\n", REGRESSION_TEST);
  P(RINT "SECONDARY_DJW=%d\n", SECONDARY_DJW);
  P(RINT "SECONDARY_FGK=%d\n", SECONDARY_FGK);
  P(RINT "VCDIFF_TOOLS=%d\n", VCDIFF_TOOLS);
  P(RINT "XD3_ALLOCSIZE=%d\n", XD3_ALLOCSIZE);
  P(RINT "XD3_DEBUG=%d\n", XD3_DEBUG);
  P(RINT "XD3_DEFAULT_CKSUM_SIZE=%d\n", XD3_DEFAULT_CKSUM_ADVANCE);
  P(RINT "XD3_DEFAULT_IOPT_SIZE=%d\n", XD3_DEFAULT_IOPT_SIZE);
  P(RINT "XD3_DEFAULT_SPREVSZ=%d\n", XD3_DEFAULT_SPREVSZ);
  P(RINT "XD3_DEFAULT_SRCWINSZ=%d\n", XD3_DEFAULT_SRCWINSZ);
  P(RINT "XD3_DEFAULT_WINSIZE=%d\n", XD3_DEFAULT_WINSIZE);
  P(RINT "XD3_ENCODER=%d\n", XD3_ENCODER);
  P(RINT "XD3_HARDMAXWINSIZE=%d\n", XD3_HARDMAXWINSIZE);
  P(RINT "XD3_NODECOMPRESSSIZE=%d\n", XD3_NODECOMPRESSSIZE);
  P(RINT "XD3_USE_LARGEFILE64=%d\n", XD3_USE_LARGEFILE64);
  P(RINT "XD3_POSIX=%d\n", XD3_POSIX);
  P(RINT "XD3_STDIO=%d\n", XD3_STDIO);
  P(RINT "XD3_WIN32=%d\n", XD3_WIN32);

  return EXIT_SUCCESS;
}

static void
reset_defaults()
{
  option_stdout = 0;
  option_force = 0;
  option_verbose = 0;
  option_quiet = 0;
  option_level = 5;
  option_use_appheader = 1;
  option_appheader = NULL;
  option_use_secondary = 0;
  option_secondary = NULL;
  option_use_checksum = 1;
  option_use_altcodetable = 0;
  option_smatch_config = NULL;
  option_no_compress = 0;
  option_no_output = 0;
  option_source_filename = NULL;
  option_winsize = XD3_DEFAULT_WINSIZE;
  option_srcwinsz = XD3_DEFAULT_SRCWINSZ;
  option_srcwinsz_set = 0;
  option_profile_cnt = 0;
#if EXTERNAL_COMPRESSION
  option_decompress_inputs  = 1;
  option_recompress_outputs = 1;
#endif
#if VCDIFF_TOOLS
  option_print_cpymode = 1;
#endif
  program_name = NULL;
  appheader_used = NULL;
  main_bdata = NULL;
  lru_size = 0;
  lru = NULL;
  do_not_lru = 0;
  lru_hits   = 0;
  lru_misses = 0;
  lru_filled = 0;
  allow_fake_source = 0;
  option_winsize = XD3_DEFAULT_WINSIZE;
  option_srcwinsz = XD3_DEFAULT_SRCWINSZ;
  option_srcwinsz_set = 0;
  option_smatch_config = NULL;
}

static void*
main_malloc1 (usize_t size)
{
  void* r = malloc (size);
  if (r == NULL) { XPR(NT "malloc: %s\n", xd3_mainerror (ENOMEM)); }
  else if (option_verbose > 3) { XPR(NT "malloc: %u: %p\n", size, r); }
  return r;
}

static void*
main_malloc (usize_t size)
{
  void *r = main_malloc1 (size);
   if (r) { IF_DEBUG (main_mallocs += 1); }
  return r;
}

static void*
main_alloc (void   *opaque,
	    usize_t  items,
	    usize_t  size)
{
  return main_malloc1 (items * size);
}

static void
main_free1 (void *opaque, void *ptr)
{
  if (option_verbose > 3) { XPR(NT "free: %p\n", ptr); }
  free (ptr);
}

static void
main_free (void *ptr)
{
  if (ptr)
    {
      IF_DEBUG (main_mallocs -= 1);
      main_free1 (NULL, ptr);
      IF_DEBUG (XD3_ASSERT(main_mallocs >= 0));
    }
}

/* This ensures that (ret = errno) always indicates failure, in case errno was
 * accidentally not set.  If this prints there's a bug somewhere. */
static int
get_errno (void)
{
#ifndef _WIN32
  if (errno == 0)
    {
      XPR(NT "you found a bug: expected errno != 0\n");
      errno = XD3_INTERNAL;
    }
  return errno;
#else
  DWORD errNum = GetLastError();
  if (errNum == NO_ERROR) {
	  errNum = XD3_INTERNAL;
  }
  return errNum;
#endif
}

const char* 
xd3_mainerror(int err_num) {
#ifndef _WIN32
	return strerror(err_num);
#else
	static char err_buf[256];
	const char* x = xd3_strerror (err_num);
	if (x != NULL) {
		return x;
	}
	memset (err_buf, 0, 256);
	FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, err_num, 
		MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
		err_buf, 256, NULL);
	return err_buf;
#endif
}

static long
get_millisecs_now (void)
{
#ifndef _WIN32
  struct timeval tv;

  gettimeofday (& tv, NULL);

  return (tv.tv_sec) * 1000L + (tv.tv_usec) / 1000;
#else
  // Found this in an example on www.codeproject.com
  // It doesn't matter that the offset is Jan 1, 1601
  // Result is the numbre of 100 nanosecond units
  // 100ns * 10,000 = 1ms
  SYSTEMTIME st;
  FILETIME ft;
  __int64 *pi = (__int64*)&ft;
  GetLocalTime(&st);
  SystemTimeToFileTime(&st, &ft);
  return (long)((*pi) / 10000);
#endif
}

/* Always >= 1 millisec, right? */
static long
get_millisecs_since (void)
{
  static long last = 0;
  long now = get_millisecs_now();
  long diff = now - last;
  last = now;
  return diff;
}

static char*
main_format_bcnt (xoff_t r, char *buf)
{
  static const char* fmts[] = { "B", "KB", "MB", "GB" };
  int i;

  for (i = 0; i < SIZEOF_ARRAY(fmts); i += 1)
    {
      if (r < 10 * 1e3 || i == (-1 + (int)SIZEOF_ARRAY(fmts)))
  	    {
	      sprintf (buf, "%"Q"u %s", r, fmts[i]);
	      break;
	    }
      r /= 1000;
    }
  return buf;
}

static char*
main_format_rate (xoff_t bytes, long millis, char *buf)
{
  xoff_t r = (xoff_t)(1.0 * bytes / (1.0 * millis / 1000.0));
  static char lbuf[32];

  main_format_bcnt (r, lbuf);
  sprintf (buf, "%s/sec", lbuf);
  return buf;
}

static char*
main_format_millis (long millis, char *buf)
{
  if (millis < 1000)       { sprintf (buf, "%lu ms", millis); }
  else if (millis < 10000) { sprintf (buf, "%.1f sec", millis / 1000.0); }  
  else                     { sprintf (buf, "%lu sec", millis / 1000L); }  
  return buf;
}

/* A safe version of strtol for xoff_t. */
static int
main_strtoxoff (const char* s, xoff_t *xo, char which)
{
  char *e;
  xoff_t x;

  XD3_ASSERT(s && *s != 0);

  {
    /* Should check LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX? */
#if SIZEOF_XOFF_T == 4
    long xx = strtol (s, &e, 0);
#else
    long long xx = strtoll (s, &e, 0);
#endif

    if (xx < 0)
      {
	XPR(NT "-%c: negative integer: %s\n", which, s);
	return EXIT_FAILURE;
      }

    x = xx;
  }

  if (*e != 0)
    {
      XPR(NT "-%c: invalid integer: %s\n", which, s);
      return EXIT_FAILURE;
    }

  (*xo) = x;
  return 0;
}

static int
main_atou (const char* arg, usize_t *xo, usize_t low, usize_t high, char which)
{
  xoff_t x;
  int ret;

  if ((ret = main_strtoxoff (arg, & x, which))) { return ret; }

  if (x < low)
    {
      XPR(NT "-%c: minimum value: %u\n", which, low);
      return EXIT_FAILURE;
    }
  if (high == 0)
    {
      high = USIZE_T_MAX;
    }
  if (x > high)
    {
      XPR(NT "-%c: maximum value: %u\n", which, high);
      return EXIT_FAILURE;
    }
  (*xo) = (usize_t)x;
  return 0;
}

/******************************************************************************************
 FILE BASICS
 ******************************************************************************************/

/* With all the variation in file system-call semantics, arguments, return values and
 * error-handling for the POSIX and STDIO file APIs, the insides of these functions make
 * me sick, which is why these wrappers exist. */

#define XOPEN_OPNAME (xfile->mode == XO_READ ? "read" : "write")
#define XOPEN_STDIO  (xfile->mode == XO_READ ? "rb" : "wb")
#define XOPEN_POSIX  (xfile->mode == XO_READ ? O_RDONLY : O_WRONLY | O_CREAT | O_TRUNC)
#define XOPEN_MODE   (xfile->mode == XO_READ ? 0 : 0666)

#define XF_ERROR(op, name, ret) XPR(NT "file %s failed: %s: %s: %s\n", (op), XOPEN_OPNAME, (name), xd3_mainerror (ret))

#if XD3_STDIO
#define XFNO(f) fileno(f->file)
#define XSTDOUT_XF(f) { (f)->file = stdout; (f)->filename = "/dev/stdout"; }
#define XSTDERR_XF(f) { (f)->file = stderr; (f)->filename = "/dev/stderr"; }
#define XSTDIN_XF(f)  { (f)->file = stdin;  (f)->filename = "/dev/stdin"; }

#elif XD3_POSIX
#define XFNO(f) f->file
#define XSTDOUT_XF(f) { (f)->file = STDOUT_FILENO; (f)->filename = "/dev/stdout"; }
#define XSTDERR_XF(f) { (f)->file = STDERR_FILENO; (f)->filename = "/dev/stderr"; }
#define XSTDIN_XF(f)  { (f)->file = STDIN_FILENO;  (f)->filename = "/dev/stdin"; }

#elif XD3_WIN32
#define XFNO(f) -1
#define XSTDOUT_XF(f) { (f)->file = INVALID_HANDLE_VALUE; (f)->filename = "/dev/stdout"; }
#define XSTDERR_XF(f) { (f)->file = INVALID_HANDLE_VALUE; (f)->filename = "/dev/stderr"; }
#define XSTDIN_XF(f)  { (f)->file = INVALID_HANDLE_VALUE;  (f)->filename = "/dev/stdin"; }
#endif

static void
main_file_init (main_file *xfile)
{
  memset (xfile, 0, sizeof (*xfile));

#if XD3_POSIX
  xfile->file = -1;
#endif
#if XD3_WIN32
  xfile->file = INVALID_HANDLE_VALUE;
#endif
}

static void
main_file_cleanup (main_file *xfile)
{
  if (xfile->filename_copy != NULL) {
    main_free(xfile->filename_copy);
    xfile->filename_copy = NULL;
  }
}

static int
main_file_isopen (main_file *xfile)
{
#if XD3_STDIO
  return xfile->file != NULL;

#elif XD3_POSIX
  return xfile->file != -1;

#elif XD3_WIN32
  return xfile->file != INVALID_HANDLE_VALUE;
#endif
}

static int
main_file_close (main_file *xfile)
{
  int ret = 0;

  if (! main_file_isopen (xfile))
    {
      return 0;
    }

#if XD3_STDIO
  ret = fclose (xfile->file);
  xfile->file = NULL;

#elif XD3_POSIX
  ret = close (xfile->file);
  xfile->file = -1;

#elif XD3_WIN32
  if (!CloseHandle(xfile->file)) {
    ret = get_errno ();
  }
  xfile->file = INVALID_HANDLE_VALUE;
#endif

  if (ret != 0) { XF_ERROR ("close", xfile->filename, ret = get_errno ()); }
  return ret;
}

static int
main_file_open (main_file *xfile, const char* name, int mode)
{
  int ret = 0;

  xfile->mode = mode;

  XD3_ASSERT (! main_file_isopen (xfile));

#if XD3_STDIO
  xfile->file = fopen (name, XOPEN_STDIO);

  ret = (xfile->file == NULL) ? get_errno () : 0;

#elif XD3_POSIX
  if ((ret = open (name, XOPEN_POSIX, XOPEN_MODE)) < 0)
    {
      ret = get_errno ();
    }
  else
    {
      xfile->file = ret;
      ret = 0;
    }

#elif XD3_WIN32
  xfile->file = CreateFile(name, 
	  (mode == XO_READ) ? GENERIC_READ : GENERIC_WRITE,
	  0,
	  NULL,
	  (mode == XO_READ) ? OPEN_EXISTING : (option_force ? CREATE_ALWAYS : CREATE_NEW),
	  FILE_ATTRIBUTE_NORMAL,
	  NULL);
  if (xfile->file == INVALID_HANDLE_VALUE) {
	  ret = get_errno ();
  }
#endif
  if (ret) { XF_ERROR ("open", name, ret); }
  else     { xfile->realname = name; xfile->nread = 0; }
  return ret;
}

static int
main_file_stat (main_file *xfile, xoff_t *size, int err_ifnoseek)
{
  int ret = 0;
#if XD3_WIN32
  LARGE_INTEGER li;
  if (GetFileSizeEx(xfile->file, &li) == 0) {
	  ret = get_errno ();
  } else {
      *size = li.QuadPart;
  }
  // TODO: check err_ifnoseek
#else
  struct stat sbuf;
  if (fstat (XFNO (xfile), & sbuf) < 0)
    {
      ret = get_errno ();
      if (err_ifnoseek) { XF_ERROR ("stat", xfile->filename, ret); }
      return ret;
    }

  if (! S_ISREG (sbuf.st_mode))
    {
      if (err_ifnoseek) { XPR(NT "source file must be seekable: %s\n", xfile->filename); }
      return ESPIPE;
    }
  (*size) = sbuf.st_size;
#endif
  return ret;
}

static int
main_file_exists (main_file *xfile)
{
  struct stat sbuf;
  return stat (xfile->filename, & sbuf) == 0 && S_ISREG (sbuf.st_mode);
}

#if (XD3_POSIX || EXTERNAL_COMPRESSION)
/* POSIX-generic code takes a function pointer to read() or write().  This calls the
 * function repeatedly until the buffer is full or EOF.  The NREAD parameter is not
 * set for write, NULL is passed.  Return is signed, < 0 indicate errors, otherwise
 * byte count. */
typedef int (xd3_posix_func) (int fd, uint8_t *buf, usize_t size);

static int
xd3_posix_io (int fd, uint8_t *buf, usize_t size, xd3_posix_func *func, usize_t *nread)
{
  int ret;
  usize_t nproc = 0;

  while (nproc < size)
    {
      int result = (*func) (fd, buf + nproc, size - nproc);
      
      if (result < 0)
	{
	  ret = get_errno ();
	  if (ret != EAGAIN && ret != EINTR)
	    {
	      return ret;
	    }
	  result = 0;
	}

      if (nread != NULL && result == 0) { break; }

      nproc += result;
    }
  if (nread != NULL) { (*nread) = nproc; }
  return 0;
}
#endif

/* POSIX is unbuffered, while STDIO is buffered.  main_file_read() should always be called
 * on blocks. */
static int
main_file_read (main_file   *ifile,
	       uint8_t    *buf,
	       usize_t      size,
	       usize_t     *nread,
	       const char *msg)
{
  int ret = 0;

#if XD3_STDIO
  usize_t result;

  result = fread (buf, 1, size, ifile->file);

  if (result < size && ferror (ifile->file))
    {
      ret = get_errno ();
    }
  else
    {
      *nread = result;
    }

#elif XD3_POSIX
  ret = xd3_posix_io (ifile->file, buf, size, (xd3_posix_func*) &read, nread);

#elif XD3_WIN32
  DWORD nread2;
  if (ReadFile (ifile->file, buf, size, &nread2, NULL) == 0) {
	  ret = get_errno();
  } else {
      *nread = (usize_t)nread2;
  }
#endif

  if (ret)
    {
      XPR(NT "%s: %s: %s\n", msg, ifile->filename, xd3_mainerror (ret));
    }
  else
    {
      if (option_verbose > 3) { XPR(NT "main read: %s: %u\n", ifile->filename, (*nread)); }
      ifile->nread += (*nread);
    }

  return ret;
}

static int
main_file_write (main_file *ofile, uint8_t *buf, usize_t size, const char *msg)
{
  int ret = 0;

#if XD3_STDIO
  usize_t result;

  result = fwrite (buf, 1, size, ofile->file);

  if (result != size) { ret = get_errno (); }

#elif XD3_POSIX
  ret = xd3_posix_io (ofile->file, buf, size, (xd3_posix_func*) &write, NULL);

#elif XD3_WIN32
  DWORD nwrite;
  if (WriteFile(ofile->file, buf, size, &nwrite, NULL) == 0) {
	  ret = get_errno ();
  } else {
	  if (size != nwrite) {
		  XPR(NT "Incorrect write count");
		  ret = XD3_INTERNAL;
	  }
  }
#endif

  if (ret)
    {
      XPR(NT "%s: %s: %s\n", msg, ofile->filename, xd3_mainerror (ret));
    }
  else
    {
      if (option_verbose > 3) { XPR(NT "main write: %s: %u\n", ofile->filename, size); }
      ofile->nwrite += size;
    }

  return ret;
}

static int
main_file_seek (main_file *xfile, xoff_t pos)
{
  int ret = 0;

#if XD3_STDIO
  if (fseek (xfile->file, pos, SEEK_SET) != 0) { ret = get_errno (); }

#elif XD3_POSIX
  if (lseek (xfile->file, pos, SEEK_SET) != pos) { ret = get_errno (); }

#elif XD3_WIN32
  LARGE_INTEGER move, out;
  move.QuadPart = pos;
  if (SetFilePointerEx(xfile->file, move, &out, FILE_BEGIN) == 0) {
	  ret = get_errno ();
  }
#endif

  if (ret)
    {
      XPR(NT "seek failed: %s: %s\n", xfile->filename, xd3_mainerror (ret));
    }

  return ret;
}

/******************************************************************************************
 VCDIFF TOOLS
 ******************************************************************************************/

#if VCDIFF_TOOLS
/* This function prints a single VCDIFF window, mainly for debugging purposes. */
static int
main_print_window (xd3_stream* stream)
{
  int ret;
  usize_t size = 0;

  VC(UT "  Offset Code Type1 Size1 @Addr1 + Type2 Size2 @Addr2\n");

  while (stream->inst_sect.buf < stream->inst_sect.buf_max)
    {
      uint   code = stream->inst_sect.buf[0];

      if ((ret = xd3_decode_instruction (stream))) { return ret; }

      VC(UT "  %06"Q"u %03u  %s %3u", stream->dec_winstart + size, code,
	       xd3_rtype_to_string (stream->dec_current1.type, option_print_cpymode),
	       stream->dec_current1.size);

      if (stream->dec_current1.type != XD3_NOOP)
	{
	  size += stream->dec_current1.size;
	  if (stream->dec_current1.type >= XD3_CPY)
	    {
	      VC(UT " @%-6u", stream->dec_current1.addr);
	    }
	  else
	    {
	      VC(UT "        ");
	    }
	}

      if (stream->dec_current2.type != XD3_NOOP)
	{
	  size += stream->dec_current2.size;
	  VC(UT "  %s %3u",
		   xd3_rtype_to_string (stream->dec_current2.type, option_print_cpymode),
		   stream->dec_current2.size);

	  if (stream->dec_current2.type >= XD3_CPY)
	    {
	      VC(UT " @%-6u", stream->dec_current2.addr);
	    }
	}

      VC(UT "\n");
    }

  if (stream->dec_tgtlen != size && (stream->flags & XD3_SKIP_WINDOW) == 0)
    {
      XPR(NT "target window size inconsistency");
      return XD3_INTERNAL;
    }

  if (stream->dec_position != stream->dec_maxpos)
    {
      XPR(NT "target window position inconsistency");
      return XD3_INTERNAL;
    }

  if (stream->addr_sect.buf != stream->addr_sect.buf_max)
    {
      XPR(NT "address section inconsistency");
      return XD3_INTERNAL;
    }

  return 0;
}

static void
main_print_vcdiff_file (main_file *file, const char *type)
{
  if (file->filename)   { VC(UT "XDELTA filename (%s):     %s\n", type, file->filename); }
  if (file->compressor) { VC(UT "XDELTA ext comp (%s):     %s\n", type, file->compressor->recomp_cmdname); }  
}

/* This function prints a VCDIFF input, mainly for debugging purposes. */
static int
main_print_func (xd3_stream* stream, main_file *xfile)
{
  int ret;
  if (stream->dec_winstart == 0)
    {
      VC(UT "VCDIFF version:               0\n");

      VC(UT "VCDIFF header size:           %d\n", stream->dec_hdrsize);
      VC(UT "VCDIFF header indicator:      ");
      if ((stream->dec_hdr_ind & VCD_SECONDARY) != 0) VC(UT "VCD_SECONDARY ");
      if ((stream->dec_hdr_ind & VCD_CODETABLE) != 0) VC(UT "VCD_CODETABLE ");
      if ((stream->dec_hdr_ind & VCD_APPHEADER) != 0) VC(UT "VCD_APPHEADER ");
      if (stream->dec_hdr_ind == 0) VC(UT "none");
      VC(UT "\n");

      IF_SEC(VC(UT "VCDIFF secondary compressor:  %s\n", stream->sec_type ? stream->sec_type->name : "none"));
      IF_NSEC(VC(UT "VCDIFF secondary compressor: unsupported\n"));

      if (stream->dec_hdr_ind & VCD_APPHEADER)
	{
	  uint8_t *apphead;
	  usize_t appheadsz;
	  ret = xd3_get_appheader (stream, & apphead, & appheadsz);

	  if (ret == 0 && appheadsz > 0)
	    {
	      int sq = option_quiet;
	      main_file i, o, s;
	      XD3_ASSERT (apphead != NULL);
	      VC(UT "VCDIFF application header:    ");
	      fwrite (apphead, 1, appheadsz, stdout);
	      VC(UT "\n");

	      main_file_init (& i);
	      main_file_init (& o);
	      main_file_init (& s);
	      option_quiet = 1;
	      main_get_appheader (stream, &i, & o, & s);
	      option_quiet = sq;
	      main_print_vcdiff_file (& o, "output");
	      main_print_vcdiff_file (& s, "source");
	      main_file_cleanup (& i);
	      main_file_cleanup (& o);
	      main_file_cleanup (& s);
	    }
	}
    }
  else
    {
      VC(UT "\n");
    }

  VC(UT "VCDIFF window number:         %"Q"u\n", stream->current_window);
  VC(UT "VCDIFF window indicator:      ");
  if ((stream->dec_win_ind & VCD_SOURCE) != 0) VC(UT "VCD_SOURCE ");
  if ((stream->dec_win_ind & VCD_TARGET) != 0) VC(UT "VCD_TARGET ");
  if ((stream->dec_win_ind & VCD_ADLER32) != 0) VC(UT "VCD_ADLER32 ");
  if (stream->dec_win_ind == 0) VC(UT "none");
  VC(UT "\n");

  if ((stream->dec_win_ind & VCD_ADLER32) != 0)
    {
      VC(UT "VCDIFF adler32 checksum:      %08X\n", stream->dec_adler32);
    }

  if (stream->dec_del_ind != 0)
    {
      VC(UT "VCDIFF delta indicator:       ");
      if ((stream->dec_del_ind & VCD_DATACOMP) != 0) VC(UT "VCD_DATACOMP ");
      if ((stream->dec_del_ind & VCD_INSTCOMP) != 0) VC(UT "VCD_INSTCOMP ");
      if ((stream->dec_del_ind & VCD_ADDRCOMP) != 0) VC(UT "VCD_ADDRCOMP ");
      if (stream->dec_del_ind == 0) VC(UT "none");
      VC(UT "\n");
    }

  if (stream->dec_winstart != 0)
    {
      VC(UT "VCDIFF window at offset:      %"Q"u\n", stream->dec_winstart);
    }

  if (SRCORTGT (stream->dec_win_ind))
    {
      VC(UT "VCDIFF copy window length:    %u\n", stream->dec_cpylen);
      VC(UT "VCDIFF copy window offset:    %"Q"u\n", stream->dec_cpyoff);
    }

  VC(UT "VCDIFF delta encoding length: %u\n", stream->dec_enclen);
  VC(UT "VCDIFF target window length:  %u\n", stream->dec_tgtlen);

  VC(UT "VCDIFF data section length:   %u\n", stream->data_sect.size);
  VC(UT "VCDIFF inst section length:   %u\n", stream->inst_sect.size);
  VC(UT "VCDIFF addr section length:   %u\n", stream->addr_sect.size);

  ret = 0; 
  if ((stream->flags & XD3_JUST_HDR) != 0)
    {
      /* Print a header -- finished! */
      ret = PRINTHDR_SPECIAL;
    }
  else if ((stream->flags & XD3_SKIP_WINDOW) == 0)
    {
      ret = main_print_window (stream);
    }

  return ret;
}
#endif /* VCDIFF_TOOLS */

/******************************************************************************************
 Input decompression, output recompression
 ******************************************************************************************/

#if EXTERNAL_COMPRESSION
/* This is tricky POSIX-specific code with lots of fork(), pipe(), dup(), waitpid(), and
 * exec() business.  Most of this code originated in PRCS1, which did automatic
 * package-file decompression.  It works with both XD3_POSIX and XD3_STDIO file
 * disciplines.
 *
 * To automatically detect compressed inputs requires a child process to reconstruct the
 * input stream, which was advanced in order to detect compression, because it may not be
 * seekable.  In other words, the main program reads part of the input stream, and if it
 * detects a compressed input it then forks a pipe copier process, which copies the
 * first-read block out of the main-program's memory, then streams the remaining
 * compressed input into the input-decompression pipe.
 */

#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>

/* Remember which pipe FD is which. */
#define PIPE_READ_FD  0
#define PIPE_WRITE_FD 1

static pid_t ext_subprocs[2];
static char* ext_tmpfile = NULL;

/* Like write(), but makes repeated calls to empty the buffer. */
static int
main_pipe_write (int outfd, const uint8_t *exist_buf, usize_t remain)
{
  int ret;

  if ((ret = xd3_posix_io (outfd, (uint8_t*) exist_buf, remain, (xd3_posix_func*) &write, NULL)))
    {
      XPR(NT "pipe write failed: %s", xd3_mainerror (ret));
      return ret;
    }

  return 0;
}

/* A simple error-reporting waitpid interface. */
static int
main_waitpid_check(pid_t pid)
{
  int status;
  int ret = 0;

  if (waitpid (pid, & status, 0) < 0)
    {
      ret = get_errno ();
      XPR(NT "compression subprocess: wait: %s\n", xd3_mainerror (ret));
    }
  else if (! WIFEXITED (status))
    {
      ret = ECHILD;
      XPR(NT "compression subprocess: signal %d\n",
	 WIFSIGNALED (status) ? WTERMSIG (status) : WSTOPSIG (status));
    }
  else if (WEXITSTATUS (status) != 0)
    {
      ret = ECHILD;
      XPR(NT "compression subprocess: exit %d\n", WEXITSTATUS (status));
    }

  return ret;
}

/* Wait for any existing child processes to check for abnormal exit. */
static int
main_external_compression_finish (void)
{
  int i;
  int ret;

  for (i = 0; i < 2; i += 1)
    {
      if (! ext_subprocs[i]) { continue; }

      if ((ret = main_waitpid_check (ext_subprocs[i])))
	{
	  return ret;
	}
    }

  return 0;
}

/* This runs as a forked process of main_input_decompress_setup() to copy input to the
 * decompression process.  First, the available input is copied out of the existing
 * buffer, then the buffer is reused to continue reading from the compressed input
 * file. */
static int
main_pipe_copier (uint8_t    *pipe_buf,
		  usize_t      pipe_bufsize,
		  usize_t      nread,
		  main_file   *ifile,
		  int         outfd)
{
  int ret;

  for (;;)
    {
      if (nread > 0 && (ret = main_pipe_write (outfd, pipe_buf, nread)))
	{
	  return ret;
	}

      if (nread < pipe_bufsize)
	{
	  break;
	}

      if ((ret = main_file_read (ifile, pipe_buf, pipe_bufsize, & nread, "pipe read failed")) < 0)
	{
	  return ret;
	}
    }

  return 0;
}

/* This function is called after we have read some amount of data from the input file and
 * detected a compressed input.  Here we start a decompression subprocess by forking
 * twice.  The first process runs the decompression command, the second process copies
 * data to the input of the first. */
static int
main_input_decompress_setup (const main_extcomp     *decomp,
			     main_file              *ifile,
			     uint8_t               *input_buf,
			     usize_t                 input_bufsize,
			     uint8_t               *pipe_buf,
			     usize_t                 pipe_bufsize,
			     usize_t                 pipe_avail,
			     usize_t                *nread)
{
  int outpipefd[2], inpipefd[2];  /* The two pipes: input and output file descriptors. */
  int input_fd = -1;              /* The resulting input_fd (output of decompression). */
  pid_t decomp_id, copier_id;     /* The two subprocs. */
  int ret;

  outpipefd[0] = outpipefd[1] = -1;
  inpipefd[0]  = inpipefd[1]  = -1;

  if (pipe (outpipefd) || pipe (inpipefd))
    {
      XPR(NT "pipe failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  if ((decomp_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  /* The first child runs the decompression process: */
  if (decomp_id == 0)
    {
      /* Setup pipes: write to the outpipe, read from the inpipe. */
      if (dup2 (outpipefd[PIPE_WRITE_FD], STDOUT_FILENO) < 0 ||
	  dup2 (inpipefd[PIPE_READ_FD], STDIN_FILENO) < 0 ||
	  close (outpipefd[PIPE_READ_FD]) ||
	  close (outpipefd[PIPE_WRITE_FD]) ||
	  close (inpipefd[PIPE_READ_FD]) ||
	  close (inpipefd[PIPE_WRITE_FD]) ||
	  execlp (decomp->decomp_cmdname, decomp->decomp_cmdname, decomp->decomp_options, NULL))
	{
	  XPR(NT "child process %s failed to execute: %s\n", decomp->decomp_cmdname, xd3_mainerror (get_errno ()));
	}

      _exit (127);
    }

  ext_subprocs[0] = decomp_id;

  if ((copier_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  /* The second child runs the copier process: */
  if (copier_id == 0)
    {
      int exitval = 0;

      if (close (inpipefd[PIPE_READ_FD]) ||
	  main_pipe_copier (pipe_buf, pipe_bufsize, pipe_avail, ifile, inpipefd[PIPE_WRITE_FD]) ||
	  close (inpipefd[PIPE_WRITE_FD]))
	{
	  XPR(NT "child copier process failed: %s\n", xd3_mainerror (get_errno ()));
	  exitval = 1;
	}

      _exit (exitval);
    }

  ext_subprocs[1] = copier_id;

  /* The parent closes both pipes after duplicating the output of compression. */
  input_fd = dup (outpipefd[PIPE_READ_FD]);

  if (input_fd < 0 ||
      main_file_close (ifile) ||
      close (outpipefd[PIPE_READ_FD]) ||
      close (outpipefd[PIPE_WRITE_FD]) ||
      close (inpipefd[PIPE_READ_FD]) ||
      close (inpipefd[PIPE_WRITE_FD]))
    {
      XPR(NT "dup/close failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#if XD3_STDIO
  /* Note: fdopen() acquires the fd, closes it when finished. */
  if ((ifile->file = fdopen (input_fd, "r")) == NULL)
    {
      XPR(NT "fdopen failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#elif XD3_POSIX
  ifile->file = input_fd;
#endif

  ifile->compressor = decomp;

  /* Now the input file is decompressed. */
  return main_file_read (ifile, input_buf, input_bufsize, nread, "input decompression failed");

 pipe_cleanup:
  close (input_fd);
  close (outpipefd[PIPE_READ_FD]);
  close (outpipefd[PIPE_WRITE_FD]);
  close (inpipefd[PIPE_READ_FD]);
  close (inpipefd[PIPE_WRITE_FD]);
  return ret;
}


/* This routine is called when the first buffer of input data is read by the main program
 * (unless input decompression is disabled by command-line option).  If it recognizes the
 * magic number of a known input type it invokes decompression.
 *
 * Skips decompression if the decompression type or the file type is RD_NONEXTERNAL.
 *
 * Behaves exactly like main_file_read, otherwise.
 *
 * This function uses a separate buffer to read the first small block of input.  If a
 * compressed input is detected, the separate buffer is passed to the pipe copier.  This
 * avoids using the same size buffer in both cases. */
static int
main_decompress_input_check (main_file   *ifile,
			    uint8_t    *input_buf,
			    usize_t      input_size,
			    usize_t     *nread)
{
  int i;
  int ret;
  uint8_t check_buf[XD3_ALLOCSIZE];
  usize_t  check_nread;

  if ((ret = main_file_read (ifile, check_buf, min (input_size, XD3_ALLOCSIZE), & check_nread, "input read failed")))
    {
      return ret;
    }

  for (i = 0; i < SIZEOF_ARRAY (extcomp_types); i += 1)
    {
      const main_extcomp *decomp = & extcomp_types[i];

      if ((check_nread > decomp->magic_size) &&
	  /* The following expr skips decompression if we are trying to read a VCDIFF
	   * input and that is the magic number. */
	  !((decomp->flags & RD_NONEXTERNAL) && (ifile->flags & RD_NONEXTERNAL)) &&
	  memcmp (check_buf, decomp->magic, decomp->magic_size) == 0)
	{
	  if (! option_quiet)
	    {
	      XPR(NT "%s | %s %s\n",
		 ifile->filename,
		 decomp->decomp_cmdname,
		 decomp->decomp_options);
	    }

	  return main_input_decompress_setup (decomp, ifile,
					      input_buf, input_size,
					      check_buf, XD3_ALLOCSIZE,
					      check_nread, nread);
	}
    }

  /* Now read the rest of the input block. */
  (*nread) = 0;

  if (check_nread == XD3_ALLOCSIZE)
    {
      ret = main_file_read (ifile, input_buf + XD3_ALLOCSIZE,
			    input_size - XD3_ALLOCSIZE, nread,
			    "input read failed");
    }

  memcpy (input_buf, check_buf, check_nread);

  (*nread) += check_nread;

  return 0;
}

/* This is called when the source file needs to be decompressed.  We fork/exec a
 * decompression command with the proper input and output to a temporary file. */
static int
main_decompress_source (main_file *sfile, xd3_source *source)
{
  const main_extcomp *decomp = sfile->compressor;
  pid_t decomp_id;  /* One subproc. */
  int   input_fd  = -1;
  int   output_fd = -1;
  int   ret;
  char *tmpname = NULL;
  char *tmpdir  = getenv ("TMPDIR");
  static const char tmpl[] = "/xd3src.XXXXXX";

  /* Make a template for mkstmp() */
  if (tmpdir == NULL) { tmpdir = "/tmp"; }
  if ((tmpname = main_malloc (strlen (tmpdir) + sizeof (tmpl) + 1)) == NULL) { return ENOMEM; }
  sprintf (tmpname, "%s%s", tmpdir, tmpl);

  XD3_ASSERT (ext_tmpfile == NULL);
  ext_tmpfile = tmpname;

  /* Open the output FD. */
  if ((output_fd = mkstemp (tmpname)) < 0)
    {
      XPR(NT "mkstemp failed: %s: %s", tmpname, xd3_mainerror (ret = get_errno ()));
      goto cleanup;
    }

  /* Copy the input FD, reset file position. */
  XD3_ASSERT (main_file_isopen (sfile));
#if XD3_STDIO
  if ((input_fd = dup (fileno (sfile->file))) < 0)
    {
      XPR(NT "dup failed: %s", xd3_mainerror (ret = get_errno ()));
      goto cleanup;
    }
  main_file_close (sfile);
  sfile->file = NULL;
#elif XD3_POSIX
  input_fd = sfile->file;
  sfile->file = -1;
#endif

  if ((ret = lseek (input_fd, SEEK_SET, 0)) != 0)
    {
      XPR(NT "lseek failed: : %s", xd3_mainerror (ret = get_errno ()));
      goto cleanup;
    }

  if ((decomp_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s", xd3_mainerror (ret = get_errno ()));
      goto cleanup;
    }

  /* The child runs the decompression process: */
  if (decomp_id == 0)
    {
      /* Setup pipes: write to the output file, read from the pipe. */
      if (dup2 (input_fd, STDIN_FILENO) < 0 ||
	  dup2 (output_fd, STDOUT_FILENO) < 0 ||
	  execlp (decomp->decomp_cmdname, decomp->decomp_cmdname, decomp->decomp_options, NULL))
	{
	  XPR(NT "child process %s failed to execute: %s\n",
		   decomp->decomp_cmdname, xd3_mainerror (get_errno ()));
	}

      _exit (127);
    }

  close (input_fd);
  close (output_fd);
  input_fd  = -1;
  output_fd = -1;

  /* Then wait for completion. */
  if ((ret = main_waitpid_check (decomp_id)))
    {
      goto cleanup;
    }

  /* Open/stat the decompressed source file. */
  if ((ret = main_file_open (sfile, tmpname, XO_READ))) { goto cleanup; }
  if ((ret = main_file_stat (sfile, & source->size, 1))) { goto cleanup; }
  return 0;

 cleanup:
  close (input_fd);
  close (output_fd);
  if (tmpname) { free (tmpname); }
  ext_tmpfile = NULL;
  return ret;
}

/* Initiate re-compression of the output stream.  This is easier than input decompression
 * because we know beforehand that the stream will be compressed, whereas the input has
 * already been read when we decide it should be decompressed.  Thus, it only requires one
 * subprocess and one pipe. */
static int
main_recompress_output (main_file *ofile)
{
  pid_t recomp_id;  /* One subproc. */
  int   pipefd[2];  /* One pipe. */
  int   output_fd = -1;
  int   ret;
  const main_extcomp *recomp = ofile->compressor;

  pipefd[0] = pipefd[1] = -1;

  if (pipe (pipefd))
    {
      XPR(NT "pipe failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  if ((recomp_id = fork ()) < 0)
    {
      XPR(NT "fork failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

  /* The child runs the recompression process: */
  if (recomp_id == 0)
    {
      /* Setup pipes: write to the output file, read from the pipe. */
      if (dup2 (XFNO (ofile), STDOUT_FILENO) < 0 ||
	  dup2 (pipefd[PIPE_READ_FD], STDIN_FILENO) < 0 ||
	  close (pipefd[PIPE_READ_FD]) ||
	  close (pipefd[PIPE_WRITE_FD]) ||
	  execlp (recomp->recomp_cmdname, recomp->recomp_cmdname, recomp->recomp_options, NULL))
	{
	  XPR(NT "child process %s failed to execute: %s\n", recomp->recomp_cmdname, xd3_mainerror (get_errno ()));
	}

      _exit (127);
    }

  ext_subprocs[0] = recomp_id;

  /* The parent closes both pipes after duplicating the output-fd for writing to the
   * compression pipe. */
  output_fd = dup (pipefd[PIPE_WRITE_FD]);

  if (output_fd < 0 ||
      main_file_close (ofile) ||
      close (pipefd[PIPE_READ_FD]) ||
      close (pipefd[PIPE_WRITE_FD]))
    {
      XPR(NT "close failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#if XD3_STDIO
  /* Note: fdopen() acquires the fd, closes it when finished. */
  if ((ofile->file = fdopen (output_fd, "w")) == NULL)
    {
      XPR(NT "fdopen failed: %s\n", xd3_mainerror (ret = get_errno ()));
      goto pipe_cleanup;
    }

#elif XD3_POSIX
  ofile->file = output_fd;
#endif

  /* Now the output file will be compressed. */
  return 0;

 pipe_cleanup:
  close (output_fd);
  close (pipefd[PIPE_READ_FD]);
  close (pipefd[PIPE_WRITE_FD]);
  return ret;
}
#endif /* EXTERNAL_COMPRESSION */

/* Identify the compressor that was used based on its ident string, which is passed in the
 * application header. */
static const main_extcomp*
main_ident_compressor (const char *ident)
{
  int i;

  for (i = 0; i < SIZEOF_ARRAY (extcomp_types); i += 1)
    {
      if (strcmp (extcomp_types[i].ident, ident) == 0)
	{
	  return & extcomp_types[i];
	}
    }

  return NULL;
}

/* Return the main_extcomp record to use for this identifier, if possible. */
static const main_extcomp*
main_get_compressor (const char *ident)
{
  const main_extcomp *ext = main_ident_compressor (ident);

  if (ext == NULL)
    {
      if (! option_quiet)
	{
	  XPR(NT "warning: cannot recompress output: "
		   "unrecognized external compression ID: %s\n", ident);
	}
      return NULL;
    }
  else if (! EXTERNAL_COMPRESSION)
    {
      if (! option_quiet)
	{
	  XPR(NT "warning: external support not compiled: "
		   "original input was compressed: %s\n", ext->recomp_cmdname);
	}
      return NULL;
    }
  else
    {
      return ext;
    }
}

/******************************************************************************************
 APPLICATION HEADER
 ******************************************************************************************/

#if XD3_ENCODER
static const char*
main_apphead_string (const char* x)
{
  const char *y;

  if (x == NULL) { return ""; }

  if (strcmp (x, "/dev/stdin") == 0 ||
      strcmp (x, "/dev/stdout") == 0 ||
      strcmp (x, "/dev/stderr") == 0) { return "-"; }

  // TODO: this is not portable
  return (y = strrchr (x, '/')) == NULL ? x : y + 1;
}

static int
main_set_appheader (xd3_stream *stream, main_file *input, main_file *sfile)
{
  /* The user may disable the application header.  Once the appheader is set, this
   * disables setting it again. */
  if (appheader_used || ! option_use_appheader) { return 0; }

  /* The user may specify the application header, otherwise format the default header. */
  if (option_appheader)
    {
      appheader_used = option_appheader;
    }
  else
    {
      const char *iname;
      const char *icomp;
      const char *sname;
      const char *scomp;
      int len;

      iname = main_apphead_string (input->filename);
      icomp = (input->compressor == NULL) ? "" : input->compressor->ident;
      len = strlen (iname) + strlen (icomp) + 2;

      if (sfile->filename != NULL)
	{
	  sname = main_apphead_string (sfile->filename);
	  scomp = (sfile->compressor == NULL) ? "" : sfile->compressor->ident;
	  len += strlen (sname) + strlen (scomp) + 2;
	}
      else
	{
	  sname = scomp = "";
	}

      if ((appheader_used = main_malloc (len)) == NULL)
	{
	  return ENOMEM;
	}

      if (sfile->filename == NULL)
	{
	  sprintf ((char*)appheader_used, "%s/%s", iname, icomp);
	}
      else
	{
	  sprintf ((char*)appheader_used, "%s/%s/%s/%s", iname, icomp, sname, scomp);
	}
    }

  xd3_set_appheader (stream, appheader_used, strlen ((char*)appheader_used));

  return 0;
}
#endif

static void
main_get_appheader_params (main_file *file, char **parsed, int output, const char *type,
			   main_file *other)
{
  /* Set the filename if it was not specified.  If output, option_stdout (-c) overrides. */
  if (file->filename == NULL && ! (output && option_stdout) && strcmp (parsed[0], "-") != 0)
    {
      file->filename = parsed[0];

      if (other->filename != NULL) {
	/* Take directory from the other file, if it has one. */
	char *last_slash = strrchr(other->filename, '/');

	if (last_slash != NULL) {
	  int dlen = last_slash - other->filename;

	  XD3_ASSERT(file->filename_copy == NULL);
	  file->filename_copy = main_malloc(dlen + 2 + strlen(file->filename));

	  strncpy(file->filename_copy, other->filename, dlen);
	  file->filename_copy[dlen] = '/';
	  strcpy(file->filename_copy + dlen + 1, parsed[0]);

	  file->filename = file->filename_copy;
	}
      }

      if (! option_quiet)
	{
	  XPR(NT "using default %s filename: %s\n", type, file->filename);
	}
    }

  /* Set the compressor, initiate de/recompression later. */
  if (file->compressor == NULL && *parsed[1] != 0)
    {
      file->compressor = main_get_compressor (parsed[1]);
    }
}

static void
main_get_appheader (xd3_stream *stream, main_file *ifile, main_file *output, main_file *sfile)
{
  uint8_t *apphead;
  usize_t appheadsz;
  int ret;

  /* The user may disable the application header.  Once the appheader is set, this
   * disables setting it again. */
  if (! option_use_appheader) { return; }

  ret = xd3_get_appheader (stream, & apphead, & appheadsz);

  /* Ignore failure, it only means we haven't received a header yet. */
  if (ret != 0) { return; }

  if (appheadsz > 0)
    {
      char *start = (char*)apphead;
      char *slash;
      int   place = 0;
      char *parsed[4];

      memset (parsed, 0, sizeof (parsed));

      while ((slash = strchr (start, '/')) != NULL)
	{
	  *slash = 0;
	  parsed[place++] = start;
	  start = slash + 1;
	}

      parsed[place++] = start;

      /* First take the output parameters. */
      if (place == 2 || place == 4)
	{
	  main_get_appheader_params (output, parsed, 1, "output", ifile);
	}

      /* Then take the source parameters. */
      if (place == 4)
	{
	  main_get_appheader_params (sfile, parsed+2, 0, "source", ifile);
	}
    }

  option_use_appheader = 0;
  return;
}

/******************************************************************************************
 Main I/O routines
 ******************************************************************************************/

/* This function acts like the above except it may also try to recognize a compressed
 * input when the first buffer of data is read.  The EXTERNAL_COMPRESSION code is called
 * to search for magic numbers. */
static int
main_read_primary_input (main_file   *ifile,
			 uint8_t    *buf,
			 usize_t      size,
			 usize_t     *nread)
{
#if EXTERNAL_COMPRESSION
  if (option_decompress_inputs && ifile->flags & RD_FIRST)
    {
      ifile->flags &= ~RD_FIRST;

      return main_decompress_input_check (ifile, buf, size, nread);
    }
#endif

  return main_file_read (ifile, buf, size, nread, "input read failed");
}

/* This function simply writes the stream output buffer, if there is any.  This is used
 * for both encode and decode commands.  (The VCDIFF tools use main_print_func()). */
static int
main_write_output (xd3_stream* stream, main_file *ofile)
{
  int ret;

  if (stream->avail_out > 0 && (ret = main_file_write (ofile, stream->next_out, stream->avail_out, "write failed")))
    {
      return ret;
    }

  return 0;
}

/* Open the main output file, sets a default file name, initiate recompression.  This
 * function is expected to fprint any error messages. */
static int
main_open_output (xd3_stream *stream, main_file *ofile)
{
  int ret;

  if (ofile->filename == NULL)
    {
      XSTDOUT_XF (ofile);

      if (option_verbose > 1) { XPR(NT "using standard output: %s\n", ofile->filename); }
    }
  else
    {
      /* Stat the file to check for overwrite. */
      if (option_force == 0 && main_file_exists (ofile))
	{
	  XPR(NT "to overwrite output file specify -f: %s\n", ofile->filename);
	  return EEXIST;
	}

      if ((ret = main_file_open (ofile, ofile->filename, XO_WRITE)))
	{
	  return ret;
	}

      if (option_verbose > 1) { XPR(NT "open output: %s\n", ofile->filename); }
    }

#if EXTERNAL_COMPRESSION
  /* Do output recompression. */
  if (ofile->compressor != NULL && option_recompress_outputs == 1)
    {
      if (! option_quiet)
	{
	  XPR(NT "%s %s | %s\n",
	     ofile->compressor->recomp_cmdname,
	     ofile->compressor->recomp_options,
	     ofile->filename);
	}

      if ((ret = main_recompress_output (ofile)))
	{
	  return ret;
	}
    }
#endif

  return 0;
}

/* This is called at different times for encoding and decoding.  The encoder calls it
 * immediately, the decoder delays until the application header is received.
 * Stream may be NULL, in which case xd3_set_source is not called. */
static int
main_set_source (xd3_stream *stream, int cmd, main_file *sfile, xd3_source *source)
{
  int ret = 0, i;
  uint8_t *tmp_buf = NULL;

  /* Open it, check for seekability, set required xd3_source fields. */
  if (allow_fake_source)
    {
      sfile->mode = XO_READ;
      sfile->realname = sfile->filename;
      sfile->nread = 0;
      source->size = UINT64_MAX;
    }
  else if ((ret = main_file_open (sfile, sfile->filename, XO_READ)) ||
	   (ret = main_file_stat (sfile, & source->size, 1)))
    {
      goto error;
    }

  source->name     = sfile->filename;
  source->ioh      = sfile;
  source->curblkno = (xoff_t) -1;
  source->curblk   = NULL;

#if EXTERNAL_COMPRESSION
  if (option_decompress_inputs)
    {
      /* If encoding, read the header to check for decompression. */
      if (IS_ENCODE (cmd))
	{
	  usize_t nread;
	  tmp_buf = main_malloc(XD3_ALLOCSIZE);

	  if ((ret = main_file_read (sfile, tmp_buf, XD3_ALLOCSIZE,
				     & nread, "source read failed")))
	    {
	      goto error;
	    }

	  /* Check known magic numbers. */
	  for (i = 0; i < SIZEOF_ARRAY (extcomp_types); i += 1)
	    {
	      const main_extcomp *decomp = & extcomp_types[i];

	      if ((nread > decomp->magic_size) &&
		  memcmp (tmp_buf, decomp->magic, decomp->magic_size) == 0)
		{
		  sfile->compressor = decomp;
		  break;
		}
	    }

	  if (sfile->compressor == NULL)
	    {
	      if (option_verbose > 2)
		{
		  XPR(NT "source block 0 read (not compressed)\n");
		}
	    }
	}

      /* In either the encoder or decoder, start decompression. */
      if (sfile->compressor)
	{
	  xoff_t osize = source->size;

	  if (osize > XD3_NODECOMPRESSSIZE)
	    {
	      XPR(NT "source file too large for external decompression: %s: %"Q"u\n",
		       sfile->filename, osize);
	      ret = XD3_INTERNAL;
	      goto error;
	    }

	  if ((ret = main_decompress_source (sfile, source)))
	    {
	      goto error;
	    }

	  if (! option_quiet)
	    {
	      char s1[32], s2[32];
	      XPR(NT "%s | %s %s => %s %.1f%% [ %s , %s ]\n",
		 sfile->filename,
		 sfile->compressor->decomp_cmdname,
		 sfile->compressor->decomp_options,
		 sfile->realname,
		 100.0 * source->size / osize,
		 main_format_bcnt (osize, s1),
		 main_format_bcnt (source->size, s2));
	    }
	}
    }
#endif

  /* At this point we know source->size.
   * Source buffer, blksize, LRU init. */
  if (source->size < option_srcwinsz)
    {
      /* Reduce sizes to actual source size, read whole file */
      option_srcwinsz = source->size;
      source->blksize = source->size;
      lru_size = 1;
    }
  else
    {
      /* Minimum size check */
      option_srcwinsz = max(option_srcwinsz, XD3_MINSRCWINSZ);

      if (!option_srcwinsz_set)
	{
	  /* If the flag was not set, scale srcwinsz up to 64MB. */
	  option_srcwinsz = min((xoff_t) XD3_DEFAULT_SRCWINSZ, source->size);
	}

      source->blksize = (option_srcwinsz / LRU_SIZE);
      lru_size = LRU_SIZE;
    }

  main_blklru_list_init (& lru_list);
  main_blklru_list_init (& lru_free);

  if ((lru = main_malloc (sizeof (main_blklru) * lru_size)) == NULL)
    {
      ret = ENOMEM;
      goto error;
    }

  for (i = 0; i < lru_size; i += 1)
    {
      lru[i].blkno = (xoff_t) -1;

      if ((lru[i].blk = main_malloc (source->blksize)) == NULL)
	{
	  ret = ENOMEM;
	  goto error;
	}

      main_blklru_list_push_back (& lru_free, & lru[i]);
    }

  if (option_verbose > 1)
    {
      XPR(NT "source window size: %u\n", option_srcwinsz);
      XPR(NT "source block size: %u\n", source->blksize);
      XPR(NT "source file: %s: %"Q"u bytes\n", sfile->realname, source->size);
    }

  if (stream && (ret = xd3_set_source (stream, source)))
    {
      XPR(NT XD3_LIB_ERRMSG (stream, ret));
      goto error;
    }

 error:
  if (tmp_buf != NULL)
    {
      main_free (tmp_buf);
    }

  return ret;
}

/******************************************************************************************
 Source routines
 ******************************************************************************************/

/* This is the callback for reading a block of source.  This function is blocking and it
 * implements a small LRU.
 *
 * Note that it is possible for main_input() to handle getblk requests in a non-blocking
 * manner.  If the callback is NULL then the caller of xd3_*_input() must handle the
 * XD3_GETSRCBLK return value and fill the source in the same way.  See xd3_getblk for
 * details.  To see an example of non-blocking getblk, see xdelta-test.h. */
static int
main_getblk_func (xd3_stream *stream,
		  xd3_source *source,
		  xoff_t      blkno)
{
  xoff_t      pos   = blkno * source->blksize;
  main_file   *sfile = (main_file*) source->ioh;
  main_blklru *blru  = NULL;
  usize_t      onblk = xd3_bytes_on_srcblk (source, blkno);
  usize_t      nread;
  int         ret;
  int         i;

  if (allow_fake_source)
    {
      source->curblkno = blkno;
      source->onblk    = onblk;
      source->curblk   = lru[0].blk;
      return 0;
    }

  if (do_not_lru)
    {
      /* Direct lookup assumes sequential scan w/o skipping blocks. */
      int idx = blkno % lru_size;
      if (lru[idx].blkno == blkno)
	{
	  source->curblkno = blkno;
	  source->onblk    = onblk;
	  source->curblk   = lru[idx].blk;
	  lru_hits += 1;
	  return 0;
	}

      if (lru[idx].blkno != -1LL &&
	  lru[idx].blkno != blkno - lru_size)
	{
	  return XD3_TOOFARBACK;
	}
    }
  else
    {
      /* Sequential search through LRU. */
      for (i = 0; i < lru_size; i += 1)
	{
	  if (lru[i].blkno == blkno)
	    {
	      main_blklru_list_remove (& lru[i]);
	      main_blklru_list_push_back (& lru_list, & lru[i]);

	      source->curblkno = blkno;
	      source->onblk    = onblk;
	      source->curblk   = lru[i].blk;
	      lru_hits += 1;
	      return 0;
	    }
	}
    }

  if (! main_blklru_list_empty (& lru_free))
    {
      blru = main_blklru_list_pop_front (& lru_free);
    }
  else if (! main_blklru_list_empty (& lru_list))
    {
      if (do_not_lru) {
	blru = & lru[blkno % lru_size];
	main_blklru_list_remove(blru);
      } else {
	blru = main_blklru_list_pop_front (& lru_list);
      }
      lru_misses += 1;
    }

  lru_filled += 1;

  if ((ret = main_file_seek (sfile, pos)))
    {
      return ret;
    }

  if ((ret = main_file_read (sfile, (uint8_t*) blru->blk, source->blksize,
			     & nread, "source read failed")))
    {
      return ret;
    }

  if (nread != onblk)
    {
      XPR(NT "source file size change: %s\n", sfile->filename);
      return XD3_INTERNAL;
    }

  main_blklru_list_push_back (& lru_list, blru);

  if (option_verbose > 2)
    {
      if (blru->blkno != -1LL)
	{
	  XPR(NT "source block %"Q"u ejects %"Q"u (lru_hits=%u, lru_misses=%u, lru_filled=%u)\n",
	      blkno, blru->blkno, lru_hits, lru_misses, lru_filled);
	}
      else
	{
	  XPR(NT "source block %"Q"u read (lru_hits=%u, lru_misses=%u, lru_filled=%u)\n",
					  blkno, lru_hits, lru_misses, lru_filled);
	}
    }

  blru->blkno      = blkno;
  source->curblk   = blru->blk;
  source->curblkno = blkno;
  source->onblk    = onblk;

  return 0;
}

/******************************************************************************************
 Main routines
 ******************************************************************************************/

/* This is a generic input function.  It calls the xd3_encode_input or xd3_decode_input
 * functions and makes calls to the various input handling routines above, which
 * coordinate external decompression.
 */
static int
main_input (xd3_cmd     cmd,
	    main_file   *ifile,
	    main_file   *ofile,
	    main_file   *sfile)
{
  int        ret;
  xd3_stream stream;
  usize_t    nread;
  int        stream_flags = 0;
  xd3_config config;
  xd3_source source;
  xoff_t     last_total_in = 0;
  xoff_t     last_total_out = 0;
  long       start_time;
  int        stdout_only = 0;

  int (*input_func) (xd3_stream*);
  int (*output_func) (xd3_stream*, main_file *);

  memset (& source, 0, sizeof (source));
  memset (& config, 0, sizeof (config));

  config.alloc = main_alloc;
  config.freef = main_free1;
  config.sec_data.ngroups = 1;
  config.sec_addr.ngroups = 1;
  config.sec_inst.ngroups = 1;

  /* TODO: eliminate static variables. */
  do_not_lru = 0;

  /* main_input setup. */
  switch ((int) cmd)
    {
#if VCDIFF_TOOLS
           if (1) { case CMD_PRINTHDR:   stream_flags = XD3_JUST_HDR; }
      else if (1) { case CMD_PRINTHDRS:  stream_flags = XD3_SKIP_WINDOW; }
      else        { case CMD_PRINTDELTA: stream_flags = XD3_SKIP_EMIT; }
      ifile->flags |= RD_NONEXTERNAL;
      input_func    = xd3_decode_input;
      output_func   = main_print_func;
      stream_flags |= XD3_ADLER32_NOVER;
      stdout_only   = 1;
      break;
#endif
#if XD3_ENCODER
    case CMD_ENCODE:
      do_not_lru  = 1;
      input_func  = xd3_encode_input;
      output_func = main_write_output;

      if (option_use_checksum) { stream_flags |= XD3_ADLER32; }
      if (option_use_secondary)
	{
	  /* The default secondary compressor is DJW, if it's compiled, being used, etc. */
	  if (option_secondary == NULL)
	    {
	      if (SECONDARY_DJW) { stream_flags |= XD3_SEC_DJW; }
	    }
	  else
	    {
	      if (strcmp (option_secondary, "fgk") == 0 && SECONDARY_FGK)
		{
		  stream_flags |= XD3_SEC_FGK;
		}
	      else if (strcmp (option_secondary, "djw") == 0 && SECONDARY_DJW)
		{
		  stream_flags |= XD3_SEC_DJW;
		}
	      else
		{
		  XPR(NT "unrecognized secondary compressor type: %s\n", option_secondary);
		  return EXIT_FAILURE;
		}
	    }
	}
      if (option_no_compress)      { stream_flags |= XD3_NOCOMPRESS; }
      if (option_use_altcodetable) { stream_flags |= XD3_ALT_CODE_TABLE; }
      if (option_smatch_config)
	{
	  char *s = option_smatch_config, *e;
	  int values[XD3_SOFTCFG_VARCNT];
	  int got;

	  config.smatch_cfg = XD3_SMATCH_SOFT;

	  for (got = 0; got < XD3_SOFTCFG_VARCNT; got += 1, s = e + 1)
	    {
	      values[got] = strtol (s, &e, 10);

	      if ((values[got] < 0) ||
		  (e == s) ||
		  (got < XD3_SOFTCFG_VARCNT-1 && *e == 0) ||
		  (got == XD3_SOFTCFG_VARCNT-1 && *e != 0))
		{
		  XPR(NT "invalid string match specifier (-C) %d: %s\n",
		      got, s);
		  return EXIT_FAILURE;
		}
	    }

	  config.smatcher_soft.large_look    = values[0];
	  config.smatcher_soft.large_step    = values[1];
	  config.smatcher_soft.small_look    = values[2];
	  config.smatcher_soft.small_chain   = values[3];
	  config.smatcher_soft.small_lchain  = values[4];
	  config.smatcher_soft.ssmatch       = values[5];
	  config.smatcher_soft.try_lazy      = values[6];
	  config.smatcher_soft.max_lazy      = values[7];
	  config.smatcher_soft.long_enough   = values[8];
	  config.smatcher_soft.promote       = values[9];
	}
      else if (option_level <= 5) { config.smatch_cfg = XD3_SMATCH_FAST; }
      else                        { config.smatch_cfg = XD3_SMATCH_SLOW; }
      break;
#endif
    case CMD_DECODE:
      if (option_use_checksum == 0) { stream_flags |= XD3_ADLER32_NOVER; }
      stream_flags  = 0;
      ifile->flags |= RD_NONEXTERNAL;
      input_func    = xd3_decode_input;
      output_func   = main_write_output;
      break;
    default:
      XPR(NT "internal error\n");
      return EXIT_FAILURE;
    }

  start_time = get_millisecs_now ();

  if (option_verbose > 1)
    {
      XPR(NT "input buffer size: %u\n", option_winsize);
    }

  if ((main_bdata = main_malloc (option_winsize)) == NULL)
    {
      return EXIT_FAILURE;
    }

  if (IS_ENCODE (cmd))
    {
      /* When encoding, open the source file, possibly decompress it.  The decoder delays
       * this step until XD3_GOTHEADER. */
      if (sfile->filename != NULL && (ret = main_set_source (NULL, cmd, sfile, & source)))
	{
	  return EXIT_FAILURE;
	}
    }

  option_winsize = max(option_winsize, XD3_ALLOCSIZE);

  config.winsize = option_winsize;
  config.srcwin_maxsz = option_srcwinsz;
  config.getblk = main_getblk_func;
  config.flags = stream_flags;

  if ((ret = xd3_config_stream (& stream, & config)))
    {
      XPR(NT XD3_LIB_ERRMSG (& stream, ret));
      return EXIT_FAILURE;
    }

  if (IS_ENCODE (cmd) && sfile->filename != NULL &&
      (ret = xd3_set_source (& stream, & source)))
    {
      XPR(NT XD3_LIB_ERRMSG (& stream, ret));
      return EXIT_FAILURE;
    }

  /* This times each window. */
  get_millisecs_since ();

  /* Main input loop. */
  do
    {
      xoff_t input_offset;
      xoff_t input_remain;
      usize_t try_read;

      input_offset = ifile->nread;

      input_remain = XOFF_T_MAX - input_offset;

      try_read = (usize_t) min ((xoff_t) config.winsize, input_remain);

      if ((ret = main_read_primary_input (ifile, main_bdata, try_read, & nread)))
	{
	  return EXIT_FAILURE;
	}

      /* If we've reached EOF tell the stream to flush. */
      if (nread < try_read)
	{
	  stream_flags |= XD3_FLUSH;
	  xd3_set_flags (& stream, stream_flags);
	}

#if XD3_ENCODER
      /* After the first main_read_primary_input completes, we know all the information
       * needed to encode the application header. */
      if (cmd == CMD_ENCODE && (ret = main_set_appheader (& stream, ifile, sfile)))
	{
	  return EXIT_FAILURE;
	}
#endif
      xd3_avail_input (& stream, main_bdata, nread);

      /* If we read zero bytes after encoding at least one window... */
      if (nread == 0 && stream.current_window > 0) {
	break;
      }

    again:
      ret = input_func (& stream);

      switch (ret)
	{
	case XD3_INPUT:
	  continue;

	case XD3_GOTHEADER:
	  {
	    XD3_ASSERT (stream.current_window == 0);

	    /* Need to process the appheader as soon as possible.  It may contain a
	     * suggested default filename/decompression routine for the ofile, and it may
	     * contain default/decompression routine for the sources. */
	    if (cmd == CMD_DECODE)
	      {
		int have_src = sfile->filename != NULL;
		int need_src = xd3_decoder_needs_source (& stream);
		int recv_src;

		/* May need to set the sfile->filename if none was given. */
		main_get_appheader (& stream, ifile, ofile, sfile);

		recv_src = sfile->filename != NULL;

		/* Check if the user expected a source to be required although it was not. */
		if (have_src && ! need_src && ! option_quiet)
		  {
		    XPR(NT "warning: output window %"Q"u does not copy source\n", stream.current_window);
		  }

		/* Check if we have no source name and need one. */
		/* TODO: this doesn't fire due to cpyblocks_ calculation check */
		if (need_src && ! recv_src)
		  {
		    XPR(NT "input requires a source file, use -s\n");
		    return EXIT_FAILURE;
		  }

		/* Now open the source file. */
		if (need_src && (ret = main_set_source (& stream, cmd, sfile, & source)))
		  {
		    return EXIT_FAILURE;
		  }
	      }
	    else if (cmd == CMD_PRINTHDR ||
		     cmd == CMD_PRINTHDRS ||
		     cmd == CMD_PRINTDELTA)
	      {
		if (xd3_decoder_needs_source (& stream) && sfile->filename == NULL)
		  {
		    allow_fake_source = 1; 
		    sfile->filename = "<placeholder>";
		    main_set_source (& stream, cmd, sfile, & source);
		  }
	      }
	  }
	/* FALLTHROUGH */
	case XD3_WINSTART:
	  {
	    /* e.g., set or unset XD3_SKIP_WINDOW. */
	    /* xd3_set_flags (& stream, stream_flags); */
	    goto again;
	  }

	case XD3_OUTPUT:
	  {
	    if (option_no_output == 0)
	      {
		/* Defer opening the output file until the stream produces its first
		 * output for both encoder and decoder, this way we delay long enough for
		 * the decoder to receive the application header.  (Or longer if there are
		 * skipped windows, but I can't think of any reason not to delay open.) */
		
		if (! main_file_isopen (ofile) && (ret = main_open_output (& stream, ofile)) != 0)
		  {
		    return EXIT_FAILURE;
		  }
		if ((ret = output_func (& stream, ofile)) && (ret != PRINTHDR_SPECIAL))
		  {
		    return EXIT_FAILURE;
		  }
		if (ret == PRINTHDR_SPECIAL)
		  {
		    xd3_abort_stream (& stream);
		    ret = EXIT_SUCCESS;
		    goto done;
		  }
		ret = 0;
	      }

	    xd3_consume_output (& stream);
	    goto again;
	  }

	case XD3_WINFINISH:
	  {
	    if (IS_ENCODE (cmd) || cmd == CMD_DECODE)
	      {
		int used_source = xd3_encoder_used_source (& stream);

		if (! option_quiet && IS_ENCODE (cmd) && main_file_isopen (sfile) && ! used_source)
		  {
		    XPR(NT "warning: input position %"Q"u no source copies\n",
			stream.current_window * option_winsize);
		  }

		if (option_verbose)
		  {
		    char rrateavg[32], wrateavg[32], tm[32];
		    char rdb[32], wdb[32];
		    char trdb[32], twdb[32];
		    long millis = get_millisecs_since ();
		    usize_t this_read = (usize_t)(stream.total_in - last_total_in);
		    usize_t this_write = (usize_t)(stream.total_out - last_total_out);
		    last_total_in = stream.total_in;
		    last_total_out = stream.total_out;

		    if (option_verbose > 1)
		      {
			XPR(NT "%"Q"u: in %s (%s): out %s (%s): total in %s: out %s: %s\n",
			    stream.current_window,
			    main_format_bcnt (this_read, rdb),
			    main_format_rate (this_read, millis, rrateavg),
			    main_format_bcnt (this_write, wdb),
			    main_format_rate (this_write, millis, wrateavg),
			    main_format_bcnt (stream.total_in, trdb),
			    main_format_bcnt (stream.total_out, twdb),
			    main_format_millis (millis, tm));
		      }
		    else
		      {
			XPR(NT "%"Q"u: in %s: out %s: total in %s: out %s: %s\n",
 			    stream.current_window,
			    main_format_bcnt (this_read, rdb),
			    main_format_bcnt (this_write, wdb),
			    main_format_bcnt (stream.total_in, trdb),
			    main_format_bcnt (stream.total_out, twdb),
			    main_format_millis (millis, tm));
		      }
		  }
	      }
	    goto again;
	  }

	default:
	  /* input_func() error */
	  XPR(NT XD3_LIB_ERRMSG (& stream, ret));
	  return EXIT_FAILURE;
	}
    }
  while (nread == config.winsize);
done:
  /* Close the inputs. (ifile must be open, sfile may be open) */
  main_file_close (ifile);
  main_file_close (sfile);

  /* If output file is not open yet because of delayed-open, it means we never encountered
   * a window in the delta, but it could have had a VCDIFF header?  TODO: solve this
   * elsewhere.  For now, it prints "nothing to output" below, but the check doesn't
   * happen in case of option_no_output.  */
  if (! option_no_output)
    {
      if (!stdout_only && ! main_file_isopen (ofile))
	{
	  XPR(NT "nothing to output: %s\n", ifile->filename);
	  return EXIT_FAILURE;
	}

      /* Have to close the output before calling main_external_compression_finish, or else it hangs. */
      if (main_file_close (ofile) != 0)
	{
	  return EXIT_FAILURE;
	}
    }

#if EXTERNAL_COMPRESSION
  if ((ret = main_external_compression_finish ()))
    {
      XPR(NT "external compression commands failed\n");
      return EXIT_FAILURE;
    }
#endif

  if ((ret = xd3_close_stream (& stream)))
    {
      XPR(NT XD3_LIB_ERRMSG (& stream, ret));
      return EXIT_FAILURE;
    }

  if (option_verbose > 1)
    {
      XPR(NT "scanner configuration: %s\n", stream.smatcher.name);
      XPR(NT "target hash table size: %u\n", stream.small_hash.size);
      if (sfile->filename != NULL)
	{
	  XPR(NT "source hash table size: %u\n", stream.large_hash.size);
	}
    }

  if (option_verbose > 2)
    {
      XPR(NT "source copies: %"Q"u (%"Q"u bytes)\n", stream.n_scpy, stream.l_scpy);
      XPR(NT "target copies: %"Q"u (%"Q"u bytes)\n", stream.n_tcpy, stream.l_tcpy);
      XPR(NT "adds: %"Q"u (%"Q"u bytes)\n", stream.n_add, stream.l_add);
      XPR(NT "runs: %"Q"u (%"Q"u bytes)\n", stream.n_run, stream.l_run);
    }

  xd3_free_stream (& stream);

  if (option_verbose)
    {
      char tm[32];
      long end_time = get_millisecs_now ();
      XPR(NT "command finished in %s\n", main_format_millis (end_time - start_time, tm));
      XPR(NT "input bytes:  %"Q"u\n",  ifile->nread);
      XPR(NT "output bytes: %"Q"u\n", ofile->nwrite);
    }

  return EXIT_SUCCESS;
}

/* free memory before exit, reset single-use variables. */
static void
main_cleanup (void)
{
  int i;
  
  if (appheader_used != NULL &&
      appheader_used != option_appheader)
    {
      main_free (appheader_used);
      appheader_used = NULL;
    }

  main_free (main_bdata);
  main_bdata = NULL;

#if EXTERNAL_COMPRESSION
  main_free (ext_tmpfile);
  ext_tmpfile = NULL;
#endif

  for (i = 0; lru && i < lru_size; i += 1)
    {
      main_free (lru[i].blk);
    }

  main_free (lru);
  lru = NULL;

  lru_hits = 0;
  lru_misses = 0;
  lru_filled = 0;

  XD3_ASSERT (main_mallocs == 0);
}

int
#if PYTHON_MODULE
xd3_main_cmdline (int argc, char **argv)
#else
main (int argc, char **argv)
#endif
{
  xd3_cmd cmd;
  main_file ifile;
  main_file ofile;
  main_file sfile;
  static char *flags = "0123456789cdefhnqvDJNORTVs:B:C:E:F:L:O:P:M:W:A::S::";
  int my_optind;
  char *my_optarg;
  char *my_optstr;
  char *sfilename;
  int orig_argc = argc;
  char **orig_argv = argv;
  int ret;

  main_file_init (& ifile);
  main_file_init (& ofile);
  main_file_init (& sfile);

  reset_defaults();

 go:  /* Go. */
  cmd = CMD_NONE;
  sfilename = NULL;
  my_optind = 1;
  argv = orig_argv;
  argc = orig_argc;
  program_name = argv[0];
  extcomp_types[0].recomp_cmdname = program_name;
  extcomp_types[0].decomp_cmdname = program_name;
 takearg:
  my_optarg = NULL;
  my_optstr = argv[my_optind];
  /* This doesn't use getopt() because it makes trouble for -P & python which reenter
   * main() and thus care about freeing all memory.  I never had much trust for getopt
   * anyway, it's too opaque.  This implements a fairly standard non-long-option getopt
   * with support for named operations (e.g., "xdelta3 [encode|decode|printhdr...] < in >
   * out"). */
  if (my_optstr)
    {
      if (*my_optstr == '-')    { my_optstr += 1; }
      else if (cmd == CMD_NONE) { goto nonflag; }
      else                      { my_optstr = NULL; }
    }
  while (my_optstr)
    {
      char *s;
      my_optarg = NULL;
      if ((ret = *my_optstr++) == 0) { my_optind += 1; goto takearg; }

      /* Option handling: first check for one ':' following the option in flags, then
       * check for two.  The syntax allows:
       *
       * 1. -Afoo                   defines optarg="foo"
       * 2. -A foo                  defines optarg="foo"
       * 3. -A ""                   defines optarg="" (allows optional empty-string)
       * 4. -A [EOA or -moreargs]   error (mandatory case)
       * 5. -A [EOA -moreargs]      defines optarg=NULL (optional case)
       * 6. -A=foo                  defines optarg="foo"
       * 7. -A=                     defines optarg="" (mandatory case)
       * 8. -A=                     defines optarg=NULL (optional case)
       *
       * See tests in test_command_line_arguments().
       */
      s = strchr (flags, ret);
      if (s && s[1] && s[1] == ':')
	{
	  int eqcase = 0;
	  int option = s[2] && s[2] == ':';

	  /* Case 1, set optarg to the remaining characters. */
	  my_optarg = my_optstr;
	  my_optstr = "";

	  /* Case 2-5 */
	  if (*my_optarg == 0)
	    {
	      /* Condition 4-5 */
	      int have_arg = my_optind < (argc - 1) && *argv[my_optind+1] != '-';

	      if (! have_arg)
		{
		  if (! option)
		  {
		    /* Case 4 */
		    XPR(NT "-%c: requires an argument\n", ret);
		    ret = EXIT_FAILURE;
		    goto cleanup;
		  }
		  /* Case 5. */
		  my_optarg = NULL;
		}
	      else
		{
		  /* Case 2-3. */
		  my_optarg = argv[++my_optind];
		}
	    }
	  /* Case 6-8. */
	  else if (*my_optarg == '=')
	    {
	      /* Remove the = in all cases. */
	      my_optarg += 1;
	      eqcase = 1;

	      if (option && *my_optarg == 0)
		{
		  /* Case 8. */
		  my_optarg = NULL;
		}
	    }
	}

      switch (ret)
	{
	/* case: if no '-' was found, maybe check for a command name. */
	nonflag:
	       if (strcmp (my_optstr, "decode") == 0) { cmd = CMD_DECODE; }
	  else if (strcmp (my_optstr, "encode") == 0)
	    {
#if XD3_ENCODER
	      cmd = CMD_ENCODE;
#else
	      XPR(NT "encoder support not compiled\n");
	      return EXIT_FAILURE;
#endif
	    }
	  else if (strcmp (my_optstr, "config") == 0) { cmd = CMD_CONFIG; }
#if REGRESSION_TEST
	  else if (strcmp (my_optstr, "test") == 0) { cmd = CMD_TEST; }
#endif
#if VCDIFF_TOOLS
	  else if (strcmp (my_optstr, "printhdr") == 0) { cmd = CMD_PRINTHDR; }
	  else if (strcmp (my_optstr, "printhdrs") == 0) { cmd = CMD_PRINTHDRS; }
	  else if (strcmp (my_optstr, "printdelta") == 0) { cmd = CMD_PRINTDELTA; }
#endif

	  /* If no option was found and still no command, let the default command be
	   * encode.  The remaining args are treated as filenames. */
	  if (cmd == CMD_NONE)
	    {
	      cmd = CMD_DEFAULT;
	      my_optstr = NULL;
	      break;
	    }
	  else
	    {
	      /* But if we find a command name, continue the getopt loop. */
	      my_optind += 1;
	      goto takearg;
	    }

	  /* gzip-like options */
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
	  option_level = ret - '0';
	  break;
	case 'f': option_force = 1; break;
	case 'v': option_verbose += 1; option_quiet = 0; break;
	case 'q': option_quiet = 1; option_verbose = 0; break;
	case 'c': option_stdout = 1; break;
	case 'd':
	  if (cmd == CMD_NONE) { cmd = CMD_DECODE; }
	  else { ret = main_help (); goto exit; }
	  break;
	case 'e':
#if XD3_ENCODER
	  if (cmd == CMD_NONE) { cmd = CMD_ENCODE; }
	  else { ret = main_help (); goto exit; }
	  break;
#else
	  XPR(NT "encoder support not compiled\n");
	  return EXIT_FAILURE;
#endif	  
	case 'P':
	  /* only set profile count once, since... */
	  if (option_profile_cnt == 0)
	    {
	      if ((ret = main_atou(my_optarg, (usize_t*) & option_profile_cnt, 0, 0, 'P')))
		{
		  goto exit;
		}

	      if (option_profile_cnt <= 0)
		{
		  ret = EXIT_SUCCESS;
		  goto exit;
		}
	    }
	  break;

	case 'n': option_use_checksum = 0; break;
	case 'N': option_no_compress = 1; break;
	case 'T': option_use_altcodetable = 1; break;
	case 'C': option_smatch_config = my_optarg; break;
	case 'J': option_no_output = 1; break;
	case 'S': if (my_optarg == NULL) { option_use_secondary = 0; }
	          else { option_use_secondary = 1; option_secondary = my_optarg; } break;
	case 'A': if (my_optarg == NULL) { option_use_appheader = 0; }
	          else { option_appheader = (uint8_t*) my_optarg; } break;
	case 'B':
	  option_srcwinsz_set = 1;
	  if ((ret = main_atou (my_optarg, & option_srcwinsz, XD3_MINSRCWINSZ,
				0, 'B')))
	    {
	      goto exit;
	    }
	  break;
	case 'W':
	  if ((ret = main_atou (my_optarg, & option_winsize, XD3_ALLOCSIZE,
				XD3_HARDMAXWINSIZE, 'W')))
	  {
	    goto exit;
	  }
	  break;
	case 'D':
#if EXTERNAL_COMPRESSION == 0
	  if (! option_quiet)
	    {
	      XPR(NT "warning: -D option ignored, "
		       "external compression support was not compiled\n");
	    }
#else
	  option_decompress_inputs  = 0;
#endif
	  break;
	case 'R':
#if EXTERNAL_COMPRESSION == 0
	  if (! option_quiet)
	    {
	      XPR(NT "warning: -R option ignored, "
		       "external compression support was not compiled\n");
	    }
#else
	  option_recompress_outputs = 0;
#endif
	  break;
	case 's':
	  if (sfilename != NULL)
	    {
	      XPR(NT "specify only one source file\n");
	      goto cleanup;
	    }

	  sfilename = my_optarg;
	  break;

	case 'V':
	  ret = main_version (); goto exit;
	default:
	  ret = main_help (); goto exit;
	}
    }

  option_source_filename = sfilename;

  /* In case there were no arguments, set the default command. */
  if (cmd == CMD_NONE) { cmd = CMD_DEFAULT; }

  argc -= my_optind;
  argv += my_optind;

  /* There may be up to two more arguments. */
  if (argc > 2)
    {
      XPR(NT "too many filenames: %s ...\n", argv[2]);
      ret = EXIT_FAILURE;
      goto cleanup;
    }

  if (option_verbose > 1)
    {
      int l = 1;
      int i;
      char buf[1024];
      for (i = 0; i < orig_argc; i += 1)
	{
	  l += strlen (orig_argv[i]) + 1;
	}
      buf[0] = 0;
      for (i = 0; i < orig_argc; i += 1)
	{
	  strcat (buf, orig_argv[i]);
	  strcat (buf, " ");
	}
      XPR(NT "command line: %s\n", buf);
    }      

  ifile.flags    = RD_FIRST;
  sfile.flags    = RD_FIRST;
  sfile.filename = option_source_filename;

  /* The infile takes the next argument, if there is one.  But if not, infile is set to
   * stdin. */
  if (argc > 0)
    {
      ifile.filename = argv[0];

      if ((ret = main_file_open (& ifile, ifile.filename, XO_READ)))
	{
	  goto cleanup;	  
	}
    }
  else
    {
      XSTDIN_XF (& ifile);
    }

  /* The ofile takes the following argument, if there is one.  But if not, it is left NULL
   * until the application header is processed.  It will be set in main_open_output. */
  if (argc > 1)
    {
      /* Check for conflicting arguments. */
      if (option_stdout && ! option_quiet)
	{
	  XPR(NT "warning: -c option overrides output filename: %s\n", argv[1]);
	}

      if (! option_stdout) { ofile.filename = argv[1]; }
    }

  switch (cmd)
    {
    case CMD_PRINTHDR:
    case CMD_PRINTHDRS:
    case CMD_PRINTDELTA:
#if XD3_ENCODER
    case CMD_ENCODE:
#endif
    case CMD_DECODE:
      ret = main_input (cmd, & ifile, & ofile, & sfile);
      break;

#if REGRESSION_TEST
    case CMD_TEST:
      ret = xd3_selftest ();
      break;
#endif

    case CMD_CONFIG:
      ret = main_config ();
      break;

    default:
      ret = main_help ();
      break;
    }

#if EXTERNAL_COMPRESSION
  if (ext_tmpfile != NULL)
    {
      unlink (ext_tmpfile);
    }
#endif

  if (0)
    {
    cleanup:
      ret = EXIT_FAILURE;
    exit:
      (void)0;
    }

  main_file_cleanup (& ifile);
  main_file_cleanup (& ofile);
  main_file_cleanup (& sfile);

  main_cleanup ();

  if (--option_profile_cnt > 0 && ret == EXIT_SUCCESS) { goto go; }

  fflush (stdout);
  fflush (stderr);
  return ret;
}

static int
main_help (void)
{
  /* TODO: update www/xdelta3-cmdline.html */ 
  main_version ();
  P(RINT "usage: xdelta3 [command/options] [input [output]]\n");
  P(RINT "special command names:\n");
  P(RINT "    config      prints xdelta3 configuration\n");
  P(RINT "    decode      decompress the input\n");
  P(RINT "    encode      compress the input%s\n", XD3_ENCODER ? "" : " [Not compiled]");
#if REGRESSION_TEST
  P(RINT "    test        run the builtin tests\n");
#endif
#if VCDIFF_TOOLS
  P(RINT "special commands for VCDIFF inputs:\n");
  P(RINT "    printdelta  print information about the entire delta\n");
  P(RINT "    printhdr    print information about the first window\n");
  P(RINT "    printhdrs   print information about all windows\n");
#endif
  P(RINT "standard options:\n");
  P(RINT "   -0 .. -9     compression level\n");
  P(RINT "   -c           use stdout\n");
  P(RINT "   -d           decompress\n");
  P(RINT "   -e           compress%s\n", XD3_ENCODER ? "" : " [Not compiled]");
  P(RINT "   -f           force overwrite\n");
  P(RINT "   -h           show help\n");
  P(RINT "   -q           be quiet\n");
  P(RINT "   -v           be verbose (max 2)\n");
  P(RINT "   -V           show version\n");

  P(RINT "memory options:\n");
  P(RINT "   -B bytes     source window size\n");
  P(RINT "   -W bytes     input window size\n");

  P(RINT "compression options:\n");
  P(RINT "   -s source    source file to copy from (if any)\n");
  P(RINT "   -S [djw|fgk] enable/disable secondary compression\n");
  P(RINT "   -N           disable small string-matching compression\n");
  P(RINT "   -D           disable external decompression (encode/decode)\n");
  P(RINT "   -R           disable external recompression (decode)\n");

#if XD3_DEBUG > 0
  P(RINT "developer options:\n");
  P(RINT "   -A [apphead] disable/provide application header\n");
  P(RINT "   -C           soft config (see xdelta3-cfgs.h)\n");
  P(RINT "   -J           disable output (check/compute only)\n");
  P(RINT "   -P           repeat count (for profiling)\n");
  P(RINT "   -T           use alternate code table\n");
  P(RINT "   -n           disable checksum (encode/decode)\n");
#endif
  return EXIT_FAILURE;
}