1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
|
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmCPackTGZGenerator.h"
#include "cmake.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmSystemTools.h"
#include "cmMakefile.h"
#include "cmGeneratedFileStream.h"
#include <cmsys/SystemTools.hxx>
#include <cmzlib/zlib.h>
#include <memory> // auto_ptr
#include <fcntl.h> // O_WRONLY, O_CREAT
//----------------------------------------------------------------------
cmCPackTGZGenerator::cmCPackTGZGenerator()
{
}
//----------------------------------------------------------------------
cmCPackTGZGenerator::~cmCPackTGZGenerator()
{
}
//----------------------------------------------------------------------
int cmCPackTGZGenerator::ProcessGenerator()
{
return this->Superclass::ProcessGenerator();
}
//----------------------------------------------------------------------
int cmCPackTGZGenerator::Initialize(const char* name)
{
return this->Superclass::Initialize(name);
}
//----------------------------------------------------------------------
// The following code is modified version of bsdtar
/*-
* Copyright (c) 2003-2004 Tim Kientzle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if defined(HAVE_IO_H)
# include <io.h>
#endif
#if defined(HAVE_UNISTD_H)
# include <unistd.h> // for geteuid
#endif
#if defined(HAVE_INTTYPES_H)
# include <inttypes.h> // for uintptr_t
#endif
#include <sys/types.h>
#include <errno.h> // for ENOMEM
#include <sys/stat.h> // for struct stat
#include <time.h> // for time
#if defined(WIN32) && !defined(__CYGWIN__)
# ifndef S_ISREG
# define S_ISREG(x) ((x) & _S_IFREG)
# endif
# ifndef S_ISLNK
# define S_ISLNK(x) (0)
# endif
#endif
//--- archive_plarform.h
//
/* Set up defaults for internal error codes. */
#ifndef ARCHIVE_ERRNO_FILE_FORMAT
#if HAVE_EFTYPE
#define ARCHIVE_ERRNO_FILE_FORMAT EFTYPE
#else
#if HAVE_EILSEQ
#define ARCHIVE_ERRNO_FILE_FORMAT EILSEQ
#else
#define ARCHIVE_ERRNO_FILE_FORMAT EINVAL
#endif
#endif
#endif
#ifndef ARCHIVE_ERRNO_PROGRAMMER
#define ARCHIVE_ERRNO_PROGRAMMER EINVAL
#endif
#ifndef ARCHIVE_ERRNO_MISC
#define ARCHIVE_ERRNO_MISC (-1)
#endif
/* Select the best way to set/get hi-res timestamps. */
#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
/* FreeBSD uses "timespec" members. */
#define ARCHIVE_STAT_ATIME_NANOS(st) (st)->st_atimespec.tv_nsec
#define ARCHIVE_STAT_CTIME_NANOS(st) (st)->st_ctimespec.tv_nsec
#define ARCHIVE_STAT_MTIME_NANOS(st) (st)->st_mtimespec.tv_nsec
#define ARCHIVE_STAT_SET_ATIME_NANOS(st, n) (st)->st_atimespec.tv_nsec = (n)
#define ARCHIVE_STAT_SET_CTIME_NANOS(st, n) (st)->st_ctimespec.tv_nsec = (n)
#define ARCHIVE_STAT_SET_MTIME_NANOS(st, n) (st)->st_mtimespec.tv_nsec = (n)
#else
#if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
/* Linux uses "tim" members. */
#define ARCHIVE_STAT_ATIME_NANOS(pstat) (pstat)->st_atim.tv_nsec
#define ARCHIVE_STAT_CTIME_NANOS(pstat) (pstat)->st_ctim.tv_nsec
#define ARCHIVE_STAT_MTIME_NANOS(pstat) (pstat)->st_mtim.tv_nsec
#define ARCHIVE_STAT_SET_ATIME_NANOS(st, n) (st)->st_atim.tv_nsec = (n)
#define ARCHIVE_STAT_SET_CTIME_NANOS(st, n) (st)->st_ctim.tv_nsec = (n)
#define ARCHIVE_STAT_SET_MTIME_NANOS(st, n) (st)->st_mtim.tv_nsec = (n)
#else
/* If we can't find a better way, just use stubs. */
#define ARCHIVE_STAT_ATIME_NANOS(pstat) 0
#define ARCHIVE_STAT_CTIME_NANOS(pstat) 0
#define ARCHIVE_STAT_MTIME_NANOS(pstat) 0
#define ARCHIVE_STAT_SET_ATIME_NANOS(st, n)
#define ARCHIVE_STAT_SET_CTIME_NANOS(st, n)
#define ARCHIVE_STAT_SET_MTIME_NANOS(st, n)
#endif
#endif
//--- archive.h
#define ARCHIVE_BYTES_PER_RECORD 512
#define ARCHIVE_DEFAULT_BYTES_PER_BLOCK 10240
/* Declare our basic types. */
struct archive;
struct archive_entry;
/*
* Error codes: Use archive_errno() and archive_error_string()
* to retrieve details. Unless specified otherwise, all functions
* that return 'int' use these codes.
*/
#define ARCHIVE_EOF 1 /* Found end of archive. */
#define ARCHIVE_OK 0 /* Operation was successful. */
#define ARCHIVE_RETRY (-10) /* Retry might succeed. */
#define ARCHIVE_WARN (-20) /* Partial sucess. */
#define ARCHIVE_FATAL (-30) /* No more operations are possible. */
/*
* As far as possible, archive_errno returns standard platform errno codes.
* Of course, the details vary by platform, so the actual definitions
* here are stored in "archive_platform.h". The symbols are listed here
* for reference; as a rule, clients should not need to know the exact
* platform-dependent error code.
*/
/* Unrecognized or invalid file format. */
/* #define ARCHIVE_ERRNO_FILE_FORMAT */
/* Illegal usage of the library. */
/* #define ARCHIVE_ERRNO_PROGRAMMER_ERROR */
/* Unknown or unclassified error. */
/* #define ARCHIVE_ERRNO_MISC */
/*
* Callbacks are invoked to automatically read/write/open/close the archive.
* You can provide your own for complex tasks (like breaking archives
* across multiple tapes) or use standard ones built into the library.
*/
/* Returns pointer and size of next block of data from archive. */
typedef ssize_t archive_read_callback(struct archive *, void *_client_data,
const void **_buffer);
/* Returns size actually written, zero on EOF, -1 on error. */
typedef ssize_t archive_write_callback(struct archive *, void *_client_data,
void *_buffer, size_t _length);
typedef int archive_open_callback(struct archive *, void *_client_data);
typedef int archive_close_callback(struct archive *, void *_client_data);
/*
* Codes for archive_compression.
*/
#define ARCHIVE_COMPRESSION_NONE 0
#define ARCHIVE_COMPRESSION_GZIP 1
#define ARCHIVE_COMPRESSION_BZIP2 2
#define ARCHIVE_COMPRESSION_COMPRESS 3
/*
* Codes returned by archive_format.
*
* Top 16 bits identifies the format family (e.g., "tar"); lower
* 16 bits indicate the variant. This is updated by read_next_header.
* Note that the lower 16 bits will often vary from entry to entry.
*/
#define ARCHIVE_FORMAT_BASE_MASK 0xff0000U
#define ARCHIVE_FORMAT_CPIO 0x10000
#define ARCHIVE_FORMAT_CPIO_POSIX (ARCHIVE_FORMAT_CPIO | 1)
#define ARCHIVE_FORMAT_SHAR 0x20000
#define ARCHIVE_FORMAT_SHAR_BASE (ARCHIVE_FORMAT_SHAR | 1)
#define ARCHIVE_FORMAT_SHAR_DUMP (ARCHIVE_FORMAT_SHAR | 2)
#define ARCHIVE_FORMAT_TAR 0x30000
#define ARCHIVE_FORMAT_TAR_USTAR (ARCHIVE_FORMAT_TAR | 1)
#define ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE (ARCHIVE_FORMAT_TAR | 2)
#define ARCHIVE_FORMAT_TAR_PAX_RESTRICTED (ARCHIVE_FORMAT_TAR | 3)
#define ARCHIVE_FORMAT_TAR_GNUTAR (ARCHIVE_FORMAT_TAR | 4)
#define ARCHIVE_FORMAT_ISO9660 0x40000
#define ARCHIVE_FORMAT_ISO9660_ROCKRIDGE (ARCHIVE_FORMAT_ISO9660 | 1)
#define ARCHIVE_FORMAT_ZIP 0x50000
/*-
* Basic outline for reading an archive:
* 1) Ask archive_read_new for an archive reader object.
* 2) Update any global properties as appropriate.
* In particular, you'll certainly want to call appropriate
* archive_read_support_XXX functions.
* 3) Call archive_read_open_XXX to open the archive
* 4) Repeatedly call archive_read_next_header to get information about
* successive archive entries. Call archive_read_data to extract
* data for entries of interest.
* 5) Call archive_read_finish to end processing.
*/
struct archive *archive_read_new(void);
/*
* The archive_read_support_XXX calls enable auto-detect for this
* archive handle. They also link in the necessary support code.
* For example, if you don't want bzlib linked in, don't invoke
* support_compression_bzip2(). The "all" functions provide the
* obvious shorthand.
*/
int archive_read_support_compression_all(struct archive *);
int archive_read_support_compression_bzip2(struct archive *);
int archive_read_support_compression_compress(struct archive *);
int archive_read_support_compression_gzip(struct archive *);
int archive_read_support_compression_none(struct archive *);
int archive_read_support_format_all(struct archive *);
int archive_read_support_format_cpio(struct archive *);
int archive_read_support_format_gnutar(struct archive *);
int archive_read_support_format_iso9660(struct archive *);
int archive_read_support_format_tar(struct archive *);
int archive_read_support_format_tp(struct archive *a);
int archive_read_support_format_zip(struct archive *);
/* Open the archive using callbacks for archive I/O. */
int archive_read_open(struct archive *, void *_client_data,
archive_open_callback *, archive_read_callback *,
archive_close_callback *);
/*
* The archive_read_open_file function is a convenience function built
* on archive_read_open that uses a canned callback suitable for
* common situations. Note that a NULL filename indicates stdin.
*/
int archive_read_open_file(struct archive *, const char *_file,
size_t _block_size);
int archive_read_open_fd(struct archive *, int _fd,
size_t _block_size);
/* Parses and returns next entry header. */
int archive_read_next_header(struct archive *,
struct archive_entry **);
/*
* Retrieve the byte offset in UNCOMPRESSED data where last-read
* header started.
*/
int64_t archive_read_header_position(struct archive *);
/* Read data from the body of an entry. Similar to read(2). */
ssize_t archive_read_data(struct archive *, void *, size_t);
/*
* A zero-copy version of archive_read_data that also exposes the file offset
* of each returned block. Note that the client has no way to specify
* the desired size of the block. The API does gaurantee that offsets will
* be strictly increasing and that returned blocks will not overlap.
*/
int archive_read_data_block(struct archive *a,
const void **buff, size_t *size, off_t *offset);
/*-
* Some convenience functions that are built on archive_read_data:
* 'skip': skips entire entry
* 'into_buffer': writes data into memory buffer that you provide
* 'into_fd': writes data to specified filedes
*/
int archive_read_data_skip(struct archive *);
int archive_read_data_into_buffer(struct archive *, void *buffer,
ssize_t len);
int archive_read_data_into_fd(struct archive *, int fd);
/*-
* Convenience function to recreate the current entry (whose header
* has just been read) on disk.
*
* This does quite a bit more than just copy data to disk. It also:
* - Creates intermediate directories as required.
* - Manages directory permissions: non-writable directories will
* be initially created with write permission enabled; when the
* archive is closed, dir permissions are edited to the values specified
* in the archive.
* - Checks hardlinks: hardlinks will not be extracted unless the
* linked-to file was also extracted within the same session. (TODO)
*/
/* The "flags" argument selects optional behavior, 'OR' the flags you want. */
/* TODO: The 'Default' comments here are not quite correct; clean this up. */
#define ARCHIVE_EXTRACT_OWNER (1) /* Default: owner/group not restored */
#define ARCHIVE_EXTRACT_PERM (2) /* Default: restore perm only for reg file*/
#define ARCHIVE_EXTRACT_TIME (4) /* Default: mod time not restored */
#define ARCHIVE_EXTRACT_NO_OVERWRITE (8) /* Default: Replace files on disk */
#define ARCHIVE_EXTRACT_UNLINK (16) /* Default: don't unlink existing files */
#define ARCHIVE_EXTRACT_ACL (32) /* Default: don't restore ACLs */
#define ARCHIVE_EXTRACT_FFLAGS (64) /* Default: don't restore fflags */
int archive_read_extract(struct archive *, struct archive_entry *,
int flags);
void archive_read_extract_set_progress_callback(struct archive *,
void (*_progress_func)(void *), void *_user_data);
/* Close the file and release most resources. */
int archive_read_close(struct archive *);
/* Release all resources and destroy the object. */
/* Note that archive_read_finish will call archive_read_close for you. */
void archive_read_finish(struct archive *);
/*-
* To create an archive:
* 1) Ask archive_write_new for a archive writer object.
* 2) Set any global properties. In particular, you should set
* the compression and format to use.
* 3) Call archive_write_open to open the file (most people
* will use archive_write_open_file or archive_write_open_fd,
* which provide convenient canned I/O callbacks for you).
* 4) For each entry:
* - construct an appropriate struct archive_entry structure
* - archive_write_header to write the header
* - archive_write_data to write the entry data
* 5) archive_write_close to close the output
* 6) archive_write_finish to cleanup the writer and release resources
*/
struct archive *archive_write_new(void);
int archive_write_set_bytes_per_block(struct archive *,
int bytes_per_block);
/* XXX This is badly misnamed; suggestions appreciated. XXX */
int archive_write_set_bytes_in_last_block(struct archive *,
int bytes_in_last_block);
int archive_write_set_compression_bzip2(struct archive *);
int archive_write_set_compression_gzip(struct archive *);
int archive_write_set_compression_none(struct archive *);
/* A convenience function to set the format based on the code or name. */
int archive_write_set_format(struct archive *, int format_code);
int archive_write_set_format_by_name(struct archive *,
const char *name);
/* To minimize link pollution, use one or more of the following. */
int archive_write_set_format_cpio(struct archive *);
/* TODO: int archive_write_set_format_old_tar(struct archive *); */
int archive_write_set_format_pax(struct archive *);
int archive_write_set_format_pax_restricted(struct archive *);
int archive_write_set_format_shar(struct archive *);
int archive_write_set_format_shar_dump(struct archive *);
int archive_write_set_format_ustar(struct archive *);
int archive_write_open(struct archive *, void *,
archive_open_callback *, archive_write_callback *,
archive_close_callback *);
int archive_write_open_fd(struct archive *, int _fd);
int archive_write_open_file(struct archive *, const char *_file);
/*
* Note that the library will truncate writes beyond the size provided
* to archive_write_header or pad if the provided data is short.
*/
int archive_write_header(struct archive *,
struct archive_entry *);
/* TODO: should be ssize_t, but that might require .so version bump? */
int archive_write_data(struct archive *, const void *, size_t);
int archive_write_close(struct archive *);
void archive_write_finish(struct archive *);
/*
* Accessor functions to read/set various information in
* the struct archive object:
*/
/* Bytes written after compression or read before decompression. */
int64_t archive_position_compressed(struct archive *);
/* Bytes written to compressor or read from decompressor. */
int64_t archive_position_uncompressed(struct archive *);
const char *archive_compression_name(struct archive *);
int archive_compression(struct archive *);
int archive_errno(struct archive *);
const char *archive_error_string(struct archive *);
const char *archive_format_name(struct archive *);
int archive_format(struct archive *);
void archive_set_error(struct archive *, int _err, const char *fmt, ...);
//--- archive_string.h
/*
* Basic resizable/reusable string support a la Java's "StringBuffer."
*
* Unlike sbuf(9), the buffers here are fully reusable and track the
* length throughout.
*
* Note that all visible symbols here begin with "__archive" as they
* are internal symbols not intended for anyone outside of this library
* to see or use.
*/
struct archive_string {
char *s; /* Pointer to the storage */
size_t length; /* Length of 's' */
size_t buffer_length; /* Length of malloc-ed storage */
};
/* Initialize an archive_string object on the stack or elsewhere. */
#define archive_string_init(a) \
do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
/* Append a C char to an archive_string, resizing as necessary. */
struct archive_string *
__archive_strappend_char(struct archive_string *, char);
#define archive_strappend_char __archive_strappend_char
/* Append a char to an archive_string using UTF8. */
struct archive_string *
__archive_strappend_char_UTF8(struct archive_string *, int);
#define archive_strappend_char_UTF8 __archive_strappend_char_UTF8
/* Append an integer in the specified base (2 <= base <= 16). */
struct archive_string *
__archive_strappend_int(struct archive_string *as, int d, int base);
#define archive_strappend_int __archive_strappend_int
/* Basic append operation. */
struct archive_string *
__archive_string_append(struct archive_string *as, const char *p, size_t s);
/* Ensure that the underlying buffer is at least as large as the request. */
struct archive_string *
__archive_string_ensure(struct archive_string *, size_t);
#define archive_string_ensure __archive_string_ensure
/* Append C string, which may lack trailing \0. */
struct archive_string *
__archive_strncat(struct archive_string *, const char *, size_t);
#define archive_strncat __archive_strncat
/* Append a C string to an archive_string, resizing as necessary. */
#define archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
/* Copy a C string to an archive_string, resizing as necessary. */
#define archive_strcpy(as,p) \
((as)->length = 0, __archive_string_append((as), (p), strlen(p)))
/* Copy a C string to an archive_string with limit, resizing as necessary. */
#define archive_strncpy(as,p,l) \
((as)->length=0, archive_strncat((as), (p), (l)))
/* Return length of string. */
#define archive_strlen(a) ((a)->length)
/* Set string length to zero. */
#define archive_string_empty(a) ((a)->length = 0)
/* Release any allocated storage resources. */
void __archive_string_free(struct archive_string *);
#define archive_string_free __archive_string_free
/* Like 'vsprintf', but resizes the underlying string as necessary. */
void __archive_string_vsprintf(struct archive_string *, const char *,
va_list);
#define archive_string_vsprintf __archive_string_vsprintf
//--- archive_private.h
#define ARCHIVE_WRITE_MAGIC (0xb0c5c0deU)
#define ARCHIVE_READ_MAGIC (0xdeb0c5U)
struct archive {
/*
* The magic/state values are used to sanity-check the
* client's usage. If an API function is called at a
* rediculous time, or the client passes us an invalid
* pointer, these values allow me to catch that.
*/
unsigned magic;
unsigned state;
struct archive_entry *entry;
uid_t user_uid; /* UID of current user. */
/* Dev/ino of the archive being read/written. */
dev_t skip_file_dev;
ino_t skip_file_ino;
/* Utility: Pointer to a block of nulls. */
const unsigned char *nulls;
size_t null_length;
/*
* Used by archive_read_data() to track blocks and copy
* data to client buffers, filling gaps with zero bytes.
*/
const char *read_data_block;
off_t read_data_offset;
off_t read_data_output_offset;
size_t read_data_remaining;
/* Callbacks to open/read/write/close archive stream. */
archive_open_callback *client_opener;
archive_read_callback *client_reader;
archive_write_callback *client_writer;
archive_close_callback *client_closer;
void *client_data;
/*
* Blocking information. Note that bytes_in_last_block is
* misleadingly named; I should find a better name. These
* control the final output from all compressors, including
* compression_none.
*/
int bytes_per_block;
int bytes_in_last_block;
/*
* These control whether data within a gzip/bzip2 compressed
* stream gets padded or not. If pad_uncompressed is set,
* the data will be padded to a full block before being
* compressed. The pad_uncompressed_byte determines the value
* that will be used for padding. Note that these have no
* effect on compression "none."
*/
int pad_uncompressed;
int pad_uncompressed_byte; /* TODO: Support this. */
/* Position in UNCOMPRESSED data stream. */
off_t file_position;
/* Position in COMPRESSED data stream. */
off_t raw_position;
/* File offset of beginning of most recently-read header. */
off_t header_position;
/*
* Detection functions for decompression: bid functions are
* given a block of data from the beginning of the stream and
* can bid on whether or not they support the data stream.
* General guideline: bid the number of bits that you actually
* test, e.g., 16 if you test a 2-byte magic value. The
* highest bidder will have their init function invoked, which
* can set up pointers to specific handlers.
*
* On write, the client just invokes an archive_write_set function
* which sets up the data here directly.
*/
int compression_code; /* Currently active compression. */
const char *compression_name;
struct {
int (*bid)(const void *buff, size_t);
int (*init)(struct archive *, const void *buff, size_t);
} decompressors[4];
/* Read/write data stream (with compression). */
void *compression_data; /* Data for (de)compressor. */
int (*compression_init)(struct archive *); /* Initialize. */
int (*compression_finish)(struct archive *);
int (*compression_write)(struct archive *, const void *, size_t);
/*
* Read uses a peek/consume I/O model: the decompression code
* returns a pointer to the requested block and advances the
* file position only when requested by a consume call. This
* reduces copying and also simplifies look-ahead for format
* detection.
*/
ssize_t (*compression_read_ahead)(struct archive *,
const void **, size_t request);
ssize_t (*compression_read_consume)(struct archive *, size_t);
/*
* Format detection is mostly the same as compression
* detection, with two significant differences: The bidders
* use the read_ahead calls above to examine the stream rather
* than having the supervisor hand them a block of data to
* examine, and the auction is repeated for every header.
* Winning bidders should set the archive_format and
* archive_format_name appropriately. Bid routines should
* check archive_format and decline to bid if the format of
* the last header was incompatible.
*
* Again, write support is considerably simpler because there's
* no need for an auction.
*/
int archive_format;
const char *archive_format_name;
struct archive_format_descriptor {
int (*bid)(struct archive *);
int (*read_header)(struct archive *, struct archive_entry *);
int (*read_data)(struct archive *, const void **, size_t *, off_t *);
int (*read_data_skip)(struct archive *);
int (*cleanup)(struct archive *);
void *format_data; /* Format-specific data for readers. */
} formats[8];
struct archive_format_descriptor *format; /* Active format. */
/*
* Storage for format-specific data. Note that there can be
* multiple format readers active at one time, so we need to
* allow for multiple format readers to have their data
* available. The pformat_data slot here is the solution: on
* read, it is gauranteed to always point to a void* variable
* that the format can use.
*/
void **pformat_data; /* Pointer to current format_data. */
void *format_data; /* Used by writers. */
/*
* Pointers to format-specific functions for writing. They're
* initialized by archive_write_set_format_XXX() calls.
*/
int (*format_init)(struct archive *); /* Only used on write. */
int (*format_finish)(struct archive *);
int (*format_finish_entry)(struct archive *);
int (*format_write_header)(struct archive *,
struct archive_entry *);
int (*format_write_data)(struct archive *,
const void *buff, size_t);
/*
* Various information needed by archive_extract.
*/
struct extract *extract;
void (*extract_progress)(void *);
void *extract_progress_user_data;
void (*cleanup_archive_extract)(struct archive *);
int archive_error_number;
const char *error;
struct archive_string error_string;
};
#define ARCHIVE_STATE_ANY 0xFFFFU
#define ARCHIVE_STATE_NEW 1U
#define ARCHIVE_STATE_HEADER 2U
#define ARCHIVE_STATE_DATA 4U
#define ARCHIVE_STATE_EOF 8U
#define ARCHIVE_STATE_CLOSED 0x10U
#define ARCHIVE_STATE_FATAL 0x8000U
/* Check magic value and state; exit if it isn't valid. */
void __archive_check_magic(struct archive *, unsigned magic,
unsigned state, const char *func);
int __archive_read_register_format(struct archive *a,
void *format_data,
int (*bid)(struct archive *),
int (*read_header)(struct archive *, struct archive_entry *),
int (*read_data)(struct archive *, const void **, size_t *, off_t *),
int (*read_data_skip)(struct archive *),
int (*cleanup)(struct archive *));
int __archive_read_register_compression(struct archive *a,
int (*bid)(const void *, size_t),
int (*init)(struct archive *, const void *, size_t));
void __archive_errx(int retvalue, const char *msg);
#define err_combine(a,b) ((a) < (b) ? (a) : (b))
/*
* Utility function to format a USTAR header into a buffer. If
* "strict" is set, this tries to create the absolutely most portable
* version of a ustar header. If "strict" is set to 0, then it will
* relax certain requirements.
*
* Generally, format-specific declarations don't belong in this
* header; this is a rare example of a function that is shared by
* two very similar formats (ustar and pax).
*/
int
__archive_write_format_header_ustar(struct archive *, char buff[512],
struct archive_entry *, int tartype, int strict);
//--- archive_entry.h
/*
* Description of an archive entry.
*
* Basically, a "struct stat" with a few text fields added in.
*
* TODO: Add "comment", "charset", and possibly other entries that are
* supported by "pax interchange" format. However, GNU, ustar, cpio,
* and other variants don't support these features, so they're not an
* excruciatingly high priority right now.
*
* TODO: "pax interchange" format allows essentially arbitrary
* key/value attributes to be attached to any entry. Supporting
* such extensions may make this library useful for special
* applications (e.g., a package manager could attach special
* package-management attributes to each entry).
*/
struct archive_entry;
/*
* Basic object manipulation
*/
struct archive_entry *archive_entry_clear(struct archive_entry *);
/* The 'clone' function does a deep copy; all of the strings are copied too. */
struct archive_entry *archive_entry_clone(struct archive_entry *);
void archive_entry_free(struct archive_entry *);
struct archive_entry *archive_entry_new(void);
/*
* Retrieve fields from an archive_entry.
*/
time_t archive_entry_atime(struct archive_entry *);
long archive_entry_atime_nsec(struct archive_entry *);
time_t archive_entry_ctime(struct archive_entry *);
long archive_entry_ctime_nsec(struct archive_entry *);
dev_t archive_entry_dev(struct archive_entry *);
void archive_entry_fflags(struct archive_entry *,
unsigned long *set, unsigned long *clear);
const char *archive_entry_fflags_text(struct archive_entry *);
gid_t archive_entry_gid(struct archive_entry *);
const char *archive_entry_gname(struct archive_entry *);
const wchar_t *archive_entry_gname_w(struct archive_entry *);
const char *archive_entry_hardlink(struct archive_entry *);
const wchar_t *archive_entry_hardlink_w(struct archive_entry *);
ino_t archive_entry_ino(struct archive_entry *);
mode_t archive_entry_mode(struct archive_entry *);
time_t archive_entry_mtime(struct archive_entry *);
long archive_entry_mtime_nsec(struct archive_entry *);
const char *archive_entry_pathname(struct archive_entry *);
const wchar_t *archive_entry_pathname_w(struct archive_entry *);
dev_t archive_entry_rdev(struct archive_entry *);
int64_t archive_entry_size(struct archive_entry *);
const struct stat *archive_entry_stat(struct archive_entry *);
const char *archive_entry_symlink(struct archive_entry *);
const wchar_t *archive_entry_symlink_w(struct archive_entry *);
uid_t archive_entry_uid(struct archive_entry *);
const char *archive_entry_uname(struct archive_entry *);
const wchar_t *archive_entry_uname_w(struct archive_entry *);
/*
* Set fields in an archive_entry.
*
* Note that string 'set' functions do not copy the string, only the pointer.
* In contrast, 'copy' functions do copy the object pointed to.
*/
void archive_entry_copy_stat(struct archive_entry *, const struct stat *);
void archive_entry_set_atime(struct archive_entry *, time_t, long);
void archive_entry_set_ctime(struct archive_entry *, time_t, long);
void archive_entry_set_fflags(struct archive_entry *,
unsigned long set, unsigned long clear);
/* Returns pointer to start of first invalid token, or NULL if none. */
/* Note that all recognized tokens are processed, regardless. */
const wchar_t *archive_entry_copy_fflags_text_w(struct archive_entry *,
const wchar_t *);
void archive_entry_set_gid(struct archive_entry *, gid_t);
void archive_entry_set_gname(struct archive_entry *, const char *);
void archive_entry_copy_gname_w(struct archive_entry *, const wchar_t *);
void archive_entry_set_hardlink(struct archive_entry *, const char *);
void archive_entry_copy_hardlink(struct archive_entry *, const char *);
void archive_entry_copy_hardlink_w(struct archive_entry *, const wchar_t *);
void archive_entry_set_link(struct archive_entry *, const char *);
void archive_entry_set_mode(struct archive_entry *, mode_t);
void archive_entry_set_mtime(struct archive_entry *, time_t, long);
void archive_entry_set_pathname(struct archive_entry *, const char *);
void archive_entry_copy_pathname(struct archive_entry *, const char *);
void archive_entry_copy_pathname_w(struct archive_entry *, const wchar_t *);
void archive_entry_set_size(struct archive_entry *, int64_t);
void archive_entry_set_symlink(struct archive_entry *, const char *);
void archive_entry_copy_symlink_w(struct archive_entry *, const wchar_t *);
void archive_entry_set_uid(struct archive_entry *, uid_t);
void archive_entry_set_uname(struct archive_entry *, const char *);
void archive_entry_copy_uname_w(struct archive_entry *, const wchar_t *);
/*
* ACL routines. This used to simply store and return text-format ACL
* strings, but that proved insufficient for a number of reasons:
* = clients need control over uname/uid and gname/gid mappings
* = there are many different ACL text formats
* = would like to be able to read/convert archives containing ACLs
* on platforms that lack ACL libraries
*/
/*
* Permission bits mimic POSIX.1e. Note that I've not followed POSIX.1e's
* "permset"/"perm" abstract type nonsense. A permset is just a simple
* bitmap, following long-standing Unix tradition.
*/
#define ARCHIVE_ENTRY_ACL_EXECUTE 1
#define ARCHIVE_ENTRY_ACL_WRITE 2
#define ARCHIVE_ENTRY_ACL_READ 4
/* We need to be able to specify either or both of these. */
#define ARCHIVE_ENTRY_ACL_TYPE_ACCESS 256
#define ARCHIVE_ENTRY_ACL_TYPE_DEFAULT 512
/* Tag values mimic POSIX.1e */
#define ARCHIVE_ENTRY_ACL_USER 10001 /* Specified user. */
#define ARCHIVE_ENTRY_ACL_USER_OBJ 10002 /* User who owns the file. */
#define ARCHIVE_ENTRY_ACL_GROUP 10003 /* Specified group. */
#define ARCHIVE_ENTRY_ACL_GROUP_OBJ 10004 /* Group who owns the file. */
#define ARCHIVE_ENTRY_ACL_MASK 10005 /* Modify group access. */
#define ARCHIVE_ENTRY_ACL_OTHER 10006 /* Public. */
/*
* Set the ACL by clearing it and adding entries one at a time.
* Unlike the POSIX.1e ACL routines, you must specify the type
* (access/default) for each entry. Internally, the ACL data is just
* a soup of entries. API calls here allow you to retrieve just the
* entries of interest. This design (which goes against the spirit of
* POSIX.1e) is useful for handling archive formats that combine
* default and access information in a single ACL list.
*/
void archive_entry_acl_clear(struct archive_entry *);
void archive_entry_acl_add_entry(struct archive_entry *,
int type, int permset, int tag, int qual, const char *name);
void archive_entry_acl_add_entry_w(struct archive_entry *,
int type, int permset, int tag, int qual, const wchar_t *name);
/*
* To retrieve the ACL, first "reset", then repeatedly ask for the
* "next" entry. The want_type parameter allows you to request only
* access entries or only default entries.
*/
int archive_entry_acl_reset(struct archive_entry *, int want_type);
int archive_entry_acl_next(struct archive_entry *, int want_type,
int *type, int *permset, int *tag, int *qual, const char **name);
int archive_entry_acl_next_w(struct archive_entry *, int want_type,
int *type, int *permset, int *tag, int *qual,
const wchar_t **name);
/*
* Construct a text-format ACL. The flags argument is a bitmask that
* can include any of the following:
*
* ARCHIVE_ENTRY_ACL_TYPE_ACCESS - Include access entries.
* ARCHIVE_ENTRY_ACL_TYPE_DEFAULT - Include default entries.
* ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID - Include extra numeric ID field in
* each ACL entry. (As used by 'star'.)
* ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT - Include "default:" before each
* default ACL entry.
*/
#define ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID 1024
#define ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT 2048
const wchar_t *archive_entry_acl_text_w(struct archive_entry *, int flags);
/* Return a count of entries matching 'want_type' */
int archive_entry_acl_count(struct archive_entry *, int want_type);
/*
* Private ACL parser. This is private because it handles some
* very weird formats that clients should not be messing with.
* Clients should only deal with their platform-native formats.
* Because of the need to support many formats cleanly, new arguments
* are likely to get added on a regular basis. Clients who try to use
* this interface are likely to be surprised when it changes.
*
* You were warned!
*/
int __archive_entry_acl_parse_w(struct archive_entry *,
const wchar_t *, int type);
//--- archive_write.h
/*
* Allocate, initialize and return an archive object.
*/
struct archive *
archive_write_new(void)
{
struct archive *a;
unsigned char *nulls;
a = (struct archive*)malloc(sizeof(*a));
if (a == NULL)
return (NULL);
memset(a, 0, sizeof(*a));
a->magic = ARCHIVE_WRITE_MAGIC;
#ifdef HAVE_GETEUID
a->user_uid = geteuid();
#endif
a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK;
a->bytes_in_last_block = -1; /* Default */
a->state = ARCHIVE_STATE_NEW;
a->pformat_data = &(a->format_data);
/* Initialize a block of nulls for padding purposes. */
a->null_length = 1024;
nulls = (unsigned char*)malloc(a->null_length);
if (nulls == NULL) {
free(a);
return (NULL);
}
memset(nulls, 0, a->null_length);
a->nulls = nulls;
/*
* Set default compression, but don't set a default format.
* Were we to set a default format here, we would force every
* client to link in support for that format, even if they didn't
* ever use it.
*/
archive_write_set_compression_none(a);
return (a);
}
/*
* Set the block size. Returns 0 if successful.
*/
int
archive_write_set_bytes_per_block(struct archive *a, int bytes_per_block)
{
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
a->bytes_per_block = bytes_per_block;
return (ARCHIVE_OK);
}
/*
* Set the size for the last block.
* Returns 0 if successful.
*/
int
archive_write_set_bytes_in_last_block(struct archive *a, int bytes)
{
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
a->bytes_in_last_block = bytes;
return (ARCHIVE_OK);
}
/*
* Open the archive using the current settings.
*/
int
archive_write_open(struct archive *a, void *client_data,
archive_open_callback *opener, archive_write_callback *writer,
archive_close_callback *closer)
{
int ret;
ret = ARCHIVE_OK;
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_open");
archive_string_empty(&a->error_string);
a->state = ARCHIVE_STATE_HEADER;
a->client_data = client_data;
a->client_writer = writer;
a->client_opener = opener;
a->client_closer = closer;
ret = (a->compression_init)(a);
if (a->format_init && ret == ARCHIVE_OK)
ret = (a->format_init)(a);
return (ret);
}
/*
* Close out the archive.
*
* Be careful: user might just call write_new and then write_finish.
* Don't assume we actually wrote anything or performed any non-trivial
* initialization.
*/
int
archive_write_close(struct archive *a)
{
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_close");
/* Finish the last entry. */
if (a->state & ARCHIVE_STATE_DATA)
((a->format_finish_entry)(a));
/* Finish off the archive. */
if (a->format_finish != NULL)
(a->format_finish)(a);
/* Finish the compression and close the stream. */
if (a->compression_finish != NULL)
(a->compression_finish)(a);
a->state = ARCHIVE_STATE_CLOSED;
return (ARCHIVE_OK);
}
/*
* Destroy the archive structure.
*/
void
archive_write_finish(struct archive *a)
{
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_ANY, "archive_write_finish");
if (a->state != ARCHIVE_STATE_CLOSED)
archive_write_close(a);
/* Release various dynamic buffers. */
free((void *)(uintptr_t)(const void *)a->nulls);
archive_string_free(&a->error_string);
a->magic = 0;
free(a);
}
/*
* Write the appropriate header.
*/
int
archive_write_header(struct archive *a, struct archive_entry *entry)
{
int ret;
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC,
ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, "archive_write_header");
archive_string_empty(&a->error_string);
/* Finish last entry. */
if (a->state & ARCHIVE_STATE_DATA)
((a->format_finish_entry)(a));
if (archive_entry_dev(entry) == a->skip_file_dev &&
archive_entry_ino(entry) == a->skip_file_ino) {
archive_set_error(a, 0, "Can't add archive to itself");
return (ARCHIVE_WARN);
}
/* Format and write header. */
ret = ((a->format_write_header)(a, entry));
a->state = ARCHIVE_STATE_DATA;
return (ret);
}
/*
* Note that the compressor is responsible for blocking.
*/
/* Should be "ssize_t", but that breaks the ABI. <sigh> */
int
archive_write_data(struct archive *a, const void *buff, size_t s)
{
int ret;
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_DATA, "archive_write_data");
archive_string_empty(&a->error_string);
ret = (a->format_write_data)(a, buff, s);
return (ret == ARCHIVE_OK ? (ssize_t)s : -1);
}
//--- archive_write_set_compression_none.c
static int archive_compressor_none_finish(struct archive *a);
static int archive_compressor_none_init(struct archive *);
static int archive_compressor_none_write(struct archive *, const void *,
size_t);
struct archive_none {
char *buffer;
ssize_t buffer_size;
char *next; /* Current insert location */
ssize_t avail; /* Free space left in buffer */
};
int
archive_write_set_compression_none(struct archive *a)
{
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_compression_none");
a->compression_init = &archive_compressor_none_init;
a->compression_code = ARCHIVE_COMPRESSION_NONE;
a->compression_name = "none";
return (0);
}
/*
* Setup callback.
*/
static int
archive_compressor_none_init(struct archive *a)
{
int ret;
struct archive_none *state;
a->compression_code = ARCHIVE_COMPRESSION_NONE;
a->compression_name = "none";
if (a->client_opener != NULL) {
ret = (a->client_opener)(a, a->client_data);
if (ret != 0)
return (ret);
}
state = (struct archive_none *)malloc(sizeof(*state));
if (state == NULL) {
archive_set_error(a, ENOMEM,
"Can't allocate data for output buffering");
return (ARCHIVE_FATAL);
}
memset(state, 0, sizeof(*state));
state->buffer_size = a->bytes_per_block;
state->buffer = (char*)malloc(state->buffer_size);
if (state->buffer == NULL) {
archive_set_error(a, ENOMEM,
"Can't allocate output buffer");
free(state);
return (ARCHIVE_FATAL);
}
state->next = state->buffer;
state->avail = state->buffer_size;
a->compression_data = state;
a->compression_write = archive_compressor_none_write;
a->compression_finish = archive_compressor_none_finish;
return (ARCHIVE_OK);
}
/*
* Write data to the stream.
*/
static int
archive_compressor_none_write(struct archive *a, const void *vbuff,
size_t length)
{
const char *buff;
ssize_t remaining, to_copy;
ssize_t bytes_written;
struct archive_none *state;
state = (struct archive_none*)a->compression_data;
buff = (const char*)vbuff;
if (a->client_writer == NULL) {
archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
"No write callback is registered? "
"This is probably an internal programming error.");
return (ARCHIVE_FATAL);
}
remaining = length;
while (remaining > 0) {
/*
* If we have a full output block, write it and reset the
* output buffer.
*/
if (state->avail == 0) {
bytes_written = (a->client_writer)(a, a->client_data,
state->buffer, state->buffer_size);
if (bytes_written <= 0)
return (ARCHIVE_FATAL);
/* XXX TODO: if bytes_written < state->buffer_size */
a->raw_position += bytes_written;
state->next = state->buffer;
state->avail = state->buffer_size;
}
/* Now we have space in the buffer; copy new data into it. */
to_copy = (remaining > state->avail) ?
state->avail : remaining;
memcpy(state->next, buff, to_copy);
state->next += to_copy;
state->avail -= to_copy;
buff += to_copy;
remaining -= to_copy;
}
a->file_position += length;
return (ARCHIVE_OK);
}
/*
* Finish the compression.
*/
static int
archive_compressor_none_finish(struct archive *a)
{
ssize_t block_length;
ssize_t target_block_length;
ssize_t bytes_written;
int ret;
int ret2;
struct archive_none *state;
state = (struct archive_none*)a->compression_data;
ret = ret2 = ARCHIVE_OK;
if (a->client_writer == NULL) {
archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
"No write callback is registered? "
"This is probably an internal programming error.");
return (ARCHIVE_FATAL);
}
/* If there's pending data, pad and write the last block */
if (state->next != state->buffer) {
block_length = state->buffer_size - state->avail;
/* Tricky calculation to determine size of last block */
target_block_length = block_length;
if (a->bytes_in_last_block <= 0)
/* Default or Zero: pad to full block */
target_block_length = a->bytes_per_block;
else
/* Round to next multiple of bytes_in_last_block. */
target_block_length = a->bytes_in_last_block *
( (block_length + a->bytes_in_last_block - 1) /
a->bytes_in_last_block);
if (target_block_length > a->bytes_per_block)
target_block_length = a->bytes_per_block;
if (block_length < target_block_length) {
memset(state->next, 0,
target_block_length - block_length);
block_length = target_block_length;
}
bytes_written = (a->client_writer)(a, a->client_data,
state->buffer, block_length);
if (bytes_written <= 0)
ret = ARCHIVE_FATAL;
else {
a->raw_position += bytes_written;
ret = ARCHIVE_OK;
}
}
/* Close the output */
if (a->client_closer != NULL)
ret2 = (a->client_closer)(a, a->client_data);
free(state->buffer);
free(state);
a->compression_data = NULL;
return (ret != ARCHIVE_OK ? ret : ret2);
}
//--- archive_check_magic.c
static void
errmsg(const char *m)
{
cmSystemTools::Error("CPack error: ", m);
}
static void
diediedie(void)
{
*(char *)0 = 1; /* Deliberately segfault and force a coredump. */
_exit(1); /* If that didn't work, just exit with an error. */
}
static const char *
state_name(unsigned s)
{
switch (s) {
case ARCHIVE_STATE_NEW: return ("new");
case ARCHIVE_STATE_HEADER: return ("header");
case ARCHIVE_STATE_DATA: return ("data");
case ARCHIVE_STATE_EOF: return ("eof");
case ARCHIVE_STATE_CLOSED: return ("closed");
case ARCHIVE_STATE_FATAL: return ("fatal");
default: return ("??");
}
}
static void
write_all_states(int states)
{
unsigned lowbit;
/* A trick for computing the lowest set bit. */
while ((lowbit = states & (-states)) != 0) {
states &= ~lowbit; /* Clear the low bit. */
errmsg(state_name(lowbit));
if (states != 0)
errmsg("/");
}
}
/*
* Check magic value and current state; bail if it isn't valid.
*
* This is designed to catch serious programming errors that violate
* the libarchive API.
*/
void
__archive_check_magic(struct archive *a, unsigned magic, unsigned state,
const char *function)
{
if (a->magic != magic) {
errmsg("INTERNAL ERROR: Function ");
errmsg(function);
errmsg(" invoked with invalid struct archive structure.\n");
diediedie();
}
if (state == ARCHIVE_STATE_ANY)
return;
if ((a->state & state) == 0) {
errmsg("INTERNAL ERROR: Function '");
errmsg(function);
errmsg("' invoked with archive structure in state '");
write_all_states(a->state);
errmsg("', should be in state '");
write_all_states(state);
errmsg("'\n");
diediedie();
}
}
//--- archive_util.c
int
archive_errno(struct archive *a)
{
return (a->archive_error_number);
}
const char *
archive_error_string(struct archive *a)
{
if (a->error != NULL && *a->error != '\0')
return (a->error);
else
return ("(Empty error message)");
}
int
archive_format(struct archive *a)
{
return (a->archive_format);
}
const char *
archive_format_name(struct archive *a)
{
return (a->archive_format_name);
}
int
archive_compression(struct archive *a)
{
return (a->compression_code);
}
const char *
archive_compression_name(struct archive *a)
{
return (a->compression_name);
}
/*
* Return a count of the number of compressed bytes processed.
*/
int64_t
archive_position_compressed(struct archive *a)
{
return (a->raw_position);
}
/*
* Return a count of the number of uncompressed bytes processed.
*/
int64_t
archive_position_uncompressed(struct archive *a)
{
return (a->file_position);
}
void
archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
{
va_list ap;
#ifdef HAVE_STRERROR_R
char errbuff[512];
#endif
char *errp;
a->archive_error_number = error_number;
if (fmt == NULL) {
a->error = NULL;
return;
}
va_start(ap, fmt);
archive_string_vsprintf(&(a->error_string), fmt, ap);
if (error_number > 0) {
archive_strcat(&(a->error_string), ": ");
#ifdef HAVE_STRERROR_R
#ifdef STRERROR_R_CHAR_P
errp = strerror_r(error_number, errbuff, sizeof(errbuff));
#else
strerror_r(error_number, errbuff, sizeof(errbuff));
errp = errbuff;
#endif
#else
/* Note: this is not threadsafe! */
errp = strerror(error_number);
#endif
archive_strcat(&(a->error_string), errp);
}
a->error = a->error_string.s;
va_end(ap);
}
void
__archive_errx(int retvalue, const char *msg)
{
static const char *msg1 = "Fatal Internal Error in libarchive: ";
write(2, msg1, strlen(msg1));
write(2, msg, strlen(msg));
write(2, "\n", 1);
exit(retvalue);
}
//--- archive_string.c
struct archive_string *
__archive_string_append(struct archive_string *as, const char *p, size_t s)
{
__archive_string_ensure(as, as->length + s + 1);
memcpy(as->s + as->length, p, s);
as->s[as->length + s] = 0;
as->length += s;
return (as);
}
void
__archive_string_free(struct archive_string *as)
{
as->length = 0;
as->buffer_length = 0;
if (as->s != NULL)
free(as->s);
}
struct archive_string *
__archive_string_ensure(struct archive_string *as, size_t s)
{
if (as->s && (s <= as->buffer_length))
return (as);
if (as->buffer_length < 32)
as->buffer_length = 32;
while (as->buffer_length < s)
as->buffer_length *= 2;
as->s = (char*)realloc(as->s, as->buffer_length);
/* TODO: Return null instead and fix up all of our callers to
* handle this correctly. */
if (as->s == NULL)
__archive_errx(1, "Out of memory");
return (as);
}
struct archive_string *
__archive_strncat(struct archive_string *as, const char *p, size_t n)
{
size_t s;
const char *pp;
/* Like strlen(p), except won't examine positions beyond p[n]. */
s = 0;
pp = p;
while (*pp && s < n) {
pp++;
s++;
}
return (__archive_string_append(as, p, s));
}
struct archive_string *
__archive_strappend_char(struct archive_string *as, char c)
{
return (__archive_string_append(as, &c, 1));
}
struct archive_string *
__archive_strappend_int(struct archive_string *as, int d, int base)
{
static const char *digits = "0123457890abcdef";
if (d < 0) {
__archive_strappend_char(as, '-');
d = -d;
}
if (d >= base)
__archive_strappend_int(as, d/base, base);
__archive_strappend_char(as, digits[d % base]);
return (as);
}
//--- archive_entry.c
/* Obtain suitable wide-character manipulation functions. */
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#else
static size_t wcslen(const wchar_t *s)
{
const wchar_t *p = s;
while (*p != L'\0')
++p;
return p - s;
}
static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
{
wchar_t *dest = s1;
while((*s1 = *s2) != L'\0')
++s1, ++s2;
return dest;
}
#define wmemcpy(a,b,i) (wchar_t *)memcpy((a),(b),(i)*sizeof(wchar_t))
/* Good enough for simple equality testing, but not for sorting. */
#define wmemcmp(a,b,i) memcmp((a),(b),(i)*sizeof(wchar_t))
#endif
#undef max
#define max(a, b) ((a)>(b)?(a):(b))
/*
* Handle wide character (i.e., Unicode) and non-wide character
* strings transparently.
*
*/
struct aes {
const char *aes_mbs;
char *aes_mbs_alloc;
const wchar_t *aes_wcs;
wchar_t *aes_wcs_alloc;
};
struct ae_acl {
struct ae_acl *next;
int type; /* E.g., access or default */
int tag; /* E.g., user/group/other/mask */
int permset; /* r/w/x bits */
int id; /* uid/gid for user/group */
struct aes name; /* uname/gname */
};
static void aes_clean(struct aes *);
static void aes_copy(struct aes *dest, struct aes *src);
static const char * aes_get_mbs(struct aes *);
static const wchar_t * aes_get_wcs(struct aes *);
static void aes_set_mbs(struct aes *, const char *mbs);
static void aes_copy_mbs(struct aes *, const char *mbs);
/* static void aes_set_wcs(struct aes *, const wchar_t *wcs); */
static void aes_copy_wcs(struct aes *, const wchar_t *wcs);
static char * ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
static const wchar_t *ae_wcstofflags(const wchar_t *stringp,
unsigned long *setp, unsigned long *clrp);
static void append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
const wchar_t *wname, int perm, int id);
static void append_id_w(wchar_t **wp, int id);
static int acl_special(struct archive_entry *entry,
int type, int permset, int tag);
static struct ae_acl *acl_new_entry(struct archive_entry *entry,
int type, int permset, int tag, int id);
static void next_field_w(const wchar_t **wp, const wchar_t **start,
const wchar_t **end, wchar_t *sep);
static int prefix_w(const wchar_t *start, const wchar_t *end,
const wchar_t *test);
/*
* Description of an archive entry.
*
* Basically, this is a "struct stat" with a few text fields added in.
*
* TODO: Add "comment", "charset", and possibly other entries
* that are supported by "pax interchange" format. However, GNU, ustar,
* cpio, and other variants don't support these features, so they're not an
* excruciatingly high priority right now.
*
* TODO: "pax interchange" format allows essentially arbitrary
* key/value attributes to be attached to any entry. Supporting
* such extensions may make this library useful for special
* applications (e.g., a package manager could attach special
* package-management attributes to each entry). There are tricky
* API issues involved, so this is not going to happen until
* there's a real demand for it.
*
* TODO: Design a good API for handling sparse files.
*/
struct archive_entry {
/*
* Note that ae_stat.st_mode & S_IFMT can be 0!
*
* This occurs when the actual file type of the object is not
* in the archive. For example, 'tar' archives store
* hardlinks without marking the type of the underlying
* object.
*/
struct stat ae_stat;
/*
* Use aes here so that we get transparent mbs<->wcs conversions.
*/
struct aes ae_fflags_text; /* Text fflags per fflagstostr(3) */
unsigned long ae_fflags_set; /* Bitmap fflags */
unsigned long ae_fflags_clear;
struct aes ae_gname; /* Name of owning group */
struct aes ae_hardlink; /* Name of target for hardlink */
struct aes ae_pathname; /* Name of entry */
struct aes ae_symlink; /* symlink contents */
struct aes ae_uname; /* Name of owner */
struct ae_acl *acl_head;
struct ae_acl *acl_p;
int acl_state; /* See acl_next for details. */
wchar_t *acl_text_w;
};
static void
aes_clean(struct aes *aes)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
memset(aes, 0, sizeof(*aes));
}
static void
aes_copy(struct aes *dest, struct aes *src)
{
*dest = *src;
if (src->aes_mbs != NULL) {
dest->aes_mbs_alloc = strdup(src->aes_mbs);
dest->aes_mbs = dest->aes_mbs_alloc;
if (dest->aes_mbs == NULL)
__archive_errx(1, "No memory for aes_copy()");
}
if (src->aes_wcs != NULL) {
dest->aes_wcs_alloc = (wchar_t*)malloc((wcslen(src->aes_wcs) + 1)
* sizeof(wchar_t));
dest->aes_wcs = dest->aes_wcs_alloc;
if (dest->aes_wcs == NULL)
__archive_errx(1, "No memory for aes_copy()");
wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
}
}
static const char *
aes_get_mbs(struct aes *aes)
{
if (aes->aes_mbs == NULL && aes->aes_wcs == NULL)
return NULL;
if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
/*
* XXX Need to estimate the number of byte in the
* multi-byte form. Assume that, on average, wcs
* chars encode to no more than 3 bytes. There must
* be a better way... XXX
*/
int mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
aes->aes_mbs_alloc = (char*)malloc(mbs_length);
aes->aes_mbs = aes->aes_mbs_alloc;
if (aes->aes_mbs == NULL)
__archive_errx(1, "No memory for aes_get_mbs()");
wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
aes->aes_mbs_alloc[mbs_length - 1] = 0;
}
return (aes->aes_mbs);
}
static const wchar_t *
aes_get_wcs(struct aes *aes)
{
if (aes->aes_wcs == NULL && aes->aes_mbs == NULL)
return NULL;
if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
/*
* No single byte will be more than one wide character,
* so this length estimate will always be big enough.
*/
int wcs_length = strlen(aes->aes_mbs);
aes->aes_wcs_alloc
= (wchar_t*)malloc((wcs_length + 1) * sizeof(wchar_t));
aes->aes_wcs = aes->aes_wcs_alloc;
if (aes->aes_wcs == NULL)
__archive_errx(1, "No memory for aes_get_wcs()");
mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
aes->aes_wcs_alloc[wcs_length] = 0;
}
return (aes->aes_wcs);
}
static void
aes_set_mbs(struct aes *aes, const char *mbs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs = mbs;
aes->aes_wcs = NULL;
}
static void
aes_copy_mbs(struct aes *aes, const char *mbs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs_alloc = (char*)malloc((strlen(mbs) + 1) * sizeof(char));
if (aes->aes_mbs_alloc == NULL)
__archive_errx(1, "No memory for aes_copy_mbs()");
strcpy(aes->aes_mbs_alloc, mbs);
aes->aes_mbs = aes->aes_mbs_alloc;
aes->aes_wcs = NULL;
}
#if 0
static void
aes_set_wcs(struct aes *aes, const wchar_t *wcs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs = NULL;
aes->aes_wcs = wcs;
}
#endif
static void
aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
{
if (aes->aes_mbs_alloc) {
free(aes->aes_mbs_alloc);
aes->aes_mbs_alloc = NULL;
}
if (aes->aes_wcs_alloc) {
free(aes->aes_wcs_alloc);
aes->aes_wcs_alloc = NULL;
}
aes->aes_mbs = NULL;
aes->aes_wcs_alloc = (wchar_t*)malloc((wcslen(wcs) + 1) * sizeof(wchar_t));
if (aes->aes_wcs_alloc == NULL)
__archive_errx(1, "No memory for aes_copy_wcs()");
wcscpy(aes->aes_wcs_alloc, wcs);
aes->aes_wcs = aes->aes_wcs_alloc;
}
struct archive_entry *
archive_entry_clear(struct archive_entry *entry)
{
aes_clean(&entry->ae_fflags_text);
aes_clean(&entry->ae_gname);
aes_clean(&entry->ae_hardlink);
aes_clean(&entry->ae_pathname);
aes_clean(&entry->ae_symlink);
aes_clean(&entry->ae_uname);
archive_entry_acl_clear(entry);
memset(entry, 0, sizeof(*entry));
return entry;
}
struct archive_entry *
archive_entry_clone(struct archive_entry *entry)
{
struct archive_entry *entry2;
/* Allocate new structure and copy over all of the fields. */
entry2 = (struct archive_entry*)malloc(sizeof(*entry2));
if (entry2 == NULL)
return (NULL);
memset(entry2, 0, sizeof(*entry2));
entry2->ae_stat = entry->ae_stat;
entry2->ae_fflags_set = entry->ae_fflags_set;
entry2->ae_fflags_clear = entry->ae_fflags_clear;
aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
aes_copy(&entry2->ae_gname, &entry->ae_gname);
aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
aes_copy(&entry2->ae_uname, &entry->ae_uname);
/* XXX TODO: Copy ACL data over as well. XXX */
return (entry2);
}
void
archive_entry_free(struct archive_entry *entry)
{
archive_entry_clear(entry);
free(entry);
}
struct archive_entry *
archive_entry_new(void)
{
struct archive_entry *entry;
entry = (struct archive_entry*)malloc(sizeof(*entry));
if (entry == NULL)
return (NULL);
memset(entry, 0, sizeof(*entry));
return (entry);
}
/*
* Functions for reading fields from an archive_entry.
*/
time_t
archive_entry_atime(struct archive_entry *entry)
{
return (entry->ae_stat.st_atime);
}
long
archive_entry_atime_nsec(struct archive_entry *entry)
{
(void)entry; /* entry can be unused here. */
return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
}
time_t
archive_entry_ctime(struct archive_entry *entry)
{
return (entry->ae_stat.st_ctime);
}
long
archive_entry_ctime_nsec(struct archive_entry *entry)
{
(void)entry; /* entry can be unused here. */
return (ARCHIVE_STAT_CTIME_NANOS(&entry->ae_stat));
}
dev_t
archive_entry_dev(struct archive_entry *entry)
{
return (entry->ae_stat.st_dev);
}
void
archive_entry_fflags(struct archive_entry *entry,
unsigned long *set, unsigned long *clear)
{
*set = entry->ae_fflags_set;
*clear = entry->ae_fflags_clear;
}
/*
* Note: if text was provided, this just returns that text. If you
* really need the text to be rebuilt in a canonical form, set the
* text, ask for the bitmaps, then set the bitmaps. (Setting the
* bitmaps clears any stored text.) This design is deliberate: if
* we're editing archives, we don't want to discard flags just because
* they aren't supported on the current system. The bitmap<->text
* conversions are platform-specific (see below).
*/
const char *
archive_entry_fflags_text(struct archive_entry *entry)
{
const char *f;
char *p;
f = aes_get_mbs(&entry->ae_fflags_text);
if (f != NULL)
return (f);
if (entry->ae_fflags_set == 0 && entry->ae_fflags_clear == 0)
return (NULL);
p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
if (p == NULL)
return (NULL);
aes_copy_mbs(&entry->ae_fflags_text, p);
free(p);
f = aes_get_mbs(&entry->ae_fflags_text);
return (f);
}
gid_t
archive_entry_gid(struct archive_entry *entry)
{
return (entry->ae_stat.st_gid);
}
const char *
archive_entry_gname(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_gname));
}
const wchar_t *
archive_entry_gname_w(struct archive_entry *entry)
{
return (aes_get_wcs(&entry->ae_gname));
}
const char *
archive_entry_hardlink(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_hardlink));
}
const wchar_t *
archive_entry_hardlink_w(struct archive_entry *entry)
{
return (aes_get_wcs(&entry->ae_hardlink));
}
ino_t
archive_entry_ino(struct archive_entry *entry)
{
return (entry->ae_stat.st_ino);
}
mode_t
archive_entry_mode(struct archive_entry *entry)
{
return (entry->ae_stat.st_mode);
}
time_t
archive_entry_mtime(struct archive_entry *entry)
{
return (entry->ae_stat.st_mtime);
}
long
archive_entry_mtime_nsec(struct archive_entry *entry)
{
(void)entry; /* entry can be unused here. */
return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
}
const char *
archive_entry_pathname(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_pathname));
}
const wchar_t *
archive_entry_pathname_w(struct archive_entry *entry)
{
return (aes_get_wcs(&entry->ae_pathname));
}
dev_t
archive_entry_rdev(struct archive_entry *entry)
{
return (entry->ae_stat.st_rdev);
}
int64_t
archive_entry_size(struct archive_entry *entry)
{
return (entry->ae_stat.st_size);
}
const struct stat *
archive_entry_stat(struct archive_entry *entry)
{
return (&entry->ae_stat);
}
const char *
archive_entry_symlink(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_symlink));
}
const wchar_t *
archive_entry_symlink_w(struct archive_entry *entry)
{
return (aes_get_wcs(&entry->ae_symlink));
}
uid_t
archive_entry_uid(struct archive_entry *entry)
{
return (entry->ae_stat.st_uid);
}
const char *
archive_entry_uname(struct archive_entry *entry)
{
return (aes_get_mbs(&entry->ae_uname));
}
const wchar_t *
archive_entry_uname_w(struct archive_entry *entry)
{
return (aes_get_wcs(&entry->ae_uname));
}
/*
* Functions to set archive_entry properties.
*/
/*
* Note "copy" not "set" here. The "set" functions that accept a pointer
* only store the pointer; they don't copy the underlying object.
*/
void
archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
{
entry->ae_stat = *st;
}
void
archive_entry_set_fflags(struct archive_entry *entry,
unsigned long set, unsigned long clear)
{
aes_clean(&entry->ae_fflags_text);
entry->ae_fflags_set = set;
entry->ae_fflags_clear = clear;
}
const wchar_t *
archive_entry_copy_fflags_text_w(struct archive_entry *entry,
const wchar_t *flags)
{
aes_copy_wcs(&entry->ae_fflags_text, flags);
return (ae_wcstofflags(flags,
&entry->ae_fflags_set, &entry->ae_fflags_clear));
}
void
archive_entry_set_gid(struct archive_entry *entry, gid_t g)
{
entry->ae_stat.st_gid = g;
}
void
archive_entry_set_gname(struct archive_entry *entry, const char *name)
{
aes_set_mbs(&entry->ae_gname, name);
}
void
archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
{
aes_copy_wcs(&entry->ae_gname, name);
}
void
archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
{
aes_set_mbs(&entry->ae_hardlink, target);
}
void
archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
{
aes_copy_mbs(&entry->ae_hardlink, target);
}
void
archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
{
aes_copy_wcs(&entry->ae_hardlink, target);
}
void
archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
{
(void)ns;
entry->ae_stat.st_atime = t;
ARCHIVE_STAT_SET_ATIME_NANOS(&entry->ae_stat, ns);
}
void
archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
{
(void)ns;
entry->ae_stat.st_ctime = t;
ARCHIVE_STAT_SET_CTIME_NANOS(&entry->ae_stat, ns);
}
/* Set symlink if symlink is already set, else set hardlink. */
void
archive_entry_set_link(struct archive_entry *entry, const char *target)
{
if (entry->ae_symlink.aes_mbs != NULL ||
entry->ae_symlink.aes_wcs != NULL)
aes_set_mbs(&entry->ae_symlink, target);
else
aes_set_mbs(&entry->ae_hardlink, target);
}
void
archive_entry_set_mode(struct archive_entry *entry, mode_t m)
{
entry->ae_stat.st_mode = m;
}
void
archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
{
(void)ns;
entry->ae_stat.st_mtime = m;
ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
}
void
archive_entry_set_pathname(struct archive_entry *entry, const char *name)
{
aes_set_mbs(&entry->ae_pathname, name);
}
void
archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
{
aes_copy_mbs(&entry->ae_pathname, name);
}
void
archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
{
aes_copy_wcs(&entry->ae_pathname, name);
}
void
archive_entry_set_size(struct archive_entry *entry, int64_t s)
{
entry->ae_stat.st_size = s;
}
void
archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
{
aes_set_mbs(&entry->ae_symlink, linkname);
}
void
archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
{
aes_copy_wcs(&entry->ae_symlink, linkname);
}
void
archive_entry_set_uid(struct archive_entry *entry, uid_t u)
{
entry->ae_stat.st_uid = u;
}
void
archive_entry_set_uname(struct archive_entry *entry, const char *name)
{
aes_set_mbs(&entry->ae_uname, name);
}
void
archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
{
aes_copy_wcs(&entry->ae_uname, name);
}
/*
* ACL management. The following would, of course, be a lot simpler
* if: 1) the last draft of POSIX.1e were a really thorough and
* complete standard that addressed the needs of ACL archiving and 2)
* everyone followed it faithfully. Alas, neither is true, so the
* following is a lot more complex than might seem necessary to the
* uninitiated.
*/
void
archive_entry_acl_clear(struct archive_entry *entry)
{
struct ae_acl *ap;
while (entry->acl_head != NULL) {
ap = entry->acl_head->next;
aes_clean(&entry->acl_head->name);
free(entry->acl_head);
entry->acl_head = ap;
}
if (entry->acl_text_w != NULL) {
free(entry->acl_text_w);
entry->acl_text_w = NULL;
}
entry->acl_p = NULL;
entry->acl_state = 0; /* Not counting. */
}
/*
* Add a single ACL entry to the internal list of ACL data.
*/
void
archive_entry_acl_add_entry(struct archive_entry *entry,
int type, int permset, int tag, int id, const char *name)
{
struct ae_acl *ap;
if (acl_special(entry, type, permset, tag) == 0)
return;
ap = acl_new_entry(entry, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return;
}
if (name != NULL && *name != '\0')
aes_copy_mbs(&ap->name, name);
else
aes_clean(&ap->name);
}
/*
* As above, but with a wide-character name.
*/
void
archive_entry_acl_add_entry_w(struct archive_entry *entry,
int type, int permset, int tag, int id, const wchar_t *name)
{
struct ae_acl *ap;
if (acl_special(entry, type, permset, tag) == 0)
return;
ap = acl_new_entry(entry, type, permset, tag, id);
if (ap == NULL) {
/* XXX Error XXX */
return;
}
if (name != NULL && *name != L'\0')
aes_copy_wcs(&ap->name, name);
else
aes_clean(&ap->name);
}
/*
* If this ACL entry is part of the standard POSIX permissions set,
* store the permissions in the stat structure and return zero.
*/
static int
acl_special(struct archive_entry *entry, int type, int permset, int tag)
{
if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
entry->ae_stat.st_mode &= ~0700;
entry->ae_stat.st_mode |= (permset & 7) << 6;
return (0);
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
entry->ae_stat.st_mode &= ~0070;
entry->ae_stat.st_mode |= (permset & 7) << 3;
return (0);
case ARCHIVE_ENTRY_ACL_OTHER:
entry->ae_stat.st_mode &= ~0007;
entry->ae_stat.st_mode |= permset & 7;
return (0);
}
}
return (1);
}
/*
* Allocate and populate a new ACL entry with everything but the
* name.
*/
static struct ae_acl *
acl_new_entry(struct archive_entry *entry,
int type, int permset, int tag, int id)
{
struct ae_acl *ap;
if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
return (NULL);
if (entry->acl_text_w != NULL) {
free(entry->acl_text_w);
entry->acl_text_w = NULL;
}
/* XXX TODO: More sanity-checks on the arguments XXX */
/* If there's a matching entry already in the list, overwrite it. */
for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
if (ap->type == type && ap->tag == tag && ap->id == id) {
ap->permset = permset;
return (ap);
}
}
/* Add a new entry to the list. */
ap = (struct ae_acl *)malloc(sizeof(*ap));
if (ap == NULL)
return (NULL);
memset(ap, 0, sizeof(*ap));
ap->next = entry->acl_head;
entry->acl_head = ap;
ap->type = type;
ap->tag = tag;
ap->id = id;
ap->permset = permset;
return (ap);
}
/*
* Return a count of entries matching "want_type".
*/
int
archive_entry_acl_count(struct archive_entry *entry, int want_type)
{
int count;
struct ae_acl *ap;
count = 0;
ap = entry->acl_head;
while (ap != NULL) {
if ((ap->type & want_type) != 0)
count++;
ap = ap->next;
}
if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
count += 3;
return (count);
}
/*
* Prepare for reading entries from the ACL data. Returns a count
* of entries matching "want_type", or zero if there are no
* non-extended ACL entries of that type.
*/
int
archive_entry_acl_reset(struct archive_entry *entry, int want_type)
{
int count, cutoff;
count = archive_entry_acl_count(entry, want_type);
/*
* If the only entries are the three standard ones,
* then don't return any ACL data. (In this case,
* client can just use chmod(2) to set permissions.)
*/
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
cutoff = 3;
else
cutoff = 0;
if (count > cutoff)
entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
else
entry->acl_state = 0;
entry->acl_p = entry->acl_head;
return (count);
}
/*
* Return the next ACL entry in the list. Fake entries for the
* standard permissions and include them in the returned list.
*/
int
archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
int *permset, int *tag, int *id, const char **name)
{
*name = NULL;
*id = -1;
/*
* The acl_state is either zero (no entries available), -1
* (reading from list), or an entry type (retrieve that type
* from ae_stat.st_mode).
*/
if (entry->acl_state == 0)
return (ARCHIVE_WARN);
/* The first three access entries are special. */
if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
switch (entry->acl_state) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
*permset = (entry->ae_stat.st_mode >> 6) & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
return (ARCHIVE_OK);
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
*permset = (entry->ae_stat.st_mode >> 3) & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
return (ARCHIVE_OK);
case ARCHIVE_ENTRY_ACL_OTHER:
*permset = entry->ae_stat.st_mode & 7;
*type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
*tag = ARCHIVE_ENTRY_ACL_OTHER;
entry->acl_state = -1;
entry->acl_p = entry->acl_head;
return (ARCHIVE_OK);
default:
break;
}
}
while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
entry->acl_p = entry->acl_p->next;
if (entry->acl_p == NULL) {
entry->acl_state = 0;
return (ARCHIVE_WARN);
}
*type = entry->acl_p->type;
*permset = entry->acl_p->permset;
*tag = entry->acl_p->tag;
*id = entry->acl_p->id;
*name = aes_get_mbs(&entry->acl_p->name);
entry->acl_p = entry->acl_p->next;
return (ARCHIVE_OK);
}
/*
* Generate a text version of the ACL. The flags parameter controls
* the style of the generated ACL.
*/
const wchar_t *
archive_entry_acl_text_w(struct archive_entry *entry, int flags)
{
int count;
int length;
const wchar_t *wname;
const wchar_t *prefix;
wchar_t separator;
struct ae_acl *ap;
int id;
wchar_t *wp;
if (entry->acl_text_w != NULL) {
free (entry->acl_text_w);
entry->acl_text_w = NULL;
}
separator = L',';
count = 0;
length = 0;
ap = entry->acl_head;
while (ap != NULL) {
if ((ap->type & flags) != 0) {
count++;
if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
(ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
length += 8; /* "default:" */
length += 5; /* tag name */
length += 1; /* colon */
wname = aes_get_wcs(&ap->name);
if (wname != NULL)
length += wcslen(wname);
length ++; /* colon */
length += 3; /* rwx */
length += 1; /* colon */
length += max(sizeof(uid_t),sizeof(gid_t)) * 3 + 1;
length ++; /* newline */
}
ap = ap->next;
}
if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
length += 10; /* "user::rwx\n" */
length += 11; /* "group::rwx\n" */
length += 11; /* "other::rwx\n" */
}
if (count == 0)
return (NULL);
/* Now, allocate the string and actually populate it. */
wp = entry->acl_text_w = (wchar_t*)malloc(length * sizeof(wchar_t));
if (wp == NULL)
__archive_errx(1, "No memory to generate the text version of the ACL");
count = 0;
if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
entry->ae_stat.st_mode & 0700, -1);
*wp++ = ',';
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
entry->ae_stat.st_mode & 0070, -1);
*wp++ = ',';
append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
entry->ae_stat.st_mode & 0007, -1);
count += 3;
ap = entry->acl_head;
while (ap != NULL) {
if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
wname = aes_get_wcs(&ap->name);
*wp++ = separator;
if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
id = ap->id;
else
id = -1;
append_entry_w(&wp, NULL, ap->tag, wname,
ap->permset, id);
count++;
}
ap = ap->next;
}
}
if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
prefix = L"default:";
else
prefix = NULL;
ap = entry->acl_head;
count = 0;
while (ap != NULL) {
if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
wname = aes_get_wcs(&ap->name);
if (count > 0)
*wp++ = separator;
if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
id = ap->id;
else
id = -1;
append_entry_w(&wp, prefix, ap->tag,
wname, ap->permset, id);
count ++;
}
ap = ap->next;
}
}
return (entry->acl_text_w);
}
static void
append_id_w(wchar_t **wp, int id)
{
if (id > 9)
append_id_w(wp, id / 10);
*(*wp)++ = L"0123456789"[id % 10];
}
static void
append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
const wchar_t *wname, int perm, int id)
{
if (prefix != NULL) {
wcscpy(*wp, prefix);
*wp += wcslen(*wp);
}
switch (tag) {
case ARCHIVE_ENTRY_ACL_USER_OBJ:
wname = NULL;
id = -1;
/* FALL THROUGH */
case ARCHIVE_ENTRY_ACL_USER:
wcscpy(*wp, L"user");
break;
case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
wname = NULL;
id = -1;
/* FALL THROUGH */
case ARCHIVE_ENTRY_ACL_GROUP:
wcscpy(*wp, L"group");
break;
case ARCHIVE_ENTRY_ACL_MASK:
wcscpy(*wp, L"mask");
wname = NULL;
id = -1;
break;
case ARCHIVE_ENTRY_ACL_OTHER:
wcscpy(*wp, L"other");
wname = NULL;
id = -1;
break;
}
*wp += wcslen(*wp);
*(*wp)++ = L':';
if (wname != NULL) {
wcscpy(*wp, wname);
*wp += wcslen(*wp);
}
*(*wp)++ = L':';
*(*wp)++ = (perm & 0444) ? L'r' : L'-';
*(*wp)++ = (perm & 0222) ? L'w' : L'-';
*(*wp)++ = (perm & 0111) ? L'x' : L'-';
if (id != -1) {
*(*wp)++ = L':';
append_id_w(wp, id);
}
**wp = L'\0';
}
/*
* Parse a textual ACL. This automatically recognizes and supports
* extensions described above. The 'type' argument is used to
* indicate the type that should be used for any entries not
* explicitly marked as "default:".
*/
int
__archive_entry_acl_parse_w(struct archive_entry *entry,
const wchar_t *text, int default_type)
{
int type, tag, permset, id;
const wchar_t *start, *end;
const wchar_t *name_start, *name_end;
wchar_t sep;
wchar_t *namebuff;
int namebuff_length;
name_start = name_end = NULL;
namebuff = NULL;
namebuff_length = 0;
while (text != NULL && *text != L'\0') {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
/*
* Solaris extension: "defaultuser::rwx" is the
* default ACL corresponding to "user::rwx", etc.
*/
if (end-start > 7 && wmemcmp(start, L"default", 7) == 0) {
type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
start += 7;
} else
type = default_type;
if (prefix_w(start, end, L"user")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start) {
tag = ARCHIVE_ENTRY_ACL_USER;
name_start = start;
name_end = end;
} else
tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
} else if (prefix_w(start, end, L"group")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start) {
tag = ARCHIVE_ENTRY_ACL_GROUP;
name_start = start;
name_end = end;
} else
tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
} else if (prefix_w(start, end, L"other")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start)
goto fail;
tag = ARCHIVE_ENTRY_ACL_OTHER;
} else if (prefix_w(start, end, L"mask")) {
next_field_w(&text, &start, &end, &sep);
if (sep != L':')
goto fail;
if (end > start)
goto fail;
tag = ARCHIVE_ENTRY_ACL_MASK;
} else
goto fail;
next_field_w(&text, &start, &end, &sep);
permset = 0;
while (start < end) {
switch (*start++) {
case 'r': case 'R':
permset |= ARCHIVE_ENTRY_ACL_READ;
break;
case 'w': case 'W':
permset |= ARCHIVE_ENTRY_ACL_WRITE;
break;
case 'x': case 'X':
permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
break;
case '-':
break;
default:
goto fail;
}
}
/*
* Support star-compatible numeric UID/GID extension.
* This extension adds a ":" followed by the numeric
* ID so that "group:groupname:rwx", for example,
* becomes "group:groupname:rwx:999", where 999 is the
* numeric GID. This extension makes it possible, for
* example, to correctly restore ACLs on a system that
* might have a damaged passwd file or be disconnected
* from a central NIS server. This extension is compatible
* with POSIX.1e draft 17.
*/
if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
tag == ARCHIVE_ENTRY_ACL_GROUP)) {
next_field_w(&text, &start, &end, &sep);
id = 0;
while (start < end && *start >= '0' && *start <= '9') {
if (id > (INT_MAX / 10))
id = INT_MAX;
else {
id *= 10;
id += *start - '0';
start++;
}
}
} else
id = -1; /* No id specified. */
/* Skip any additional entries. */
while (sep == L':') {
next_field_w(&text, &start, &end, &sep);
}
/* Add entry to the internal list. */
if (name_end == name_start) {
archive_entry_acl_add_entry_w(entry, type, permset,
tag, id, NULL);
} else {
if (namebuff_length <= name_end - name_start) {
if (namebuff != NULL)
free(namebuff);
namebuff_length = name_end - name_start + 256;
namebuff =
(wchar_t*)malloc(namebuff_length * sizeof(wchar_t));
if (namebuff == NULL)
goto fail;
}
wmemcpy(namebuff, name_start, name_end - name_start);
namebuff[name_end - name_start] = L'\0';
archive_entry_acl_add_entry_w(entry, type,
permset, tag, id, namebuff);
}
}
if (namebuff != NULL)
free(namebuff);
return (ARCHIVE_OK);
fail:
if (namebuff != NULL)
free(namebuff);
return (ARCHIVE_WARN);
}
/*
* Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]". *wp is updated
* to point to just after the separator. *start points to the first
* character of the matched text and *end just after the last
* character of the matched identifier. In particular *end - *start
* is the length of the field body, not including leading or trailing
* whitespace.
*/
static void
next_field_w(const wchar_t **wp, const wchar_t **start,
const wchar_t **end, wchar_t *sep)
{
/* Skip leading whitespace to find start of field. */
while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
(*wp)++;
}
*start = *wp;
/* Scan for the separator. */
while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
**wp != L'\n') {
(*wp)++;
}
*sep = **wp;
/* Trim trailing whitespace to locate end of field. */
*end = *wp - 1;
while (**end == L' ' || **end == L'\t' || **end == L'\n') {
(*end)--;
}
(*end)++;
/* Adjust scanner location. */
if (**wp != L'\0')
(*wp)++;
}
static int
prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
{
if (start == end)
return (0);
if (*start++ != *test++)
return (0);
while (start < end && *start++ == *test++)
;
if (start < end)
return (0);
return (1);
}
/*
* Following code is modified from UC Berkeley sources, and
* is subject to the following copyright notice.
*/
/*-
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static struct flag {
const char *name;
const wchar_t *wname;
unsigned long set;
unsigned long clear;
} flags[] = {
/* Preferred (shorter) names per flag first, all prefixed by "no" */
#ifdef SF_APPEND
{ "nosappnd", L"nosappnd", SF_APPEND, 0 },
{ "nosappend", L"nosappend", SF_APPEND, 0 },
#endif
#ifdef EXT2_APPEND_FL /* 'a' */
{ "nosappnd", L"nosappnd", EXT2_APPEND_FL, 0 },
{ "nosappend", L"nosappend", EXT2_APPEND_FL, 0 },
#endif
#ifdef SF_ARCHIVED
{ "noarch", L"noarch", SF_ARCHIVED, 0 },
{ "noarchived", L"noarchived", SF_ARCHIVED, 0 },
#endif
#ifdef SF_IMMUTABLE
{ "noschg", L"noschg", SF_IMMUTABLE, 0 },
{ "noschange", L"noschange", SF_IMMUTABLE, 0 },
{ "nosimmutable", L"nosimmutable", SF_IMMUTABLE, 0 },
#endif
#ifdef EXT2_IMMUTABLE_FL /* 'i' */
{ "noschg", L"noschg", EXT2_IMMUTABLE_FL, 0 },
{ "noschange", L"noschange", EXT2_IMMUTABLE_FL, 0 },
{ "nosimmutable", L"nosimmutable", EXT2_IMMUTABLE_FL, 0 },
#endif
#ifdef SF_NOUNLINK
{ "nosunlnk", L"nosunlnk", SF_NOUNLINK, 0 },
{ "nosunlink", L"nosunlink", SF_NOUNLINK, 0 },
#endif
#ifdef SF_SNAPSHOT
{ "nosnapshot", L"nosnapshot", SF_SNAPSHOT, 0 },
#endif
#ifdef UF_APPEND
{ "nouappnd", L"nouappnd", UF_APPEND, 0 },
{ "nouappend", L"nouappend", UF_APPEND, 0 },
#endif
#ifdef UF_IMMUTABLE
{ "nouchg", L"nouchg", UF_IMMUTABLE, 0 },
{ "nouchange", L"nouchange", UF_IMMUTABLE, 0 },
{ "nouimmutable", L"nouimmutable", UF_IMMUTABLE, 0 },
#endif
#ifdef UF_NODUMP
{ "nodump", L"nodump", 0, UF_NODUMP},
#endif
#ifdef EXT2_NODUMP_FL /* 'd' */
{ "nodump", L"nodump", 0, EXT2_NODUMP_FL},
#endif
#ifdef UF_OPAQUE
{ "noopaque", L"noopaque", UF_OPAQUE, 0 },
#endif
#ifdef UF_NOUNLINK
{ "nouunlnk", L"nouunlnk", UF_NOUNLINK, 0 },
{ "nouunlink", L"nouunlink", UF_NOUNLINK, 0 },
#endif
#ifdef EXT2_COMPR_FL /* 'c' */
{ "nocompress", L"nocompress", EXT2_COMPR_FL, 0 },
#endif
#ifdef EXT2_NOATIME_FL /* 'A' */
{ "noatime", L"noatime", 0, EXT2_NOATIME_FL},
#endif
{ NULL, NULL, 0, 0 }
};
/*
* fflagstostr --
* Convert file flags to a comma-separated string. If no flags
* are set, return the empty string.
*/
char *
ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
{
char *string, *dp;
const char *sp;
unsigned long bits;
struct flag *flag;
int length;
bits = bitset | bitclear;
length = 0;
for (flag = flags; flag->name != NULL; flag++)
if (bits & (flag->set | flag->clear)) {
length += strlen(flag->name) + 1;
bits &= ~(flag->set | flag->clear);
}
if (length == 0)
return (NULL);
string = (char*)malloc(length);
if (string == NULL)
return (NULL);
dp = string;
for (flag = flags; flag->name != NULL; flag++) {
if (bitset & flag->set || bitclear & flag->clear) {
sp = flag->name + 2;
} else if (bitset & flag->clear || bitclear & flag->set) {
sp = flag->name;
} else
continue;
bitset &= ~(flag->set | flag->clear);
bitclear &= ~(flag->set | flag->clear);
if (dp > string)
*dp++ = ',';
while ((*dp++ = *sp++) != '\0')
;
dp--;
}
*dp = '\0';
return (string);
}
/*
* wcstofflags --
* Take string of arguments and return file flags. This
* version works a little differently than strtofflags(3).
* In particular, it always tests every token, skipping any
* unrecognized tokens. It returns a pointer to the first
* unrecognized token, or NULL if every token was recognized.
* This version is also const-correct and does not modify the
* provided string.
*/
const wchar_t *
ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
{
const wchar_t *start, *end;
struct flag *flag;
unsigned long set, clear;
const wchar_t *failed;
set = clear = 0;
start = s;
failed = NULL;
/* Find start of first token. */
while (*start == L'\t' || *start == L' ' || *start == L',')
start++;
while (*start != L'\0') {
/* Locate end of token. */
end = start;
while (*end != L'\0' && *end != L'\t' &&
*end != L' ' && *end != L',')
end++;
for (flag = flags; flag->wname != NULL; flag++) {
if (wmemcmp(start, flag->wname, end - start) == 0) {
/* Matched "noXXXX", so reverse the sense. */
clear |= flag->set;
set |= flag->clear;
break;
} else if (wmemcmp(start, flag->wname + 2, end - start)
== 0) {
/* Matched "XXXX", so don't reverse. */
set |= flag->set;
clear |= flag->clear;
break;
}
}
/* Ignore unknown flag names. */
if (flag->wname == NULL && failed == NULL)
failed = start;
/* Find start of next token. */
start = end;
while (*start == L'\t' || *start == L' ' || *start == L',')
start++;
}
if (setp)
*setp = set;
if (clrp)
*clrp = clear;
/* Return location of first failure. */
return (failed);
}
//--- archive_string_sprintf.c
/*
* Like 'vsprintf', but ensures the target is big enough, resizing if
* necessary.
*/
void
__archive_string_vsprintf(struct archive_string *as, const char *fmt,
va_list ap)
{
char long_flag;
intmax_t s; /* Signed integer temp. */
uintmax_t u; /* Unsigned integer temp. */
const char *p, *p2;
__archive_string_ensure(as, 64);
if (fmt == NULL) {
as->s[0] = 0;
return;
}
long_flag = '\0';
for (p = fmt; *p != '\0'; p++) {
const char *saved_p = p;
if (*p != '%') {
archive_strappend_char(as, *p);
continue;
}
p++;
switch(*p) {
case 'j':
long_flag = 'j';
p++;
break;
case 'l':
long_flag = 'l';
p++;
break;
}
switch (*p) {
case '%':
__archive_strappend_char(as, '%');
break;
case 'c':
s = va_arg(ap, int);
__archive_strappend_char(as, s);
break;
case 'd':
switch(long_flag) {
case 'j': s = va_arg(ap, intmax_t); break;
case 'l': s = va_arg(ap, long); break;
default: s = va_arg(ap, int); break;
}
archive_strappend_int(as, s, 10);
break;
case 's':
p2 = va_arg(ap, char *);
archive_strcat(as, p2);
break;
case 'o': case 'u': case 'x': case 'X':
/* Common handling for unsigned integer formats. */
switch(long_flag) {
case 'j': u = va_arg(ap, uintmax_t); break;
case 'l': u = va_arg(ap, unsigned long); break;
default: u = va_arg(ap, unsigned int); break;
}
/* Format it in the correct base. */
switch (*p) {
case 'o': archive_strappend_int(as, u, 8); break;
case 'u': archive_strappend_int(as, u, 10); break;
default: archive_strappend_int(as, u, 16); break;
}
break;
default:
/* Rewind and print the initial '%' literally. */
p = saved_p;
archive_strappend_char(as, *p);
}
}
}
//----------------------------------------------------------------------
class cmCPackTGZ_Data
{
public:
cmCPackTGZ_Data(cmCPackTGZGenerator* gen) :
Name(0), OutputStream(0), Generator(gen) {}
const char *Name;
std::ostream* OutputStream;
cmCPackTGZGenerator* Generator;
};
//----------------------------------------------------------------------
int cmCPackTGZGenerator::TGZ_Open(struct archive *a, void *client_data)
{
cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
(void)a;
mydata->OutputStream = new cmGeneratedFileStream(mydata->Name);
if ( *mydata->OutputStream &&
mydata->Generator->GenerateHeader(mydata->OutputStream))
{
return (ARCHIVE_OK);
}
else
{
return (ARCHIVE_FATAL);
}
}
//----------------------------------------------------------------------
ssize_t cmCPackTGZGenerator::TGZ_Write(struct archive *a, void *client_data, void *buff, size_t n)
{
cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
(void)a;
mydata->OutputStream->write(reinterpret_cast<const char*>(buff), n);
if ( !*mydata->OutputStream )
{
return 0;
}
return n;
}
//----------------------------------------------------------------------
int cmCPackTGZGenerator::TGZ_Close(struct archive *a, void *client_data)
{
cmCPackTGZ_Data *mydata = (cmCPackTGZ_Data*)client_data;
(void)a;
delete mydata->OutputStream;
return (0);
}
//----------------------------------------------------------------------
int cmCPackTGZGenerator::CompressFiles(const char* outFileName, const char* toplevel,
const std::vector<std::string>& files)
{
std::cout << "Toplevel: " << toplevel << std::endl;
cmCPackTGZ_Data mydata(this);
struct archive *a;
struct archive_entry *entry;
struct stat st;
char buff[8192];
int len;
int fd;
a = archive_write_new();
mydata.Name = outFileName;
archive_write_set_compression_gzip(a);
archive_write_set_format_ustar(a);
archive_write_open(a, &mydata, cmCPackTGZGenerator::TGZ_Open,
cmCPackTGZGenerator::TGZ_Write, cmCPackTGZGenerator::TGZ_Close);
std::vector<std::string>::const_iterator fileIt;
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
{
std::string fname = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
const char* filename = fileIt->c_str();
stat(filename, &st);
entry = archive_entry_new();
archive_entry_copy_stat(entry, &st);
archive_entry_set_pathname(entry, fname.c_str());
archive_write_header(a, entry);
fd = open(filename, O_RDONLY);
len = read(fd, buff, sizeof(buff));
while ( len > 0 )
{
archive_write_data(a, buff, len);
len = read(fd, buff, sizeof(buff));
}
archive_entry_free(entry);
}
archive_write_finish(a);
return 1;
}
//--- archive_write_set_format_ustar.c
struct ustar {
uint64_t entry_bytes_remaining;
uint64_t entry_padding;
char written;
};
/*
* Define structure of POSIX 'ustar' tar header.
*/
struct archive_entry_header_ustar {
char name[100];
char mode[6];
char mode_padding[2];
char uid[6];
char uid_padding[2];
char gid[6];
char gid_padding[2];
char size[11];
char size_padding[1];
char mtime[11];
char mtime_padding[1];
char checksum[8];
char typeflag[1];
char linkname[100];
char magic[6]; /* For POSIX: "ustar\0" */
char version[2]; /* For POSIX: "00" */
char uname[32];
char gname[32];
char rdevmajor[6];
char rdevmajor_padding[2];
char rdevminor[6];
char rdevminor_padding[2];
char prefix[155];
char padding[12];
};
/*
* A filled-in copy of the header for initialization.
*/
static const struct archive_entry_header_ustar template_header = {
{ "" }, /* name */
{ '0','0','0','0','0','0' }, { ' ', '\0' }, /* mode, space-null termination. */
{ '0','0','0','0','0','0' }, { ' ', '\0' }, /* uid, space-null termination. */
{ '0','0','0','0','0','0' }, { ' ', '\0' }, /* gid, space-null termination. */
{ '0','0','0','0','0','0','0','0','0','0','0' }, { ' ' }, /* size, space termination. */
{ '0','0','0','0','0','0','0','0','0','0','0' }, { ' ' }, /* mtime, space termination. */
{ ' ',' ',' ',' ',' ',' ',' ',' ' }, /* Initial checksum value. */
{ '0' }, /* default: regular file */
{ "" }, /* linkname */
{ 'u','s','t','a','r' }, /* magic */
{ '0', '0' }, /* version */
{ "" }, /* uname */
{ "" }, /* gname */
{'0','0','0','0','0','0'}, { ' ', '\0' }, /* rdevmajor, space-null termination */
{'0','0','0','0','0','0'}, { ' ', '\0' }, /* rdevminor, space-null termination */
{ "" }, /* prefix */
{ "" } /* padding */
};
static int archive_write_ustar_data(struct archive *a, const void *buff,
size_t s);
static int archive_write_ustar_finish(struct archive *);
static int archive_write_ustar_finish_entry(struct archive *);
static int archive_write_ustar_header(struct archive *,
struct archive_entry *entry);
static int format_256(int64_t, char *, int);
static int format_number(int64_t, char *, int size, int max, int strict);
static int format_octal(int64_t, char *, int);
static int write_nulls(struct archive *a, size_t);
/*
* Set output format to 'ustar' format.
*/
int
archive_write_set_format_ustar(struct archive *a)
{
struct ustar *ustar;
/* If someone else was already registered, unregister them. */
if (a->format_finish != NULL)
(a->format_finish)(a);
ustar = (struct ustar*)malloc(sizeof(*ustar));
if (ustar == NULL) {
archive_set_error(a, ENOMEM, "Can't allocate ustar data");
return (ARCHIVE_FATAL);
}
memset(ustar, 0, sizeof(*ustar));
a->format_data = ustar;
a->pad_uncompressed = 1; /* Mimic gtar in this respect. */
a->format_write_header = archive_write_ustar_header;
a->format_write_data = archive_write_ustar_data;
a->format_finish = archive_write_ustar_finish;
a->format_finish_entry = archive_write_ustar_finish_entry;
a->archive_format = ARCHIVE_FORMAT_TAR_USTAR;
a->archive_format_name = "POSIX ustar";
return (ARCHIVE_OK);
}
static int
archive_write_ustar_header(struct archive *a, struct archive_entry *entry)
{
char buff[512];
int ret;
struct ustar *ustar;
ustar = (struct ustar*)a->format_data;
ustar->written = 1;
/* Only regular files (not hardlinks) have data. */
if (archive_entry_hardlink(entry) != NULL ||
archive_entry_symlink(entry) != NULL ||
!S_ISREG(archive_entry_mode(entry)))
archive_entry_set_size(entry, 0);
ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1);
if (ret != ARCHIVE_OK)
return (ret);
ret = (a->compression_write)(a, buff, 512);
if (ret != ARCHIVE_OK)
return (ret);
ustar->entry_bytes_remaining = archive_entry_size(entry);
ustar->entry_padding = 0x1ff & (- ustar->entry_bytes_remaining);
return (ARCHIVE_OK);
}
/*
* Format a basic 512-byte "ustar" header.
*
* Returns -1 if format failed (due to field overflow).
* Note that this always formats as much of the header as possible.
* If "strict" is set to zero, it will extend numeric fields as
* necessary (overwriting terminators or using base-256 extensions).
*
* This is exported so that other 'tar' formats can use it.
*/
int
__archive_write_format_header_ustar(struct archive *a, char buff[512],
struct archive_entry *entry, int tartype, int strict)
{
unsigned int checksum;
struct archive_entry_header_ustar *h;
int i, ret;
size_t copy_length;
const char *p, *pp;
const struct stat *st;
int mytartype;
ret = 0;
mytartype = -1;
/*
* The "template header" already includes the "ustar"
* signature, various end-of-field markers and other required
* elements.
*/
memcpy(buff, &template_header, 512);
h = (struct archive_entry_header_ustar *)buff;
/*
* Because the block is already null-filled, and strings
* are allowed to exactly fill their destination (without null),
* I use memcpy(dest, src, strlen()) here a lot to copy strings.
*/
pp = archive_entry_pathname(entry);
if (strlen(pp) <= sizeof(h->name))
memcpy(h->name, pp, strlen(pp));
else {
/* Store in two pieces, splitting at a '/'. */
p = strchr(pp + strlen(pp) - sizeof(h->name) - 1, '/');
/*
* If there is no path separator, or the prefix or
* remaining name are too large, return an error.
*/
if (!p) {
archive_set_error(a, ENAMETOOLONG,
"Pathname too long");
ret = ARCHIVE_WARN;
} else if (p > pp + sizeof(h->prefix)) {
archive_set_error(a, ENAMETOOLONG,
"Pathname too long");
ret = ARCHIVE_WARN;
} else {
/* Copy prefix and remainder to appropriate places */
memcpy(h->prefix, pp, p - pp);
memcpy(h->name, p + 1, pp + strlen(pp) - p - 1);
}
}
p = archive_entry_hardlink(entry);
if (p != NULL)
mytartype = '1';
else
p = archive_entry_symlink(entry);
if (p != NULL && p[0] != '\0') {
copy_length = strlen(p);
if (copy_length > sizeof(h->linkname)) {
archive_set_error(a, ENAMETOOLONG,
"Link contents too long");
ret = ARCHIVE_WARN;
copy_length = sizeof(h->linkname);
}
memcpy(h->linkname, p, copy_length);
}
p = archive_entry_uname(entry);
if (p != NULL && p[0] != '\0') {
copy_length = strlen(p);
if (copy_length > sizeof(h->uname)) {
archive_set_error(a, ARCHIVE_ERRNO_MISC,
"Username too long");
ret = ARCHIVE_WARN;
copy_length = sizeof(h->uname);
}
memcpy(h->uname, p, copy_length);
}
p = archive_entry_gname(entry);
if (p != NULL && p[0] != '\0') {
copy_length = strlen(p);
if (strlen(p) > sizeof(h->gname)) {
archive_set_error(a, ARCHIVE_ERRNO_MISC,
"Group name too long");
ret = ARCHIVE_WARN;
copy_length = sizeof(h->gname);
}
memcpy(h->gname, p, copy_length);
}
st = archive_entry_stat(entry);
if (format_number(st->st_mode & 07777, h->mode, sizeof(h->mode), 8, strict)) {
archive_set_error(a, ERANGE, "Numeric mode too large");
ret = ARCHIVE_WARN;
}
if (format_number(st->st_uid, h->uid, sizeof(h->uid), 8, strict)) {
archive_set_error(a, ERANGE, "Numeric user ID too large");
ret = ARCHIVE_WARN;
}
if (format_number(st->st_gid, h->gid, sizeof(h->gid), 8, strict)) {
archive_set_error(a, ERANGE, "Numeric group ID too large");
ret = ARCHIVE_WARN;
}
if (format_number(st->st_size, h->size, sizeof(h->size), 12, strict)) {
archive_set_error(a, ERANGE, "File size out of range");
ret = ARCHIVE_WARN;
}
if (format_number(st->st_mtime, h->mtime, sizeof(h->mtime), 12, strict)) {
archive_set_error(a, ERANGE,
"File modification time too large");
ret = ARCHIVE_WARN;
}
#if defined(S_ISBLK)
if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode)) {
if (format_number(major(st->st_rdev), h->rdevmajor,
sizeof(h->rdevmajor), 8, strict)) {
archive_set_error(a, ERANGE,
"Major device number too large");
ret = ARCHIVE_WARN;
}
if (format_number(minor(st->st_rdev), h->rdevminor,
sizeof(h->rdevminor), 8, strict)) {
archive_set_error(a, ERANGE,
"Minor device number too large");
ret = ARCHIVE_WARN;
}
}
#endif
if (tartype >= 0) {
h->typeflag[0] = tartype;
} else if (mytartype >= 0) {
h->typeflag[0] = mytartype;
} else {
switch (st->st_mode & S_IFMT) {
case S_IFREG: h->typeflag[0] = '0' ; break;
#if defined(S_IFLNK)
case S_IFLNK: h->typeflag[0] = '2' ; break;
#endif
case S_IFCHR: h->typeflag[0] = '3' ; break;
#if defined(S_IFBLK)
case S_IFBLK: h->typeflag[0] = '4' ; break;
#endif
case S_IFDIR: h->typeflag[0] = '5' ; break;
#if defined(S_IFIFO)
case S_IFIFO: h->typeflag[0] = '6' ; break;
#endif
#if defined(S_IFSOCK)
case S_IFSOCK:
archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
"tar format cannot archive socket");
ret = ARCHIVE_WARN;
break;
#endif
default:
archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
"tar format cannot archive this (mode=0%lo)",
(unsigned long)st->st_mode);
ret = ARCHIVE_WARN;
}
}
checksum = 0;
for (i = 0; i < 512; i++)
checksum += 255 & (unsigned int)buff[i];
h->checksum[6] = '\0'; /* Can't be pre-set in the template. */
/* h->checksum[7] = ' '; */ /* This is pre-set in the template. */
format_octal(checksum, h->checksum, 6);
return (ret);
}
/*
* Format a number into a field, with some intelligence.
*/
static int
format_number(int64_t v, char *p, int s, int maxsize, int strict)
{
int64_t limit;
limit = ((int64_t)1 << (s*3));
/* "Strict" only permits octal values with proper termination. */
if (strict)
return (format_octal(v, p, s));
/*
* In non-strict mode, we allow the number to overwrite one or
* more bytes of the field termination. Even old tar
* implementations should be able to handle this with no
* problem.
*/
if (v >= 0) {
while (s <= maxsize) {
if (v < limit)
return (format_octal(v, p, s));
s++;
limit <<= 3;
}
}
/* Base-256 can handle any number, positive or negative. */
return (format_256(v, p, maxsize));
}
/*
* Format a number into the specified field using base-256.
*/
static int
format_256(int64_t v, char *p, int s)
{
p += s;
while (s-- > 0) {
*--p = (char)(v & 0xff);
v >>= 8;
}
*p |= 0x80; /* Set the base-256 marker bit. */
return (0);
}
/*
* Format a number into the specified field.
*/
static int
format_octal(int64_t v, char *p, int s)
{
int len;
len = s;
/* Octal values can't be negative, so use 0. */
if (v < 0) {
while (len-- > 0)
*p++ = '0';
return (-1);
}
p += s; /* Start at the end and work backwards. */
while (s-- > 0) {
*--p = '0' + (v & 7);
v >>= 3;
}
if (v == 0)
return (0);
/* If it overflowed, fill field with max value. */
while (len-- > 0)
*p++ = '7';
return (-1);
}
static int
archive_write_ustar_finish(struct archive *a)
{
struct ustar *ustar;
int r;
r = ARCHIVE_OK;
ustar = (struct ustar*)a->format_data;
/*
* Suppress end-of-archive if nothing else was ever written.
* This fixes a problem where setting one format, then another
* ends up writing a gratuitous end-of-archive marker.
*/
if (ustar->written && a->compression_write != NULL)
r = write_nulls(a, 512*2);
free(ustar);
a->format_data = NULL;
return (r);
}
static int
archive_write_ustar_finish_entry(struct archive *a)
{
struct ustar *ustar;
int ret;
ustar = (struct ustar*) a->format_data;
ret = write_nulls(a,
ustar->entry_bytes_remaining + ustar->entry_padding);
ustar->entry_bytes_remaining = ustar->entry_padding = 0;
return (ret);
}
static int
write_nulls(struct archive *a, size_t padding)
{
int ret, to_write;
while (padding > 0) {
to_write = padding < a->null_length ? padding : a->null_length;
ret = (a->compression_write)(a, a->nulls, to_write);
if (ret != ARCHIVE_OK)
return (ret);
padding -= to_write;
}
return (ARCHIVE_OK);
}
static int
archive_write_ustar_data(struct archive *a, const void *buff, size_t s)
{
struct ustar *ustar;
int ret;
ustar = (struct ustar*)a->format_data;
if (s > ustar->entry_bytes_remaining)
s = ustar->entry_bytes_remaining;
ret = (a->compression_write)(a, buff, s);
ustar->entry_bytes_remaining -= s;
return (ret);
}
//--- archive_write_set_compression_gzip.c
struct private_data {
z_stream stream;
int64_t total_in;
unsigned char *compressed;
size_t compressed_buffer_size;
unsigned long crc;
};
/*
* Yuck. zlib.h is not const-correct, so I need this one bit
* of ugly hackery to convert a const * pointer to a non-const pointer.
*/
#define SET_NEXT_IN(st,src) \
(st)->stream.next_in = (Bytef*)(void *)(uintptr_t)(const void *)(src)
static int archive_compressor_gzip_finish(struct archive *);
static int archive_compressor_gzip_init(struct archive *);
static int archive_compressor_gzip_write(struct archive *, const void *,
size_t);
static int drive_compressor(struct archive *, struct private_data *,
int finishing);
/*
* Allocate, initialize and return a archive object.
*/
int
archive_write_set_compression_gzip(struct archive *a)
{
__archive_check_magic(a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, "archive_write_set_compression_gzip");
a->compression_init = &archive_compressor_gzip_init;
a->compression_code = ARCHIVE_COMPRESSION_GZIP;
a->compression_name = "gzip";
return (ARCHIVE_OK);
}
/*
* Setup callback.
*/
static int
archive_compressor_gzip_init(struct archive *a)
{
int ret;
struct private_data *state;
time_t t;
a->compression_code = ARCHIVE_COMPRESSION_GZIP;
a->compression_name = "gzip";
if (a->client_opener != NULL) {
ret = (a->client_opener)(a, a->client_data);
if (ret != ARCHIVE_OK)
return (ret);
}
state = (struct private_data *)malloc(sizeof(*state));
if (state == NULL) {
archive_set_error(a, ENOMEM,
"Can't allocate data for compression");
return (ARCHIVE_FATAL);
}
memset(state, 0, sizeof(*state));
state->compressed_buffer_size = a->bytes_per_block;
state->compressed = (unsigned char*)malloc(state->compressed_buffer_size);
state->crc = crc32(0L, NULL, 0);
if (state->compressed == NULL) {
archive_set_error(a, ENOMEM,
"Can't allocate data for compression buffer");
free(state);
return (ARCHIVE_FATAL);
}
state->stream.next_out = state->compressed;
state->stream.avail_out = state->compressed_buffer_size;
/* Prime output buffer with a gzip header. */
t = time(NULL);
state->compressed[0] = 0x1f; /* GZip signature bytes */
state->compressed[1] = 0x8b;
state->compressed[2] = 0x08; /* "Deflate" compression */
state->compressed[3] = 0; /* No options */
state->compressed[4] = (t)&0xff; /* Timestamp */
state->compressed[5] = (t>>8)&0xff;
state->compressed[6] = (t>>16)&0xff;
state->compressed[7] = (t>>24)&0xff;
state->compressed[8] = 0; /* No deflate options */
state->compressed[9] = 3; /* OS=Unix */
state->stream.next_out += 10;
state->stream.avail_out -= 10;
a->compression_write = archive_compressor_gzip_write;
a->compression_finish = archive_compressor_gzip_finish;
/* Initialize compression library. */
ret = deflateInit2(&(state->stream),
Z_DEFAULT_COMPRESSION,
Z_DEFLATED,
-15 /* < 0 to suppress zlib header */,
8,
Z_DEFAULT_STRATEGY);
if (ret == Z_OK) {
a->compression_data = state;
return (0);
}
/* Library setup failed: clean up. */
archive_set_error(a, ARCHIVE_ERRNO_MISC, "Internal error "
"initializing compression library");
free(state->compressed);
free(state);
/* Override the error message if we know what really went wrong. */
switch (ret) {
case Z_STREAM_ERROR:
archive_set_error(a, ARCHIVE_ERRNO_MISC,
"Internal error initializing "
"compression library: invalid setup parameter");
break;
case Z_MEM_ERROR:
archive_set_error(a, ENOMEM, "Internal error initializing "
"compression library");
break;
case Z_VERSION_ERROR:
archive_set_error(a, ARCHIVE_ERRNO_MISC,
"Internal error initializing "
"compression library: invalid library version");
break;
}
return (ARCHIVE_FATAL);
}
/*
* Write data to the compressed stream.
*/
static int
archive_compressor_gzip_write(struct archive *a, const void *buff,
size_t length)
{
struct private_data *state;
int ret;
state = (struct private_data*)a->compression_data;
if (a->client_writer == NULL) {
archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
"No write callback is registered? "
"This is probably an internal programming error.");
return (ARCHIVE_FATAL);
}
/* Update statistics */
state->crc = crc32(state->crc, (const Bytef*)buff, length);
state->total_in += length;
/* Compress input data to output buffer */
SET_NEXT_IN(state, buff);
state->stream.avail_in = length;
if ((ret = drive_compressor(a, state, 0)) != ARCHIVE_OK)
return (ret);
a->file_position += length;
return (ARCHIVE_OK);
}
/*
* Finish the compression...
*/
static int
archive_compressor_gzip_finish(struct archive *a)
{
ssize_t block_length, target_block_length, bytes_written;
int ret;
struct private_data *state;
unsigned tocopy;
unsigned char trailer[8];
state = (struct private_data*)a->compression_data;
ret = 0;
if (a->client_writer == NULL) {
archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
"No write callback is registered? "
"This is probably an internal programming error.");
ret = ARCHIVE_FATAL;
goto cleanup;
}
/* By default, always pad the uncompressed data. */
if (a->pad_uncompressed) {
tocopy = a->bytes_per_block -
(state->total_in % a->bytes_per_block);
while (tocopy > 0 && tocopy < (unsigned)a->bytes_per_block) {
SET_NEXT_IN(state, a->nulls);
state->stream.avail_in = tocopy < a->null_length ?
tocopy : a->null_length;
state->crc = crc32(state->crc, a->nulls,
state->stream.avail_in);
state->total_in += state->stream.avail_in;
tocopy -= state->stream.avail_in;
ret = drive_compressor(a, state, 0);
if (ret != ARCHIVE_OK)
goto cleanup;
}
}
/* Finish compression cycle */
if (((ret = drive_compressor(a, state, 1))) != ARCHIVE_OK)
goto cleanup;
/* Build trailer: 4-byte CRC and 4-byte length. */
trailer[0] = (state->crc)&0xff;
trailer[1] = (state->crc >> 8)&0xff;
trailer[2] = (state->crc >> 16)&0xff;
trailer[3] = (state->crc >> 24)&0xff;
trailer[4] = (state->total_in)&0xff;
trailer[5] = (state->total_in >> 8)&0xff;
trailer[6] = (state->total_in >> 16)&0xff;
trailer[7] = (state->total_in >> 24)&0xff;
/* Add trailer to current block. */
tocopy = 8;
if (tocopy > state->stream.avail_out)
tocopy = state->stream.avail_out;
memcpy(state->stream.next_out, trailer, tocopy);
state->stream.next_out += tocopy;
state->stream.avail_out -= tocopy;
/* If it overflowed, flush and start a new block. */
if (tocopy < 8) {
bytes_written = (a->client_writer)(a, a->client_data,
state->compressed, state->compressed_buffer_size);
if (bytes_written <= 0) {
ret = ARCHIVE_FATAL;
goto cleanup;
}
a->raw_position += bytes_written;
state->stream.next_out = state->compressed;
state->stream.avail_out = state->compressed_buffer_size;
memcpy(state->stream.next_out, trailer + tocopy, 8-tocopy);
state->stream.next_out += 8-tocopy;
state->stream.avail_out -= 8-tocopy;
}
/* Optionally, pad the final compressed block. */
block_length = state->stream.next_out - state->compressed;
/* Tricky calculation to determine size of last block. */
target_block_length = block_length;
if (a->bytes_in_last_block <= 0)
/* Default or Zero: pad to full block */
target_block_length = a->bytes_per_block;
else
/* Round length to next multiple of bytes_in_last_block. */
target_block_length = a->bytes_in_last_block *
( (block_length + a->bytes_in_last_block - 1) /
a->bytes_in_last_block);
if (target_block_length > a->bytes_per_block)
target_block_length = a->bytes_per_block;
if (block_length < target_block_length) {
memset(state->stream.next_out, 0,
target_block_length - block_length);
block_length = target_block_length;
}
/* Write the last block */
bytes_written = (a->client_writer)(a, a->client_data,
state->compressed, block_length);
if (bytes_written <= 0) {
ret = ARCHIVE_FATAL;
goto cleanup;
}
a->raw_position += bytes_written;
/* Cleanup: shut down compressor, release memory, etc. */
cleanup:
switch (deflateEnd(&(state->stream))) {
case Z_OK:
break;
default:
archive_set_error(a, ARCHIVE_ERRNO_MISC,
"Failed to clean up compressor");
ret = ARCHIVE_FATAL;
}
free(state->compressed);
free(state);
/* Close the output */
if (a->client_closer != NULL)
(a->client_closer)(a, a->client_data);
return (ret);
}
/*
* Utility function to push input data through compressor,
* writing full output blocks as necessary.
*
* Note that this handles both the regular write case (finishing ==
* false) and the end-of-archive case (finishing == true).
*/
static int
drive_compressor(struct archive *a, struct private_data *state, int finishing)
{
ssize_t bytes_written;
int ret;
for (;;) {
if (state->stream.avail_out == 0) {
bytes_written = (a->client_writer)(a, a->client_data,
state->compressed, state->compressed_buffer_size);
if (bytes_written <= 0) {
/* TODO: Handle this write failure */
return (ARCHIVE_FATAL);
} else if ((size_t)bytes_written < state->compressed_buffer_size) {
/* Short write: Move remaining to
* front of block and keep filling */
memmove(state->compressed,
state->compressed + bytes_written,
state->compressed_buffer_size - bytes_written);
}
a->raw_position += bytes_written;
state->stream.next_out
= state->compressed +
state->compressed_buffer_size - bytes_written;
state->stream.avail_out = bytes_written;
}
ret = deflate(&(state->stream),
finishing ? Z_FINISH : Z_NO_FLUSH );
switch (ret) {
case Z_OK:
/* In non-finishing case, check if compressor
* consumed everything */
if (!finishing && state->stream.avail_in == 0)
return (ARCHIVE_OK);
/* In finishing case, this return always means
* there's more work */
break;
case Z_STREAM_END:
/* This return can only occur in finishing case. */
return (ARCHIVE_OK);
default:
/* Any other return value indicates an error. */
archive_set_error(a, ARCHIVE_ERRNO_MISC,
"GZip compression failed");
return (ARCHIVE_FATAL);
}
}
}
|