1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
|
/*
zipint.h -- internal declarations.
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <libzip@nih.at>
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.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
*/
#include <zlib.h>
/*
#ifdef _MSC_VER
#define ZIP_EXTERN __declspec(dllimport)
#endif
*/
/*
zip.h -- exported declarations.
Copyright (C) 1999-2008 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <libzip@nih.at>
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.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
*/
#ifndef ZIP_EXTERN
#define ZIP_EXTERN
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/types.h>
#include <stdio.h>
#include <time.h>
/* flags for zip_open */
#define ZIP_CREATE 1
#define ZIP_EXCL 2
#define ZIP_CHECKCONS 4
/* flags for zip_name_locate, zip_fopen, zip_stat, ... */
#define ZIP_FL_NOCASE 1 /* ignore case on name lookup */
#define ZIP_FL_NODIR 2 /* ignore directory component */
#define ZIP_FL_COMPRESSED 4 /* read compressed data */
#define ZIP_FL_UNCHANGED 8 /* use original data, ignoring changes */
#define ZIP_FL_RECOMPRESS 16 /* force recompression of data */
/* archive global flags flags */
#define ZIP_AFL_TORRENT 1 /* torrent zipped */
/* libzip error codes */
#define ZIP_ER_OK 0 /* N No error */
#define ZIP_ER_MULTIDISK 1 /* N Multi-disk zip archives not supported */
#define ZIP_ER_RENAME 2 /* S Renaming temporary file failed */
#define ZIP_ER_CLOSE 3 /* S Closing zip archive failed */
#define ZIP_ER_SEEK 4 /* S Seek error */
#define ZIP_ER_READ 5 /* S Read error */
#define ZIP_ER_WRITE 6 /* S Write error */
#define ZIP_ER_CRC 7 /* N CRC error */
#define ZIP_ER_ZIPCLOSED 8 /* N Containing zip archive was closed */
#define ZIP_ER_NOENT 9 /* N No such file */
#define ZIP_ER_EXISTS 10 /* N File already exists */
#define ZIP_ER_OPEN 11 /* S Can't open file */
#define ZIP_ER_TMPOPEN 12 /* S Failure to create temporary file */
#define ZIP_ER_ZLIB 13 /* Z Zlib error */
#define ZIP_ER_MEMORY 14 /* N Malloc failure */
#define ZIP_ER_CHANGED 15 /* N Entry has been changed */
#define ZIP_ER_COMPNOTSUPP 16 /* N Compression method not supported */
#define ZIP_ER_EOF 17 /* N Premature EOF */
#define ZIP_ER_INVAL 18 /* N Invalid argument */
#define ZIP_ER_NOZIP 19 /* N Not a zip archive */
#define ZIP_ER_INTERNAL 20 /* N Internal error */
#define ZIP_ER_INCONS 21 /* N Zip archive inconsistent */
#define ZIP_ER_REMOVE 22 /* S Can't remove file */
#define ZIP_ER_DELETED 23 /* N Entry has been deleted */
/* type of system error value */
#define ZIP_ET_NONE 0 /* sys_err unused */
#define ZIP_ET_SYS 1 /* sys_err is errno */
#define ZIP_ET_ZLIB 2 /* sys_err is zlib error code */
/* compression methods */
#define ZIP_CM_DEFAULT -1 /* better of deflate or store */
#define ZIP_CM_STORE 0 /* stored (uncompressed) */
#define ZIP_CM_SHRINK 1 /* shrunk */
#define ZIP_CM_REDUCE_1 2 /* reduced with factor 1 */
#define ZIP_CM_REDUCE_2 3 /* reduced with factor 2 */
#define ZIP_CM_REDUCE_3 4 /* reduced with factor 3 */
#define ZIP_CM_REDUCE_4 5 /* reduced with factor 4 */
#define ZIP_CM_IMPLODE 6 /* imploded */
/* 7 - Reserved for Tokenizing compression algorithm */
#define ZIP_CM_DEFLATE 8 /* deflated */
#define ZIP_CM_DEFLATE64 9 /* deflate64 */
#define ZIP_CM_PKWARE_IMPLODE 10 /* PKWARE imploding */
/* 11 - Reserved by PKWARE */
#define ZIP_CM_BZIP2 12 /* compressed using BZIP2 algorithm */
/* 13 - Reserved by PKWARE */
#define ZIP_CM_LZMA 14 /* LZMA (EFS) */
/* 15-17 - Reserved by PKWARE */
#define ZIP_CM_TERSE 18 /* compressed using IBM TERSE (new) */
#define ZIP_CM_LZ77 19 /* IBM LZ77 z Architecture (PFS) */
#define ZIP_CM_WAVPACK 97 /* WavPack compressed data */
#define ZIP_CM_PPMD 98 /* PPMd version I, Rev 1 */
/* encryption methods */
#define ZIP_EM_NONE 0 /* not encrypted */
#define ZIP_EM_TRAD_PKWARE 1 /* traditional PKWARE encryption */
#if 0 /* Strong Encryption Header not parsed yet */
#define ZIP_EM_DES 0x6601 /* strong encryption: DES */
#define ZIP_EM_RC2_OLD 0x6602 /* strong encryption: RC2, version < 5.2 */
#define ZIP_EM_3DES_168 0x6603
#define ZIP_EM_3DES_112 0x6609
#define ZIP_EM_AES_128 0x660e
#define ZIP_EM_AES_192 0x660f
#define ZIP_EM_AES_256 0x6610
#define ZIP_EM_RC2 0x6702 /* strong encryption: RC2, version >= 5.2 */
#define ZIP_EM_RC4 0x6801
#endif
#define ZIP_EM_UNKNOWN 0xffff /* unknown algorithm */
typedef long myoff_t; /* XXX: 64 bit support */
enum zip_source_cmd {
ZIP_SOURCE_OPEN, /* prepare for reading */
ZIP_SOURCE_READ, /* read data */
ZIP_SOURCE_CLOSE, /* reading is done */
ZIP_SOURCE_STAT, /* get meta information */
ZIP_SOURCE_ERROR, /* get error information */
ZIP_SOURCE_FREE /* cleanup and free resources */
};
typedef ssize_t (*zip_source_callback)(void *state, void *data,
size_t len, enum zip_source_cmd cmd);
struct zip_stat {
const char *name; /* name of the file */
int index; /* index within archive */
unsigned int crc; /* crc of file data */
time_t mtime; /* modification time */
myoff_t size; /* size of file (uncompressed) */
myoff_t comp_size; /* size of file (compressed) */
unsigned short comp_method; /* compression method used */
unsigned short encryption_method; /* encryption method used */
};
struct zip;
struct zip_file;
struct zip_source;
ZIP_EXTERN int zip_add(struct zip *, const char *, struct zip_source *);
ZIP_EXTERN int zip_add_dir(struct zip *, const char *);
ZIP_EXTERN int zip_close(struct zip *);
ZIP_EXTERN int zip_delete(struct zip *, int);
ZIP_EXTERN void zip_error_clear(struct zip *);
ZIP_EXTERN void zip_error_get(struct zip *, int *, int *);
ZIP_EXTERN int zip_error_get_sys_type(int);
ZIP_EXTERN int zip_error_to_str(char *, size_t, int, int);
ZIP_EXTERN int zip_fclose(struct zip_file *);
ZIP_EXTERN void zip_file_error_clear(struct zip_file *);
ZIP_EXTERN void zip_file_error_get(struct zip_file *, int *, int *);
ZIP_EXTERN const char *zip_file_strerror(struct zip_file *);
ZIP_EXTERN struct zip_file *zip_fopen(struct zip *, const char *, int);
ZIP_EXTERN struct zip_file *zip_fopen_index(struct zip *, int, int);
ZIP_EXTERN ssize_t zip_fread(struct zip_file *, void *, size_t);
ZIP_EXTERN const char *zip_get_archive_comment(struct zip *, int *, int);
ZIP_EXTERN int zip_get_archive_flag(struct zip *, int, int);
ZIP_EXTERN const char *zip_get_file_comment(struct zip *, int, int *, int);
ZIP_EXTERN const char *zip_get_name(struct zip *, int, int);
ZIP_EXTERN int zip_get_num_files(struct zip *);
ZIP_EXTERN int zip_name_locate(struct zip *, const char *, int);
ZIP_EXTERN struct zip *zip_open(const char *, int, int *);
ZIP_EXTERN int zip_rename(struct zip *, int, const char *);
ZIP_EXTERN int zip_replace(struct zip *, int, struct zip_source *);
ZIP_EXTERN int zip_set_archive_comment(struct zip *, const char *, int);
ZIP_EXTERN int zip_set_archive_flag(struct zip *, int, int);
ZIP_EXTERN int zip_set_file_comment(struct zip *, int, const char *, int);
ZIP_EXTERN struct zip_source *zip_source_buffer(struct zip *, const void *,
myoff_t, int);
ZIP_EXTERN struct zip_source *zip_source_file(struct zip *, const char *,
myoff_t, myoff_t);
ZIP_EXTERN struct zip_source *zip_source_filep(struct zip *, FILE *,
myoff_t, myoff_t);
ZIP_EXTERN void zip_source_free(struct zip_source *);
ZIP_EXTERN struct zip_source *zip_source_function(struct zip *,
zip_source_callback, void *);
ZIP_EXTERN struct zip_source *zip_source_zip(struct zip *, struct zip *,
int, int, myoff_t, myoff_t);
ZIP_EXTERN int zip_stat(struct zip *, const char *, int, struct zip_stat *);
ZIP_EXTERN int zip_stat_index(struct zip *, int, int, struct zip_stat *);
ZIP_EXTERN void zip_stat_init(struct zip_stat *);
ZIP_EXTERN const char *zip_strerror(struct zip *);
ZIP_EXTERN int zip_unchange(struct zip *, int);
ZIP_EXTERN int zip_unchange_all(struct zip *);
ZIP_EXTERN int zip_unchange_archive(struct zip *);
#ifdef __cplusplus
}
#endif
/* config.h. Generated from config.h.in by configure. */
/* config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if you have the declaration of `tzname', and to 0 if you don't.
*/
/* #undef HAVE_DECL_TZNAME */
#define HAVE_CONFIG_H 1
/* Define to 1 if you have the <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
/* Define to 1 if you have the `fseeko' function. */
#define HAVE_FSEEKO 1
/* Define to 1 if you have the `ftello' function. */
#define HAVE_FTELLO 1
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
/* Define to 1 if you have the `z' library (-lz). */
#define HAVE_LIBZ 1
/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1
/* Define to 1 if you have the `mkstemp' function. */
#define HAVE_MKSTEMP 1
/* Define to 1 if you have the `MoveFileExA' function. */
/* #undef HAVE_MOVEFILEEXA */
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* Define to 1 if `tm_zone' is member of `struct tm'. */
#define HAVE_STRUCT_TM_TM_ZONE 1
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
/* Define to 1 if your `struct tm' has `tm_zone'. Deprecated, use
`HAVE_STRUCT_TM_TM_ZONE' instead. */
#define HAVE_TM_ZONE 1
/* Define to 1 if you don't have `tm_zone' but do have the external array
`tzname'. */
/* #undef HAVE_TZNAME */
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
/* Define to 1 if your C compiler doesn't accept -c and -o together. */
/* #undef NO_MINUS_C_MINUS_O */
/* Name of package */
#define PACKAGE "libzip"
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "libzip@nih.at"
/* Define to the full name of this package. */
#define PACKAGE_NAME "libzip"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "libzip 0.9"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "libzip"
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.9"
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* Define to 1 if your <sys/time.h> declares `struct tm'. */
/* #undef TM_IN_SYS_TIME */
/* Version number of package */
#define VERSION "0.9"
#ifndef HAVE_MKSTEMP
int _zip_mkstemp(char *);
#define mkstemp _zip_mkstemp
#endif
#ifdef HAVE_MOVEFILEEXA
#include <windows.h>
#define _zip_rename(s, t) \
(!MoveFileExA((s), (t), \
MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING))
#else
#define _zip_rename rename
#endif
#ifndef HAVE_FSEEKO
#define fseeko(s, o, w) (fseek((s), (long int)(o), (w)))
#endif
#ifndef HAVE_FTELLO
#define ftello(s) ((long)ftell((s)))
#endif
#define CENTRAL_MAGIC "PK\1\2"
#define LOCAL_MAGIC "PK\3\4"
#define EOCD_MAGIC "PK\5\6"
#define DATADES_MAGIC "PK\7\8"
#define TORRENT_SIG "TORRENTZIPPED-"
#define TORRENT_SIG_LEN 14
#define TORRENT_CRC_LEN 8
#define TORRENT_MEM_LEVEL 8
#define CDENTRYSIZE 46u
#define LENTRYSIZE 30
#define MAXCOMLEN 65536
#define EOCDLEN 22
#define CDBUFSIZE (MAXCOMLEN+EOCDLEN)
#define BUFSIZE 8192
/* state of change of a file in zip archive */
enum zip_state { ZIP_ST_UNCHANGED, ZIP_ST_DELETED, ZIP_ST_REPLACED,
ZIP_ST_ADDED, ZIP_ST_RENAMED };
/* constants for struct zip_file's member flags */
#define ZIP_ZF_EOF 1 /* EOF reached */
#define ZIP_ZF_DECOMP 2 /* decompress data */
#define ZIP_ZF_CRC 4 /* compute and compare CRC */
/* directory entry: general purpose bit flags */
#define ZIP_GPBF_ENCRYPTED 0x0001 /* is encrypted */
#define ZIP_GPBF_DATA_DESCRIPTOR 0x0008 /* crc/size after file data */
#define ZIP_GPBF_STRONG_ENCRYPTION 0x0040 /* uses strong encryption */
/* error information */
struct zip_error {
int zip_err; /* libzip error code (ZIP_ER_*) */
int sys_err; /* copy of errno (E*) or zlib error code */
char *str; /* string representation or NULL */
};
/* zip archive, part of API */
struct zip {
char *zn; /* file name */
FILE *zp; /* file */
struct zip_error error; /* error information */
unsigned int flags; /* archive global flags */
unsigned int ch_flags; /* changed archive global flags */
struct zip_cdir *cdir; /* central directory */
char *ch_comment; /* changed archive comment */
int ch_comment_len; /* length of changed zip archive
* comment, -1 if unchanged */
int nentry; /* number of entries */
int nentry_alloc; /* number of entries allocated */
struct zip_entry *entry; /* entries */
int nfile; /* number of opened files within archive */
int nfile_alloc; /* number of files allocated */
struct zip_file **file; /* opened files within archive */
};
/* file in zip archive, part of API */
struct zip_file {
struct zip *za; /* zip archive containing this file */
struct zip_error error; /* error information */
int flags; /* -1: eof, >0: error */
int method; /* compression method */
myoff_t fpos; /* position within zip file (fread/fwrite) */
unsigned long bytes_left; /* number of bytes left to read */
unsigned long cbytes_left; /* number of bytes of compressed data left */
unsigned long crc; /* CRC so far */
unsigned long crc_orig; /* CRC recorded in archive */
char *buffer;
z_stream *zstr;
};
/* zip archive directory entry (central or local) */
struct zip_dirent {
unsigned short version_madeby; /* (c) version of creator */
unsigned short version_needed; /* (cl) version needed to extract */
unsigned short bitflags; /* (cl) general purpose bit flag */
unsigned short comp_method; /* (cl) compression method used */
time_t last_mod; /* (cl) time of last modification */
unsigned int crc; /* (cl) CRC-32 of uncompressed data */
unsigned int comp_size; /* (cl) size of commpressed data */
unsigned int uncomp_size; /* (cl) size of uncommpressed data */
char *filename; /* (cl) file name (NUL-terminated) */
unsigned short filename_len; /* (cl) length of filename (w/o NUL) */
char *extrafield; /* (cl) extra field */
unsigned short extrafield_len; /* (cl) length of extra field */
char *comment; /* (c) file comment */
unsigned short comment_len; /* (c) length of file comment */
unsigned short disk_number; /* (c) disk number start */
unsigned short int_attrib; /* (c) internal file attributes */
unsigned int ext_attrib; /* (c) external file attributes */
unsigned int offset; /* (c) offset of local header */
};
/* zip archive central directory */
struct zip_cdir {
struct zip_dirent *entry; /* directory entries */
int nentry; /* number of entries */
unsigned int size; /* size of central direcotry */
unsigned int offset; /* offset of central directory in file */
char *comment; /* zip archive comment */
unsigned short comment_len; /* length of zip archive comment */
};
struct zip_source {
zip_source_callback f;
void *ud;
};
/* entry in zip archive directory */
struct zip_entry {
enum zip_state state;
struct zip_source *source;
char *ch_filename;
char *ch_comment;
int ch_comment_len;
};
extern const char * const _zip_err_str[];
extern const int _zip_nerr_str;
extern const int _zip_err_type[];
#define ZIP_ENTRY_DATA_CHANGED(x) \
((x)->state == ZIP_ST_REPLACED \
|| (x)->state == ZIP_ST_ADDED)
int _zip_cdir_compute_crc(struct zip *, uLong *);
void _zip_cdir_free(struct zip_cdir *);
struct zip_cdir *_zip_cdir_new(int, struct zip_error *);
int _zip_cdir_write(struct zip_cdir *, FILE *, struct zip_error *);
void _zip_dirent_finalize(struct zip_dirent *);
void _zip_dirent_init(struct zip_dirent *);
int _zip_dirent_read(struct zip_dirent *, FILE *,
unsigned char **, unsigned int, int, struct zip_error *);
void _zip_dirent_torrent_normalize(struct zip_dirent *);
int _zip_dirent_write(struct zip_dirent *, FILE *, int, struct zip_error *);
void _zip_entry_free(struct zip_entry *);
void _zip_entry_init(struct zip *, int);
struct zip_entry *_zip_entry_new(struct zip *);
void _zip_error_clear(struct zip_error *);
void _zip_error_copy(struct zip_error *, struct zip_error *);
void _zip_error_fini(struct zip_error *);
void _zip_error_get(struct zip_error *, int *, int *);
void _zip_error_init(struct zip_error *);
void _zip_error_set(struct zip_error *, int, int);
const char *_zip_error_strerror(struct zip_error *);
int _zip_file_fillbuf(void *, size_t, struct zip_file *);
unsigned int _zip_file_get_offset(struct zip *, int);
int _zip_filerange_crc(FILE *, myoff_t, myoff_t, uLong *, struct zip_error *);
struct zip_source *_zip_source_file_or_p(struct zip *, const char *, FILE *,
myoff_t, myoff_t);
void _zip_free(struct zip *);
const char *_zip_get_name(struct zip *, int, int, struct zip_error *);
int _zip_local_header_read(struct zip *, int);
void *_zip_memdup(const void *, size_t, struct zip_error *);
int _zip_name_locate(struct zip *, const char *, int, struct zip_error *);
struct zip *_zip_new(struct zip_error *);
unsigned short _zip_read2(unsigned char **);
unsigned int _zip_read4(unsigned char **);
int _zip_replace(struct zip *, int, const char *, struct zip_source *);
int _zip_set_name(struct zip *, int, const char *);
int _zip_unchange(struct zip *, int, int);
void _zip_unchange_data(struct zip_entry *);
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *
_zip_error_strerror(struct zip_error *err)
{
const char *zs, *ss;
char buf[128], *s;
_zip_error_fini(err);
if (err->zip_err < 0 || err->zip_err >= _zip_nerr_str) {
sprintf(buf, "Unknown error %d", err->zip_err);
zs = NULL;
ss = buf;
}
else {
zs = _zip_err_str[err->zip_err];
switch (_zip_err_type[err->zip_err]) {
case ZIP_ET_SYS:
ss = strerror(err->sys_err);
break;
case ZIP_ET_ZLIB:
ss = zError(err->sys_err);
break;
default:
ss = NULL;
}
}
if (ss == NULL)
return zs;
else {
if ((s=(char *)malloc(strlen(ss)
+ (zs ? strlen(zs)+2 : 0) + 1)) == NULL)
return _zip_err_str[ZIP_ER_MEMORY];
sprintf(s, "%s%s%s",
(zs ? zs : ""),
(zs ? ": " : ""),
ss);
err->str = s;
return s;
}
}
#include <stdlib.h>
void
_zip_error_clear(struct zip_error *err)
{
err->zip_err = ZIP_ER_OK;
err->sys_err = 0;
}
void
_zip_error_copy(struct zip_error *dst, struct zip_error *src)
{
dst->zip_err = src->zip_err;
dst->sys_err = src->sys_err;
}
void
_zip_error_fini(struct zip_error *err)
{
free(err->str);
err->str = NULL;
}
void
_zip_error_get(struct zip_error *err, int *zep, int *sep)
{
if (zep)
*zep = err->zip_err;
if (sep) {
if (zip_error_get_sys_type(err->zip_err) != ZIP_ET_NONE)
*sep = err->sys_err;
else
*sep = 0;
}
}
void
_zip_error_init(struct zip_error *err)
{
err->zip_err = ZIP_ER_OK;
err->sys_err = 0;
err->str = NULL;
}
void
_zip_error_set(struct zip_error *err, int ze, int se)
{
if (err) {
err->zip_err = ze;
err->sys_err = se;
}
}
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
int
_zip_mkstemp(char *path)
{
int fd;
char *start, *trv;
struct stat sbuf;
pid_t pid;
/* To guarantee multiple calls generate unique names even if
the file is not created. 676 different possibilities with 7
or more X's, 26 with 6 or less. */
static char xtra[2] = "aa";
int xcnt = 0;
pid = getpid();
/* Move to end of path and count trailing X's. */
for (trv = path; *trv; ++trv)
if (*trv == 'X')
xcnt++;
else
xcnt = 0;
/* Use at least one from xtra. Use 2 if more than 6 X's. */
if (*(trv - 1) == 'X')
*--trv = xtra[0];
if (xcnt > 6 && *(trv - 1) == 'X')
*--trv = xtra[1];
/* Set remaining X's to pid digits with 0's to the left. */
while (*--trv == 'X') {
*trv = (pid % 10) + '0';
pid /= 10;
}
/* update xtra for next call. */
if (xtra[0] != 'z')
xtra[0]++;
else {
xtra[0] = 'a';
if (xtra[1] != 'z')
xtra[1]++;
else
xtra[1] = 'a';
}
/*
* check the target directory; if you have six X's and it
* doesn't exist this runs for a *very* long time.
*/
for (start = trv + 1;; --trv) {
if (trv <= path)
break;
if (*trv == '/') {
*trv = '\0';
if (stat(path, &sbuf))
return (0);
if (!S_ISDIR(sbuf.st_mode)) {
errno = ENOTDIR;
return (0);
}
*trv = '/';
break;
}
}
for (;;) {
if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)
return (fd);
if (errno != EEXIST)
return (0);
/* tricky little algorithm for backward compatibility */
for (trv = start;;) {
if (!*trv)
return (0);
if (*trv == 'z')
*trv++ = 'a';
else {
if (isdigit((unsigned char)*trv))
*trv = 'a';
else
++*trv;
break;
}
}
}
/*NOTREACHED*/
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
static time_t _zip_d2u_time(int, int);
static char *_zip_readfpstr(FILE *, unsigned int, int, struct zip_error *);
static char *_zip_readstr(unsigned char **, int, int, struct zip_error *);
static void _zip_u2d_time(time_t, unsigned short *, unsigned short *);
static void _zip_write2(unsigned short, FILE *);
static void _zip_write4(unsigned int, FILE *);
void
_zip_cdir_free(struct zip_cdir *cd)
{
int i;
if (!cd)
return;
for (i=0; i<cd->nentry; i++)
_zip_dirent_finalize(cd->entry+i);
free(cd->comment);
free(cd->entry);
free(cd);
}
struct zip_cdir *
_zip_cdir_new(int nentry, struct zip_error *error)
{
struct zip_cdir *cd;
if ((cd=(struct zip_cdir *)malloc(sizeof(*cd))) == NULL) {
_zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
if ((cd->entry=(struct zip_dirent *)malloc(sizeof(*(cd->entry))*nentry))
== NULL) {
_zip_error_set(error, ZIP_ER_MEMORY, 0);
free(cd);
return NULL;
}
/* entries must be initialized by caller */
cd->nentry = nentry;
cd->size = cd->offset = 0;
cd->comment = NULL;
cd->comment_len = 0;
return cd;
}
int
_zip_cdir_write(struct zip_cdir *cd, FILE *fp, struct zip_error *error)
{
int i;
cd->offset = ftello(fp);
for (i=0; i<cd->nentry; i++) {
if (_zip_dirent_write(cd->entry+i, fp, 0, error) != 0)
return -1;
}
cd->size = ftello(fp) - cd->offset;
/* clearerr(fp); */
fwrite(EOCD_MAGIC, 1, 4, fp);
_zip_write4(0, fp);
_zip_write2((unsigned short)cd->nentry, fp);
_zip_write2((unsigned short)cd->nentry, fp);
_zip_write4(cd->size, fp);
_zip_write4(cd->offset, fp);
_zip_write2(cd->comment_len, fp);
fwrite(cd->comment, 1, cd->comment_len, fp);
if (ferror(fp)) {
_zip_error_set(error, ZIP_ER_WRITE, errno);
return -1;
}
return 0;
}
void
_zip_dirent_finalize(struct zip_dirent *zde)
{
free(zde->filename);
zde->filename = NULL;
free(zde->extrafield);
zde->extrafield = NULL;
free(zde->comment);
zde->comment = NULL;
}
void
_zip_dirent_init(struct zip_dirent *de)
{
de->version_madeby = 0;
de->version_needed = 20; /* 2.0 */
de->bitflags = 0;
de->comp_method = 0;
de->last_mod = 0;
de->crc = 0;
de->comp_size = 0;
de->uncomp_size = 0;
de->filename = NULL;
de->filename_len = 0;
de->extrafield = NULL;
de->extrafield_len = 0;
de->comment = NULL;
de->comment_len = 0;
de->disk_number = 0;
de->int_attrib = 0;
de->ext_attrib = 0;
de->offset = 0;
}
/* _zip_dirent_read(zde, fp, bufp, left, localp, error):
Fills the zip directory entry zde.
If bufp is non-NULL, data is taken from there and bufp is advanced
by the amount of data used; no more than left bytes are used.
Otherwise data is read from fp as needed.
If localp != 0, it reads a local header instead of a central
directory entry.
Returns 0 if successful. On error, error is filled in and -1 is
returned.
*/
int
_zip_dirent_read(struct zip_dirent *zde, FILE *fp,
unsigned char **bufp, unsigned int left, int localp,
struct zip_error *error)
{
unsigned char buf[CDENTRYSIZE];
unsigned char *cur;
unsigned short dostime, dosdate;
unsigned int size;
if (localp)
size = LENTRYSIZE;
else
size = CDENTRYSIZE;
if (bufp) {
/* use data from buffer */
cur = *bufp;
if (left < size) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return -1;
}
}
else {
/* read entry from disk */
if ((fread(buf, 1, size, fp)<size)) {
_zip_error_set(error, ZIP_ER_READ, errno);
return -1;
}
left = size;
cur = buf;
}
if (memcmp(cur, (localp ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return -1;
}
cur += 4;
/* convert buffercontents to zip_dirent */
if (!localp)
zde->version_madeby = _zip_read2(&cur);
else
zde->version_madeby = 0;
zde->version_needed = _zip_read2(&cur);
zde->bitflags = _zip_read2(&cur);
zde->comp_method = _zip_read2(&cur);
/* convert to time_t */
dostime = _zip_read2(&cur);
dosdate = _zip_read2(&cur);
zde->last_mod = _zip_d2u_time(dostime, dosdate);
zde->crc = _zip_read4(&cur);
zde->comp_size = _zip_read4(&cur);
zde->uncomp_size = _zip_read4(&cur);
zde->filename_len = _zip_read2(&cur);
zde->extrafield_len = _zip_read2(&cur);
if (localp) {
zde->comment_len = 0;
zde->disk_number = 0;
zde->int_attrib = 0;
zde->ext_attrib = 0;
zde->offset = 0;
} else {
zde->comment_len = _zip_read2(&cur);
zde->disk_number = _zip_read2(&cur);
zde->int_attrib = _zip_read2(&cur);
zde->ext_attrib = _zip_read4(&cur);
zde->offset = _zip_read4(&cur);
}
zde->filename = NULL;
zde->extrafield = NULL;
zde->comment = NULL;
if (bufp) {
if (left < CDENTRYSIZE + (zde->filename_len+zde->extrafield_len
+zde->comment_len)) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return -1;
}
if (zde->filename_len) {
zde->filename = _zip_readstr(&cur, zde->filename_len, 1, error);
if (!zde->filename)
return -1;
}
if (zde->extrafield_len) {
zde->extrafield = _zip_readstr(&cur, zde->extrafield_len, 0,
error);
if (!zde->extrafield)
return -1;
}
if (zde->comment_len) {
zde->comment = _zip_readstr(&cur, zde->comment_len, 0, error);
if (!zde->comment)
return -1;
}
}
else {
if (zde->filename_len) {
zde->filename = _zip_readfpstr(fp, zde->filename_len, 1, error);
if (!zde->filename)
return -1;
}
if (zde->extrafield_len) {
zde->extrafield = _zip_readfpstr(fp, zde->extrafield_len, 0,
error);
if (!zde->extrafield)
return -1;
}
if (zde->comment_len) {
zde->comment = _zip_readfpstr(fp, zde->comment_len, 0, error);
if (!zde->comment)
return -1;
}
}
if (bufp)
*bufp = cur;
return 0;
}
/* _zip_dirent_torrent_normalize(de);
Set values suitable for torrentzip.
*/
void
_zip_dirent_torrent_normalize(struct zip_dirent *de)
{
static struct tm torrenttime;
static time_t last_mod = 0;
if (last_mod == 0) {
#ifdef HAVE_STRUCT_TM_TM_ZONE
time_t now;
struct tm *l;
#endif
torrenttime.tm_sec = 0;
torrenttime.tm_min = 32;
torrenttime.tm_hour = 23;
torrenttime.tm_mday = 24;
torrenttime.tm_mon = 11;
torrenttime.tm_year = 96;
torrenttime.tm_wday = 0;
torrenttime.tm_yday = 0;
torrenttime.tm_isdst = 0;
#ifdef HAVE_STRUCT_TM_TM_ZONE
time(&now);
l = localtime(&now);
torrenttime.tm_gmtoff = l->tm_gmtoff;
torrenttime.tm_zone = l->tm_zone;
#endif
last_mod = mktime(&torrenttime);
}
de->version_madeby = 0;
de->version_needed = 20; /* 2.0 */
de->bitflags = 2; /* maximum compression */
de->comp_method = ZIP_CM_DEFLATE;
de->last_mod = last_mod;
de->disk_number = 0;
de->int_attrib = 0;
de->ext_attrib = 0;
de->offset = 0;
free(de->extrafield);
de->extrafield = NULL;
de->extrafield_len = 0;
free(de->comment);
de->comment = NULL;
de->comment_len = 0;
}
/* _zip_dirent_write(zde, fp, localp, error):
Writes zip directory entry zde to file fp.
If localp != 0, it writes a local header instead of a central
directory entry.
Returns 0 if successful. On error, error is filled in and -1 is
returned.
*/
int
_zip_dirent_write(struct zip_dirent *zde, FILE *fp, int localp,
struct zip_error *error)
{
unsigned short dostime, dosdate;
fwrite(localp ? LOCAL_MAGIC : CENTRAL_MAGIC, 1, 4, fp);
if (!localp)
_zip_write2(zde->version_madeby, fp);
_zip_write2(zde->version_needed, fp);
_zip_write2(zde->bitflags, fp);
_zip_write2(zde->comp_method, fp);
_zip_u2d_time(zde->last_mod, &dostime, &dosdate);
_zip_write2(dostime, fp);
_zip_write2(dosdate, fp);
_zip_write4(zde->crc, fp);
_zip_write4(zde->comp_size, fp);
_zip_write4(zde->uncomp_size, fp);
_zip_write2(zde->filename_len, fp);
_zip_write2(zde->extrafield_len, fp);
if (!localp) {
_zip_write2(zde->comment_len, fp);
_zip_write2(zde->disk_number, fp);
_zip_write2(zde->int_attrib, fp);
_zip_write4(zde->ext_attrib, fp);
_zip_write4(zde->offset, fp);
}
if (zde->filename_len)
fwrite(zde->filename, 1, zde->filename_len, fp);
if (zde->extrafield_len)
fwrite(zde->extrafield, 1, zde->extrafield_len, fp);
if (!localp) {
if (zde->comment_len)
fwrite(zde->comment, 1, zde->comment_len, fp);
}
if (ferror(fp)) {
_zip_error_set(error, ZIP_ER_WRITE, errno);
return -1;
}
return 0;
}
static time_t
_zip_d2u_time(int dtime, int ddate)
{
struct tm *tm;
time_t now;
now = time(NULL);
tm = localtime(&now);
/* let mktime decide if DST is in effect */
tm->tm_isdst = -1;
tm->tm_year = ((ddate>>9)&127) + 1980 - 1900;
tm->tm_mon = ((ddate>>5)&15) - 1;
tm->tm_mday = ddate&31;
tm->tm_hour = (dtime>>11)&31;
tm->tm_min = (dtime>>5)&63;
tm->tm_sec = (dtime<<1)&62;
return mktime(tm);
}
unsigned short
_zip_read2(unsigned char **a)
{
unsigned short ret;
ret = (*a)[0]+((*a)[1]<<8);
*a += 2;
return ret;
}
unsigned int
_zip_read4(unsigned char **a)
{
unsigned int ret;
ret = ((((((*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0];
*a += 4;
return ret;
}
static char *
_zip_readfpstr(FILE *fp, unsigned int len, int nulp, struct zip_error *error)
{
char *r, *o;
r = (char *)malloc(nulp ? len+1 : len);
if (!r) {
_zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
if (fread(r, 1, len, fp)<len) {
free(r);
_zip_error_set(error, ZIP_ER_READ, errno);
return NULL;
}
if (nulp) {
/* replace any in-string NUL characters with spaces */
r[len] = 0;
for (o=r; o<r+len; o++)
if (*o == '\0')
*o = ' ';
}
return r;
}
static char *
_zip_readstr(unsigned char **buf, int len, int nulp, struct zip_error *error)
{
char *r, *o;
r = (char *)malloc(nulp ? len+1 : len);
if (!r) {
_zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
memcpy(r, *buf, len);
*buf += len;
if (nulp) {
/* replace any in-string NUL characters with spaces */
r[len] = 0;
for (o=r; o<r+len; o++)
if (*o == '\0')
*o = ' ';
}
return r;
}
static void
_zip_write2(unsigned short i, FILE *fp)
{
putc(i&0xff, fp);
putc((i>>8)&0xff, fp);
return;
}
static void
_zip_write4(unsigned int i, FILE *fp)
{
putc(i&0xff, fp);
putc((i>>8)&0xff, fp);
putc((i>>16)&0xff, fp);
putc((i>>24)&0xff, fp);
return;
}
static void
_zip_u2d_time(time_t time, unsigned short *dtime, unsigned short *ddate)
{
struct tm *tm;
tm = localtime(&time);
*ddate = ((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5)
+ tm->tm_mday;
*dtime = ((tm->tm_hour)<<11) + ((tm->tm_min)<<5)
+ ((tm->tm_sec)>>1);
return;
}
ZIP_EXTERN int
zip_delete(struct zip *za, int idx)
{
if (idx < 0 || idx >= za->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
/* allow duplicate file names, because the file will
* be removed directly afterwards */
if (_zip_unchange(za, idx, 1) != 0)
return -1;
za->entry[idx].state = ZIP_ST_DELETED;
return 0;
}
ZIP_EXTERN void
zip_error_clear(struct zip *za)
{
_zip_error_clear(&za->error);
}
ZIP_EXTERN int
zip_add(struct zip *za, const char *name, struct zip_source *source)
{
if (name == NULL || source == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
return _zip_replace(za, -1, name, source);
}
ZIP_EXTERN int
zip_error_get_sys_type(int ze)
{
if (ze < 0 || ze >= _zip_nerr_str)
return 0;
return _zip_err_type[ze];
}
ZIP_EXTERN void
zip_error_get(struct zip *za, int *zep, int *sep)
{
_zip_error_get(&za->error, zep, sep);
}
const char * const _zip_err_str[] = {
"No error",
"Multi-disk zip archives not supported",
"Renaming temporary file failed",
"Closing zip archive failed",
"Seek error",
"Read error",
"Write error",
"CRC error",
"Containing zip archive was closed",
"No such file",
"File already exists",
"Can't open file",
"Failure to create temporary file",
"Zlib error",
"Malloc failure",
"Entry has been changed",
"Compression method not supported",
"Premature EOF",
"Invalid argument",
"Not a zip archive",
"Internal error",
"Zip archive inconsistent",
"Can't remove file",
"Entry has been deleted",
};
const int _zip_nerr_str = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]);
#define N ZIP_ET_NONE
#define S ZIP_ET_SYS
#define Z ZIP_ET_ZLIB
const int _zip_err_type[] = {
N,
N,
S,
S,
S,
S,
S,
N,
N,
N,
N,
S,
S,
Z,
N,
N,
N,
N,
N,
N,
N,
N,
S,
N,
};
struct zip_entry *
_zip_entry_new(struct zip *za)
{
struct zip_entry *ze;
if (!za) {
ze = (struct zip_entry *)malloc(sizeof(struct zip_entry));
if (!ze) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
}
else {
if (za->nentry >= za->nentry_alloc-1) {
za->nentry_alloc += 16;
za->entry = (struct zip_entry *)realloc(za->entry,
sizeof(struct zip_entry)
* za->nentry_alloc);
if (!za->entry) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
}
ze = za->entry+za->nentry;
}
ze->state = ZIP_ST_UNCHANGED;
ze->ch_filename = NULL;
ze->ch_comment = NULL;
ze->ch_comment_len = -1;
ze->source = NULL;
if (za)
za->nentry++;
return ze;
}
void
_zip_entry_free(struct zip_entry *ze)
{
free(ze->ch_filename);
ze->ch_filename = NULL;
free(ze->ch_comment);
ze->ch_comment = NULL;
ze->ch_comment_len = -1;
_zip_unchange_data(ze);
}
static int add_data(struct zip *, struct zip_source *, struct zip_dirent *,
FILE *);
static int add_data_comp(zip_source_callback, void *, struct zip_stat *,
FILE *, struct zip_error *);
static int add_data_uncomp(struct zip *, zip_source_callback, void *,
struct zip_stat *, FILE *);
static void ch_set_error(struct zip_error *, zip_source_callback, void *);
static int copy_data(FILE *, myoff_t, FILE *, struct zip_error *);
static int write_cdir(struct zip *, struct zip_cdir *, FILE *);
static int _zip_cdir_set_comment(struct zip_cdir *, struct zip *);
static int _zip_changed(struct zip *, int *);
static char *_zip_create_temp_output(struct zip *, FILE **);
static int _zip_torrentzip_cmp(const void *, const void *);
struct filelist {
int idx;
const char *name;
};
ZIP_EXTERN int
zip_close(struct zip *za)
{
int survivors;
int i, j, error;
char *temp;
FILE *out;
mode_t mask;
struct zip_cdir *cd;
struct zip_dirent de;
struct filelist *filelist;
int reopen_on_error;
int new_torrentzip;
reopen_on_error = 0;
if (za == NULL)
return -1;
if (!_zip_changed(za, &survivors)) {
_zip_free(za);
return 0;
}
/* don't create zip files with no entries */
if (survivors == 0) {
if (za->zn && za->zp) {
if (remove(za->zn) != 0) {
_zip_error_set(&za->error, ZIP_ER_REMOVE, errno);
return -1;
}
}
_zip_free(za);
return 0;
}
if ((filelist=(struct filelist *)malloc(sizeof(filelist[0])*survivors))
== NULL)
return -1;
if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) {
free(filelist);
return -1;
}
for (i=0; i<survivors; i++)
_zip_dirent_init(&cd->entry[i]);
/* archive comment is special for torrentzip */
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) {
cd->comment = _zip_memdup(TORRENT_SIG "XXXXXXXX",
TORRENT_SIG_LEN + TORRENT_CRC_LEN,
&za->error);
if (cd->comment == NULL) {
_zip_cdir_free(cd);
free(filelist);
return -1;
}
cd->comment_len = TORRENT_SIG_LEN + TORRENT_CRC_LEN;
}
else if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0) {
if (_zip_cdir_set_comment(cd, za) == -1) {
_zip_cdir_free(cd);
free(filelist);
return -1;
}
}
if ((temp=_zip_create_temp_output(za, &out)) == NULL) {
_zip_cdir_free(cd);
return -1;
}
/* create list of files with index into original archive */
for (i=j=0; i<za->nentry; i++) {
if (za->entry[i].state == ZIP_ST_DELETED)
continue;
filelist[j].idx = i;
filelist[j].name = zip_get_name(za, i, 0);
j++;
}
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
qsort(filelist, survivors, sizeof(filelist[0]),
_zip_torrentzip_cmp);
new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1
&& zip_get_archive_flag(za, ZIP_AFL_TORRENT,
ZIP_FL_UNCHANGED) == 0);
error = 0;
for (j=0; j<survivors; j++) {
i = filelist[j].idx;
/* create new local directory entry */
if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
_zip_dirent_init(&de);
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
_zip_dirent_torrent_normalize(&de);
/* use it as central directory entry */
memcpy(cd->entry+j, &de, sizeof(cd->entry[j]));
/* set/update file name */
if (za->entry[i].ch_filename == NULL) {
if (za->entry[i].state == ZIP_ST_ADDED) {
de.filename = strdup("-");
de.filename_len = 1;
cd->entry[j].filename = "-";
}
else {
de.filename = strdup(za->cdir->entry[i].filename);
de.filename_len = strlen(de.filename);
cd->entry[j].filename = za->cdir->entry[i].filename;
cd->entry[j].filename_len = de.filename_len;
}
}
}
else {
/* copy existing directory entries */
if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) {
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
error = 1;
break;
}
if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0) {
error = 1;
break;
}
if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
de.crc = za->cdir->entry[i].crc;
de.comp_size = za->cdir->entry[i].comp_size;
de.uncomp_size = za->cdir->entry[i].uncomp_size;
de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR;
}
memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j]));
}
if (za->entry[i].ch_filename) {
free(de.filename);
if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) {
error = 1;
break;
}
de.filename_len = strlen(de.filename);
cd->entry[j].filename = za->entry[i].ch_filename;
cd->entry[j].filename_len = de.filename_len;
}
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0
&& za->entry[i].ch_comment_len != -1) {
/* as the rest of cd entries, its malloc/free is done by za */
cd->entry[j].comment = za->entry[i].ch_comment;
cd->entry[j].comment_len = za->entry[i].ch_comment_len;
}
cd->entry[j].offset = ftello(out);
if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) {
struct zip_source *zs;
zs = NULL;
if (!ZIP_ENTRY_DATA_CHANGED(za->entry+i)) {
if ((zs=zip_source_zip(za, za, i, ZIP_FL_RECOMPRESS, 0, -1))
== NULL) {
error = 1;
break;
}
}
if (add_data(za, zs ? zs : za->entry[i].source, &de, out) < 0) {
error = 1;
break;
}
cd->entry[j].last_mod = de.last_mod;
cd->entry[j].comp_method = de.comp_method;
cd->entry[j].comp_size = de.comp_size;
cd->entry[j].uncomp_size = de.uncomp_size;
cd->entry[j].crc = de.crc;
}
else {
if (_zip_dirent_write(&de, out, 1, &za->error) < 0) {
error = 1;
break;
}
/* we just read the local dirent, file is at correct position */
if (copy_data(za->zp, cd->entry[j].comp_size, out,
&za->error) < 0) {
error = 1;
break;
}
}
_zip_dirent_finalize(&de);
}
if (!error) {
if (write_cdir(za, cd, out) < 0)
error = 1;
}
/* pointers in cd entries are owned by za */
cd->nentry = 0;
_zip_cdir_free(cd);
if (error) {
_zip_dirent_finalize(&de);
fclose(out);
remove(temp);
free(temp);
return -1;
}
if (fclose(out) != 0) {
_zip_error_set(&za->error, ZIP_ER_CLOSE, errno);
remove(temp);
free(temp);
return -1;
}
if (za->zp) {
fclose(za->zp);
za->zp = NULL;
reopen_on_error = 1;
}
if (_zip_rename(temp, za->zn) != 0) {
_zip_error_set(&za->error, ZIP_ER_RENAME, errno);
remove(temp);
free(temp);
if (reopen_on_error) {
/* ignore errors, since we're already in an error case */
za->zp = fopen(za->zn, "rb");
}
return -1;
}
mask = umask(0);
umask(mask);
chmod(za->zn, 0666&~mask);
_zip_free(za);
free(temp);
return 0;
}
static int
add_data(struct zip *za, struct zip_source *zs, struct zip_dirent *de, FILE *ft)
{
myoff_t offstart, offend;
zip_source_callback cb;
void *ud;
struct zip_stat st;
cb = zs->f;
ud = zs->ud;
if (cb(ud, &st, sizeof(st), ZIP_SOURCE_STAT) < (ssize_t)sizeof(st)) {
ch_set_error(&za->error, cb, ud);
return -1;
}
if (cb(ud, NULL, 0, ZIP_SOURCE_OPEN) < 0) {
ch_set_error(&za->error, cb, ud);
return -1;
}
offstart = ftello(ft);
if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
return -1;
if (st.comp_method != ZIP_CM_STORE) {
if (add_data_comp(cb, ud, &st, ft, &za->error) < 0)
return -1;
}
else {
if (add_data_uncomp(za, cb, ud, &st, ft) < 0)
return -1;
}
if (cb(ud, NULL, 0, ZIP_SOURCE_CLOSE) < 0) {
ch_set_error(&za->error, cb, ud);
return -1;
}
offend = ftello(ft);
if (fseeko(ft, offstart, SEEK_SET) < 0) {
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
return -1;
}
de->last_mod = st.mtime;
de->comp_method = st.comp_method;
de->crc = st.crc;
de->uncomp_size = st.size;
de->comp_size = st.comp_size;
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
_zip_dirent_torrent_normalize(de);
if (_zip_dirent_write(de, ft, 1, &za->error) < 0)
return -1;
if (fseeko(ft, offend, SEEK_SET) < 0) {
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
return -1;
}
return 0;
}
static int
add_data_comp(zip_source_callback cb, void *ud, struct zip_stat *st,FILE *ft,
struct zip_error *error)
{
char buf[BUFSIZE];
ssize_t n;
st->comp_size = 0;
while ((n=cb(ud, buf, sizeof(buf), ZIP_SOURCE_READ)) > 0) {
if (fwrite(buf, 1, n, ft) != (size_t)n) {
_zip_error_set(error, ZIP_ER_WRITE, errno);
return -1;
}
st->comp_size += n;
}
if (n < 0) {
ch_set_error(error, cb, ud);
return -1;
}
return 0;
}
static int
add_data_uncomp(struct zip *za, zip_source_callback cb, void *ud,
struct zip_stat *st, FILE *ft)
{
char b1[BUFSIZE], b2[BUFSIZE];
int end, flush, ret;
ssize_t n;
size_t n2;
z_stream zstr;
int mem_level;
st->comp_method = ZIP_CM_DEFLATE;
st->comp_size = st->size = 0;
st->crc = crc32(0, NULL, 0);
zstr.zalloc = Z_NULL;
zstr.zfree = Z_NULL;
zstr.opaque = NULL;
zstr.avail_in = 0;
zstr.avail_out = 0;
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0))
mem_level = TORRENT_MEM_LEVEL;
else
mem_level = MAX_MEM_LEVEL;
/* -MAX_WBITS: undocumented feature of zlib to _not_ write a zlib header */
deflateInit2(&zstr, Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS, mem_level,
Z_DEFAULT_STRATEGY);
zstr.next_out = (Bytef *)b2;
zstr.avail_out = sizeof(b2);
zstr.avail_in = 0;
flush = 0;
end = 0;
while (!end) {
if (zstr.avail_in == 0 && !flush) {
if ((n=cb(ud, b1, sizeof(b1), ZIP_SOURCE_READ)) < 0) {
ch_set_error(&za->error, cb, ud);
deflateEnd(&zstr);
return -1;
}
if (n > 0) {
zstr.avail_in = n;
zstr.next_in = (Bytef *)b1;
st->size += n;
st->crc = crc32(st->crc, (Bytef *)b1, n);
}
else
flush = Z_FINISH;
}
ret = deflate(&zstr, flush);
if (ret != Z_OK && ret != Z_STREAM_END) {
_zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
return -1;
}
if (zstr.avail_out != sizeof(b2)) {
n2 = sizeof(b2) - zstr.avail_out;
if (fwrite(b2, 1, n2, ft) != n2) {
_zip_error_set(&za->error, ZIP_ER_WRITE, errno);
return -1;
}
zstr.next_out = (Bytef *)b2;
zstr.avail_out = sizeof(b2);
st->comp_size += n2;
}
if (ret == Z_STREAM_END) {
deflateEnd(&zstr);
end = 1;
}
}
return 0;
}
static void
ch_set_error(struct zip_error *error, zip_source_callback cb, void *ud)
{
int e[2];
if ((cb(ud, e, sizeof(e), ZIP_SOURCE_ERROR)) < (ssize_t)sizeof(e)) {
error->zip_err = ZIP_ER_INTERNAL;
error->sys_err = 0;
}
else {
error->zip_err = e[0];
error->sys_err = e[1];
}
}
static int
copy_data(FILE *fs, myoff_t len, FILE *ft, struct zip_error *error)
{
char buf[BUFSIZE];
int n, nn;
if (len == 0)
return 0;
while (len > 0) {
nn = len > sizeof(buf) ? sizeof(buf) : len;
if ((n=fread(buf, 1, nn, fs)) < 0) {
_zip_error_set(error, ZIP_ER_READ, errno);
return -1;
}
else if (n == 0) {
_zip_error_set(error, ZIP_ER_EOF, 0);
return -1;
}
if (fwrite(buf, 1, n, ft) != (size_t)n) {
_zip_error_set(error, ZIP_ER_WRITE, errno);
return -1;
}
len -= n;
}
return 0;
}
static int
write_cdir(struct zip *za, struct zip_cdir *cd, FILE *out)
{
myoff_t offset;
uLong crc;
char buf[TORRENT_CRC_LEN+1];
if (_zip_cdir_write(cd, out, &za->error) < 0)
return -1;
if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0)
return 0;
/* fix up torrentzip comment */
offset = ftello(out);
if (_zip_filerange_crc(out, cd->offset, cd->size, &crc, &za->error) < 0)
return -1;
snprintf(buf, sizeof(buf), "%08lX", (long)crc);
if (fseeko(out, offset-TORRENT_CRC_LEN, SEEK_SET) < 0) {
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
return -1;
}
if (fwrite(buf, TORRENT_CRC_LEN, 1, out) != 1) {
_zip_error_set(&za->error, ZIP_ER_WRITE, errno);
return -1;
}
return 0;
}
static int
_zip_cdir_set_comment(struct zip_cdir *dest, struct zip *src)
{
if (src->ch_comment_len != -1) {
dest->comment = _zip_memdup(src->ch_comment,
src->ch_comment_len, &src->error);
if (dest->comment == NULL)
return -1;
dest->comment_len = src->ch_comment_len;
} else {
if (src->cdir && src->cdir->comment) {
dest->comment = _zip_memdup(src->cdir->comment,
src->cdir->comment_len, &src->error);
if (dest->comment == NULL)
return -1;
dest->comment_len = src->cdir->comment_len;
}
}
return 0;
}
static int
_zip_changed(struct zip *za, int *survivorsp)
{
int changed, i, survivors;
changed = survivors = 0;
if (za->ch_comment_len != -1
|| za->ch_flags != za->flags)
changed = 1;
for (i=0; i<za->nentry; i++) {
if ((za->entry[i].state != ZIP_ST_UNCHANGED)
|| (za->entry[i].ch_comment_len != -1))
changed = 1;
if (za->entry[i].state != ZIP_ST_DELETED)
survivors++;
}
*survivorsp = survivors;
return changed;
}
static char *
_zip_create_temp_output(struct zip *za, FILE **outp)
{
char *temp;
int tfd;
FILE *tfp;
if ((temp=(char *)malloc(strlen(za->zn)+8)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
sprintf(temp, "%s.XXXXXX", za->zn);
if ((tfd=mkstemp(temp)) == -1) {
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
free(temp);
return NULL;
}
if ((tfp=fdopen(tfd, "r+b")) == NULL) {
_zip_error_set(&za->error, ZIP_ER_TMPOPEN, errno);
close(tfd);
remove(temp);
free(temp);
return NULL;
}
*outp = tfp;
return temp;
}
static int
_zip_torrentzip_cmp(const void *a, const void *b)
{
return strcasecmp(((const struct filelist *)a)->name,
((const struct filelist *)b)->name);
}
ZIP_EXTERN int
zip_add_dir(struct zip *za, const char *name)
{
int len, ret;
char *s;
struct zip_source *source;
if (name == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
s = NULL;
len = strlen(name);
if (name[len-1] != '/') {
if ((s=(char *)malloc(len+2)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return -1;
}
strcpy(s, name);
s[len] = '/';
s[len+1] = '\0';
}
if ((source=zip_source_buffer(za, NULL, 0, 0)) == NULL) {
free(s);
return -1;
}
ret = _zip_replace(za, -1, s ? s : name, source);
free(s);
if (ret < 0)
zip_source_free(source);
return ret;
}
ZIP_EXTERN int
zip_error_to_str(char *buf, size_t len, int ze, int se)
{
const char *zs, *ss;
if (ze < 0 || ze >= _zip_nerr_str)
return snprintf(buf, len, "Unknown error %d", ze);
zs = _zip_err_str[ze];
switch (_zip_err_type[ze]) {
case ZIP_ET_SYS:
ss = strerror(se);
break;
case ZIP_ET_ZLIB:
ss = zError(se);
break;
default:
ss = NULL;
}
return snprintf(buf, len, "%s%s%s",
zs, (ss ? ": " : ""), (ss ? ss : ""));
}
ZIP_EXTERN void
zip_file_error_clear(struct zip_file *zf)
{
_zip_error_clear(&zf->error);
}
ZIP_EXTERN int
zip_fclose(struct zip_file *zf)
{
int i, ret;
if (zf->zstr)
inflateEnd(zf->zstr);
free(zf->buffer);
free(zf->zstr);
for (i=0; i<zf->za->nfile; i++) {
if (zf->za->file[i] == zf) {
zf->za->file[i] = zf->za->file[zf->za->nfile-1];
zf->za->nfile--;
break;
}
}
ret = 0;
if (zf->error.zip_err)
ret = zf->error.zip_err;
else if ((zf->flags & ZIP_ZF_CRC) && (zf->flags & ZIP_ZF_EOF)) {
/* if EOF, compare CRC */
if (zf->crc_orig != zf->crc)
ret = ZIP_ER_CRC;
}
free(zf);
return ret;
}
int
_zip_filerange_crc(FILE *fp, myoff_t start, myoff_t len, uLong *crcp,
struct zip_error *errp)
{
Bytef buf[BUFSIZE];
size_t n;
*crcp = crc32(0L, Z_NULL, 0);
if (fseeko(fp, start, SEEK_SET) != 0) {
_zip_error_set(errp, ZIP_ER_SEEK, errno);
return -1;
}
while (len > 0) {
n = len > BUFSIZE ? BUFSIZE : len;
if ((n=fread(buf, 1, n, fp)) <= 0) {
_zip_error_set(errp, ZIP_ER_READ, errno);
return -1;
}
*crcp = crc32(*crcp, buf, n);
len-= n;
}
return 0;
}
ZIP_EXTERN const char *
zip_file_strerror(struct zip_file *zf)
{
return _zip_error_strerror(&zf->error);
}
/* _zip_file_get_offset(za, ze):
Returns the offset of the file data for entry ze.
On error, fills in za->error and returns 0.
*/
unsigned int
_zip_file_get_offset(struct zip *za, int idx)
{
struct zip_dirent de;
unsigned int offset;
offset = za->cdir->entry[idx].offset;
if (fseeko(za->zp, offset, SEEK_SET) != 0) {
_zip_error_set(&za->error, ZIP_ER_SEEK, errno);
return 0;
}
if (_zip_dirent_read(&de, za->zp, NULL, 0, 1, &za->error) != 0)
return 0;
offset += LENTRYSIZE + de.filename_len + de.extrafield_len;
_zip_dirent_finalize(&de);
return offset;
}
ZIP_EXTERN void
zip_file_error_get(struct zip_file *zf, int *zep, int *sep)
{
_zip_error_get(&zf->error, zep, sep);
}
static struct zip_file *_zip_file_new(struct zip *za);
ZIP_EXTERN struct zip_file *
zip_fopen_index(struct zip *za, int fileno, int flags)
{
int len, ret;
int zfflags;
struct zip_file *zf;
if ((fileno < 0) || (fileno >= za->nentry)) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((flags & ZIP_FL_UNCHANGED) == 0
&& ZIP_ENTRY_DATA_CHANGED(za->entry+fileno)) {
_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
return NULL;
}
if (fileno >= za->cdir->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
zfflags = 0;
switch (za->cdir->entry[fileno].comp_method) {
case ZIP_CM_STORE:
zfflags |= ZIP_ZF_CRC;
break;
case ZIP_CM_DEFLATE:
if ((flags & ZIP_FL_COMPRESSED) == 0)
zfflags |= ZIP_ZF_CRC | ZIP_ZF_DECOMP;
break;
default:
if ((flags & ZIP_FL_COMPRESSED) == 0) {
_zip_error_set(&za->error, ZIP_ER_COMPNOTSUPP, 0);
return NULL;
}
break;
}
zf = _zip_file_new(za);
zf->flags = zfflags;
/* zf->name = za->cdir->entry[fileno].filename; */
zf->method = za->cdir->entry[fileno].comp_method;
zf->bytes_left = za->cdir->entry[fileno].uncomp_size;
zf->cbytes_left = za->cdir->entry[fileno].comp_size;
zf->crc_orig = za->cdir->entry[fileno].crc;
if ((zf->fpos=_zip_file_get_offset(za, fileno)) == 0) {
zip_fclose(zf);
return NULL;
}
if ((zf->flags & ZIP_ZF_DECOMP) == 0)
zf->bytes_left = zf->cbytes_left;
else {
if ((zf->buffer=(char *)malloc(BUFSIZE)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
zip_fclose(zf);
return NULL;
}
len = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
if (len <= 0) {
_zip_error_copy(&za->error, &zf->error);
zip_fclose(zf);
return NULL;
}
if ((zf->zstr = (z_stream *)malloc(sizeof(z_stream))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
zip_fclose(zf);
return NULL;
}
zf->zstr->zalloc = Z_NULL;
zf->zstr->zfree = Z_NULL;
zf->zstr->opaque = NULL;
zf->zstr->next_in = (Bytef *)zf->buffer;
zf->zstr->avail_in = len;
/* negative value to tell zlib that there is no header */
if ((ret=inflateInit2(zf->zstr, -MAX_WBITS)) != Z_OK) {
_zip_error_set(&za->error, ZIP_ER_ZLIB, ret);
zip_fclose(zf);
return NULL;
}
}
return zf;
}
int
_zip_file_fillbuf(void *buf, size_t buflen, struct zip_file *zf)
{
int i, j;
if (zf->error.zip_err != ZIP_ER_OK)
return -1;
if ((zf->flags & ZIP_ZF_EOF) || zf->cbytes_left <= 0 || buflen <= 0)
return 0;
if (fseeko(zf->za->zp, zf->fpos, SEEK_SET) < 0) {
_zip_error_set(&zf->error, ZIP_ER_SEEK, errno);
return -1;
}
if (buflen < zf->cbytes_left)
i = buflen;
else
i = zf->cbytes_left;
j = fread(buf, 1, i, zf->za->zp);
if (j == 0) {
_zip_error_set(&zf->error, ZIP_ER_EOF, 0);
j = -1;
}
else if (j < 0)
_zip_error_set(&zf->error, ZIP_ER_READ, errno);
else {
zf->fpos += j;
zf->cbytes_left -= j;
}
return j;
}
static struct zip_file *
_zip_file_new(struct zip *za)
{
struct zip_file *zf, **file;
int n;
if ((zf=(struct zip_file *)malloc(sizeof(struct zip_file))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
if (za->nfile >= za->nfile_alloc-1) {
n = za->nfile_alloc + 10;
file = (struct zip_file **)realloc(za->file,
n*sizeof(struct zip_file *));
if (file == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
free(zf);
return NULL;
}
za->nfile_alloc = n;
za->file = file;
}
za->file[za->nfile++] = zf;
zf->za = za;
_zip_error_init(&zf->error);
zf->flags = 0;
zf->crc = crc32(0L, Z_NULL, 0);
zf->crc_orig = 0;
zf->method = -1;
zf->bytes_left = zf->cbytes_left = 0;
zf->fpos = 0;
zf->buffer = NULL;
zf->zstr = NULL;
return zf;
}
ZIP_EXTERN struct zip_file *
zip_fopen(struct zip *za, const char *fname, int flags)
{
int idx;
if ((idx=zip_name_locate(za, fname, flags)) < 0)
return NULL;
return zip_fopen_index(za, idx, flags);
}
ZIP_EXTERN int
zip_set_file_comment(struct zip *za, int idx, const char *comment, int len)
{
char *tmpcom;
if (idx < 0 || idx >= za->nentry
|| len < 0 || len > MAXCOMLEN
|| (len > 0 && comment == NULL)) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if (len > 0) {
if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
return -1;
}
else
tmpcom = NULL;
free(za->entry[idx].ch_comment);
za->entry[idx].ch_comment = tmpcom;
za->entry[idx].ch_comment_len = len;
return 0;
}
ZIP_EXTERN struct zip_source *
zip_source_file(struct zip *za, const char *fname, myoff_t start, myoff_t len)
{
if (za == NULL)
return NULL;
if (fname == NULL || start < 0 || len < -1) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
return _zip_source_file_or_p(za, fname, NULL, start, len);
}
struct read_data {
const char *buf, *data, *end;
time_t mtime;
int freep;
};
static ssize_t read_data(void *state, void *data, size_t len,
enum zip_source_cmd cmd);
ZIP_EXTERN struct zip_source *
zip_source_buffer(struct zip *za, const void *data, myoff_t len, int freep)
{
struct read_data *f;
struct zip_source *zs;
if (za == NULL)
return NULL;
if (len < 0 || (data == NULL && len > 0)) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
f->data = (const char *)data;
f->end = ((const char *)data)+len;
f->freep = freep;
f->mtime = time(NULL);
if ((zs=zip_source_function(za, read_data, f)) == NULL) {
free(f);
return NULL;
}
return zs;
}
static ssize_t
read_data(void *state, void *data, size_t len, enum zip_source_cmd cmd)
{
struct read_data *z;
char *buf;
size_t n;
z = (struct read_data *)state;
buf = (char *)data;
switch (cmd) {
case ZIP_SOURCE_OPEN:
z->buf = z->data;
return 0;
case ZIP_SOURCE_READ:
n = z->end - z->buf;
if (n > len)
n = len;
if (n) {
memcpy(buf, z->buf, n);
z->buf += n;
}
return n;
case ZIP_SOURCE_CLOSE:
return 0;
case ZIP_SOURCE_STAT:
{
struct zip_stat *st;
if (len < sizeof(*st))
return -1;
st = (struct zip_stat *)data;
zip_stat_init(st);
st->mtime = z->mtime;
st->size = z->end - z->data;
return sizeof(*st);
}
case ZIP_SOURCE_ERROR:
{
int *e;
if (len < sizeof(int)*2)
return -1;
e = (int *)data;
e[0] = e[1] = 0;
}
return sizeof(int)*2;
case ZIP_SOURCE_FREE:
if (z->freep) {
free((void *)z->data);
z->data = NULL;
}
free(z);
return 0;
default:
;
}
return -1;
}
int
_zip_set_name(struct zip *za, int idx, const char *name)
{
char *s;
int i;
if (idx < 0 || idx >= za->nentry || name == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if ((i=_zip_name_locate(za, name, 0, NULL)) != -1 && i != idx) {
_zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
return -1;
}
/* no effective name change */
if (i == idx)
return 0;
if ((s=strdup(name)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return -1;
}
if (za->entry[idx].state == ZIP_ST_UNCHANGED)
za->entry[idx].state = ZIP_ST_RENAMED;
free(za->entry[idx].ch_filename);
za->entry[idx].ch_filename = s;
return 0;
}
ZIP_EXTERN int
zip_set_archive_flag(struct zip *za, int flag, int value)
{
if (value)
za->ch_flags |= flag;
else
za->ch_flags &= ~flag;
return 0;
}
void
_zip_unchange_data(struct zip_entry *ze)
{
if (ze->source) {
(void)ze->source->f(ze->source->ud, NULL, 0, ZIP_SOURCE_FREE);
free(ze->source);
ze->source = NULL;
}
ze->state = ze->ch_filename ? ZIP_ST_RENAMED : ZIP_ST_UNCHANGED;
}
ZIP_EXTERN int
zip_unchange_archive(struct zip *za)
{
free(za->ch_comment);
za->ch_comment = NULL;
za->ch_comment_len = -1;
za->ch_flags = za->flags;
return 0;
}
ZIP_EXTERN int
zip_unchange(struct zip *za, int idx)
{
return _zip_unchange(za, idx, 0);
}
int
_zip_unchange(struct zip *za, int idx, int allow_duplicates)
{
int i;
if (idx < 0 || idx >= za->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if (za->entry[idx].ch_filename) {
if (!allow_duplicates) {
i = _zip_name_locate(za,
_zip_get_name(za, idx, ZIP_FL_UNCHANGED, NULL),
0, NULL);
if (i != -1 && i != idx) {
_zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
return -1;
}
}
free(za->entry[idx].ch_filename);
za->entry[idx].ch_filename = NULL;
}
free(za->entry[idx].ch_comment);
za->entry[idx].ch_comment = NULL;
za->entry[idx].ch_comment_len = -1;
_zip_unchange_data(za->entry+idx);
return 0;
}
ZIP_EXTERN int
zip_unchange_all(struct zip *za)
{
int ret, i;
ret = 0;
for (i=0; i<za->nentry; i++)
ret |= _zip_unchange(za, i, 1);
ret |= zip_unchange_archive(za);
return ret;
}
ZIP_EXTERN int
zip_set_archive_comment(struct zip *za, const char *comment, int len)
{
char *tmpcom;
if (len < 0 || len > MAXCOMLEN
|| (len > 0 && comment == NULL)) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if (len > 0) {
if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
return -1;
}
else
tmpcom = NULL;
free(za->ch_comment);
za->ch_comment = tmpcom;
za->ch_comment_len = len;
return 0;
}
ZIP_EXTERN int
zip_replace(struct zip *za, int idx, struct zip_source *source)
{
if (idx < 0 || idx >= za->nentry || source == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if (_zip_replace(za, idx, NULL, source) == -1)
return -1;
return 0;
}
int
_zip_replace(struct zip *za, int idx, const char *name,
struct zip_source *source)
{
if (idx == -1) {
if (_zip_entry_new(za) == NULL)
return -1;
idx = za->nentry - 1;
}
_zip_unchange_data(za->entry+idx);
if (name && _zip_set_name(za, idx, name) != 0)
return -1;
za->entry[idx].state = ((za->cdir == NULL || idx >= za->cdir->nentry)
? ZIP_ST_ADDED : ZIP_ST_REPLACED);
za->entry[idx].source = source;
return idx;
}
ZIP_EXTERN int
zip_rename(struct zip *za, int idx, const char *name)
{
const char *old_name;
int old_is_dir, new_is_dir;
if (idx >= za->nentry || idx < 0 || name[0] == '\0') {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if ((old_name=zip_get_name(za, idx, 0)) == NULL)
return -1;
new_is_dir = (name[strlen(name)-1] == '/');
old_is_dir = (old_name[strlen(old_name)-1] == '/');
if (new_is_dir != old_is_dir) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
return _zip_set_name(za, idx, name);
}
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void set_error(int *, struct zip_error *, int);
static struct zip *_zip_allocate_new(const char *, int *);
static int _zip_checkcons(FILE *, struct zip_cdir *, struct zip_error *);
static void _zip_check_torrentzip(struct zip *);
static struct zip_cdir *_zip_find_central_dir(FILE *, int, int *, myoff_t);
static int _zip_file_exists(const char *, int, int *);
static int _zip_headercomp(struct zip_dirent *, int,
struct zip_dirent *, int);
static unsigned char *_zip_memmem(const unsigned char *, int,
const unsigned char *, int);
static struct zip_cdir *_zip_readcdir(FILE *, unsigned char *, unsigned char *,
int, int, struct zip_error *);
ZIP_EXTERN struct zip *
zip_open(const char *fn, int flags, int *zep)
{
FILE *fp;
struct zip *za;
struct zip_cdir *cdir;
int i;
myoff_t len;
switch (_zip_file_exists(fn, flags, zep)) {
case -1:
return NULL;
case 0:
return _zip_allocate_new(fn, zep);
default:
break;
}
if ((fp=fopen(fn, "rb")) == NULL) {
set_error(zep, NULL, ZIP_ER_OPEN);
return NULL;
}
fseeko(fp, 0, SEEK_END);
len = ftello(fp);
/* treat empty files as empty archives */
if (len == 0) {
if ((za=_zip_allocate_new(fn, zep)) == NULL)
fclose(fp);
else
za->zp = fp;
return za;
}
cdir = _zip_find_central_dir(fp, flags, zep, len);
if (cdir == NULL) {
fclose(fp);
return NULL;
}
if ((za=_zip_allocate_new(fn, zep)) == NULL) {
_zip_cdir_free(cdir);
fclose(fp);
return NULL;
}
za->cdir = cdir;
za->zp = fp;
if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry))
* cdir->nentry)) == NULL) {
set_error(zep, NULL, ZIP_ER_MEMORY);
_zip_free(za);
return NULL;
}
for (i=0; i<cdir->nentry; i++)
_zip_entry_new(za);
_zip_check_torrentzip(za);
za->ch_flags = za->flags;
return za;
}
static void
set_error(int *zep, struct zip_error *err, int ze)
{
int se;
if (err) {
_zip_error_get(err, &ze, &se);
if (zip_error_get_sys_type(ze) == ZIP_ET_SYS)
errno = se;
}
if (zep)
*zep = ze;
}
/* _zip_readcdir:
tries to find a valid end-of-central-directory at the beginning of
buf, and then the corresponding central directory entries.
Returns a struct zip_cdir which contains the central directory
entries, or NULL if unsuccessful. */
static struct zip_cdir *
_zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen,
int flags, struct zip_error *error)
{
struct zip_cdir *cd;
unsigned char *cdp, **bufp;
int i, comlen, nentry;
comlen = buf + buflen - eocd - EOCDLEN;
if (comlen < 0) {
/* not enough bytes left for comment */
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return NULL;
}
/* check for end-of-central-dir magic */
if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return NULL;
}
if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
_zip_error_set(error, ZIP_ER_MULTIDISK, 0);
return NULL;
}
cdp = eocd + 8;
/* number of cdir-entries on this disk */
i = _zip_read2(&cdp);
/* number of cdir-entries */
nentry = _zip_read2(&cdp);
if ((cd=_zip_cdir_new(nentry, error)) == NULL)
return NULL;
cd->size = _zip_read4(&cdp);
cd->offset = _zip_read4(&cdp);
cd->comment = NULL;
cd->comment_len = _zip_read2(&cdp);
if ((comlen < cd->comment_len) || (cd->nentry != i)) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
free(cd);
return NULL;
}
if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) {
_zip_error_set(error, ZIP_ER_INCONS, 0);
free(cd);
return NULL;
}
if (cd->comment_len) {
if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN,
cd->comment_len, error))
== NULL) {
free(cd);
return NULL;
}
}
cdp = eocd;
if (cd->size < (unsigned int)(eocd-buf)) {
/* if buffer already read in, use it */
cdp = eocd - cd->size;
bufp = &cdp;
}
else {
/* go to start of cdir and read it entry by entry */
bufp = NULL;
clearerr(fp);
fseeko(fp, cd->offset, SEEK_SET);
/* possible consistency check: cd->offset =
len-(cd->size+cd->comment_len+EOCDLEN) ? */
if (ferror(fp) || ((unsigned long)ftello(fp) != cd->offset)) {
/* seek error or offset of cdir wrong */
if (ferror(fp))
_zip_error_set(error, ZIP_ER_SEEK, errno);
else
_zip_error_set(error, ZIP_ER_NOZIP, 0);
free(cd);
return NULL;
}
}
for (i=0; i<cd->nentry; i++) {
if ((_zip_dirent_read(cd->entry+i, fp, bufp, eocd-cdp, 0,
error)) < 0) {
cd->nentry = i;
_zip_cdir_free(cd);
return NULL;
}
}
return cd;
}
/* _zip_checkcons:
Checks the consistency of the central directory by comparing central
directory entries with local headers and checking for plausible
file and header offsets. Returns -1 if not plausible, else the
difference between the lowest and the highest fileposition reached */
static int
_zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error)
{
int i;
unsigned int min, max, j;
struct zip_dirent temp;
if (cd->nentry) {
max = cd->entry[0].offset;
min = cd->entry[0].offset;
}
else
min = max = 0;
for (i=0; i<cd->nentry; i++) {
if (cd->entry[i].offset < min)
min = cd->entry[i].offset;
if (min > cd->offset) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return -1;
}
j = cd->entry[i].offset + cd->entry[i].comp_size
+ cd->entry[i].filename_len + LENTRYSIZE;
if (j > max)
max = j;
if (max > cd->offset) {
_zip_error_set(error, ZIP_ER_NOZIP, 0);
return -1;
}
if (fseeko(fp, cd->entry[i].offset, SEEK_SET) != 0) {
_zip_error_set(error, ZIP_ER_SEEK, 0);
return -1;
}
if (_zip_dirent_read(&temp, fp, NULL, 0, 1, error) == -1)
return -1;
if (_zip_headercomp(cd->entry+i, 0, &temp, 1) != 0) {
_zip_error_set(error, ZIP_ER_INCONS, 0);
_zip_dirent_finalize(&temp);
return -1;
}
_zip_dirent_finalize(&temp);
}
return max - min;
}
/* _zip_check_torrentzip:
check wether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */
static void
_zip_check_torrentzip(struct zip *za)
{
uLong crc_got, crc_should;
char buf[8+1];
char *end;
if (za->zp == NULL || za->cdir == NULL)
return;
if (za->cdir->comment_len != TORRENT_SIG_LEN+8
|| strncmp(za->cdir->comment, TORRENT_SIG, TORRENT_SIG_LEN) != 0)
return;
memcpy(buf, za->cdir->comment+TORRENT_SIG_LEN, 8);
buf[8] = '\0';
errno = 0;
crc_should = strtoul(buf, &end, 16);
if ((crc_should == UINT_MAX && errno != 0) || (end && *end))
return;
if (_zip_filerange_crc(za->zp, za->cdir->offset, za->cdir->size,
&crc_got, NULL) < 0)
return;
if (crc_got == crc_should)
za->flags |= ZIP_AFL_TORRENT;
}
/* _zip_headercomp:
compares two headers h1 and h2; if they are local headers, set
local1p or local2p respectively to 1, else 0. Return 0 if they
are identical, -1 if not. */
static int
_zip_headercomp(struct zip_dirent *h1, int local1p, struct zip_dirent *h2,
int local2p)
{
if ((h1->version_needed != h2->version_needed)
#if 0
/* some zip-files have different values in local
and global headers for the bitflags */
|| (h1->bitflags != h2->bitflags)
#endif
|| (h1->comp_method != h2->comp_method)
|| (h1->last_mod != h2->last_mod)
|| (h1->filename_len != h2->filename_len)
|| !h1->filename || !h2->filename
|| strcmp(h1->filename, h2->filename))
return -1;
/* check that CRC and sizes are zero if data descriptor is used */
if ((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local1p
&& (h1->crc != 0
|| h1->comp_size != 0
|| h1->uncomp_size != 0))
return -1;
if ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local2p
&& (h2->crc != 0
|| h2->comp_size != 0
|| h2->uncomp_size != 0))
return -1;
/* check that CRC and sizes are equal if no data descriptor is used */
if (((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local1p == 0)
&& ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local2p == 0)) {
if ((h1->crc != h2->crc)
|| (h1->comp_size != h2->comp_size)
|| (h1->uncomp_size != h2->uncomp_size))
return -1;
}
if ((local1p == local2p)
&& ((h1->extrafield_len != h2->extrafield_len)
|| (h1->extrafield_len && h2->extrafield
&& memcmp(h1->extrafield, h2->extrafield,
h1->extrafield_len))))
return -1;
/* if either is local, nothing more to check */
if (local1p || local2p)
return 0;
if ((h1->version_madeby != h2->version_madeby)
|| (h1->disk_number != h2->disk_number)
|| (h1->int_attrib != h2->int_attrib)
|| (h1->ext_attrib != h2->ext_attrib)
|| (h1->offset != h2->offset)
|| (h1->comment_len != h2->comment_len)
|| (h1->comment_len && h2->comment
&& memcmp(h1->comment, h2->comment, h1->comment_len)))
return -1;
return 0;
}
static struct zip *
_zip_allocate_new(const char *fn, int *zep)
{
struct zip *za;
struct zip_error error;
if ((za=_zip_new(&error)) == NULL) {
set_error(zep, &error, 0);
return NULL;
}
za->zn = strdup(fn);
if (!za->zn) {
_zip_free(za);
set_error(zep, NULL, ZIP_ER_MEMORY);
return NULL;
}
return za;
}
static int
_zip_file_exists(const char *fn, int flags, int *zep)
{
struct stat st;
if (fn == NULL) {
set_error(zep, NULL, ZIP_ER_INVAL);
return -1;
}
if (stat(fn, &st) != 0) {
if (flags & ZIP_CREATE)
return 0;
else {
set_error(zep, NULL, ZIP_ER_OPEN);
return -1;
}
}
else if ((flags & ZIP_EXCL)) {
set_error(zep, NULL, ZIP_ER_EXISTS);
return -1;
}
/* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL,
just like open() */
return 1;
}
static struct zip_cdir *
_zip_find_central_dir(FILE *fp, int flags, int *zep, myoff_t len)
{
struct zip_cdir *cdir, *cdirnew;
unsigned char *buf, *match;
int a, best, buflen, i;
struct zip_error zerr;
i = fseeko(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END);
if (i == -1 && errno != EFBIG) {
/* seek before start of file on my machine */
set_error(zep, NULL, ZIP_ER_SEEK);
return NULL;
}
/* 64k is too much for stack */
if ((buf=(unsigned char *)malloc(CDBUFSIZE)) == NULL) {
set_error(zep, NULL, ZIP_ER_MEMORY);
return NULL;
}
clearerr(fp);
buflen = fread(buf, 1, CDBUFSIZE, fp);
if (ferror(fp)) {
set_error(zep, NULL, ZIP_ER_READ);
free(buf);
return NULL;
}
best = -1;
cdir = NULL;
match = buf;
_zip_error_set(&zerr, ZIP_ER_NOZIP, 0);
while ((match=_zip_memmem(match, buflen-(match-buf)-18,
(const unsigned char *)EOCD_MAGIC, 4))!=NULL) {
/* found match -- check, if good */
/* to avoid finding the same match all over again */
match++;
if ((cdirnew=_zip_readcdir(fp, buf, match-1, buflen, flags,
&zerr)) == NULL)
continue;
if (cdir) {
if (best <= 0)
best = _zip_checkcons(fp, cdir, &zerr);
a = _zip_checkcons(fp, cdirnew, &zerr);
if (best < a) {
_zip_cdir_free(cdir);
cdir = cdirnew;
best = a;
}
else
_zip_cdir_free(cdirnew);
}
else {
cdir = cdirnew;
if (flags & ZIP_CHECKCONS)
best = _zip_checkcons(fp, cdir, &zerr);
else
best = 0;
}
cdirnew = NULL;
}
free(buf);
if (best < 0) {
set_error(zep, &zerr, 0);
_zip_cdir_free(cdir);
return NULL;
}
return cdir;
}
static unsigned char *
_zip_memmem(const unsigned char *big, int biglen, const unsigned char *little,
int littlelen)
{
const unsigned char *p;
if ((biglen < littlelen) || (littlelen == 0))
return NULL;
p = big-1;
while ((p=(const unsigned char *)
memchr(p+1, little[0], (size_t)(big-(p+1)+biglen-littlelen+1)))
!= NULL) {
if (memcmp(p+1, little+1, littlelen-1)==0)
return (unsigned char *)p;
}
return NULL;
}
/* _zip_new:
creates a new zipfile struct, and sets the contents to zero; returns
the new struct. */
struct zip *
_zip_new(struct zip_error *error)
{
struct zip *za;
za = (struct zip *)malloc(sizeof(struct zip));
if (!za) {
_zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
za->zn = NULL;
za->zp = NULL;
_zip_error_init(&za->error);
za->cdir = NULL;
za->ch_comment = NULL;
za->ch_comment_len = -1;
za->nentry = za->nentry_alloc = 0;
za->entry = NULL;
za->nfile = za->nfile_alloc = 0;
za->file = NULL;
za->flags = za->ch_flags = 0;
return za;
}
void *
_zip_memdup(const void *mem, size_t len, struct zip_error *error)
{
void *ret;
ret = malloc(len);
if (!ret) {
_zip_error_set(error, ZIP_ER_MEMORY, 0);
return NULL;
}
memcpy(ret, mem, len);
return ret;
}
ZIP_EXTERN int
zip_get_num_files(struct zip *za)
{
if (za == NULL)
return -1;
return za->nentry;
}
ZIP_EXTERN const char *
zip_get_name(struct zip *za, int idx, int flags)
{
return _zip_get_name(za, idx, flags, &za->error);
}
const char *
_zip_get_name(struct zip *za, int idx, int flags, struct zip_error *error)
{
if (idx < 0 || idx >= za->nentry) {
_zip_error_set(error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((flags & ZIP_FL_UNCHANGED) == 0) {
if (za->entry[idx].state == ZIP_ST_DELETED) {
_zip_error_set(error, ZIP_ER_DELETED, 0);
return NULL;
}
if (za->entry[idx].ch_filename)
return za->entry[idx].ch_filename;
}
if (za->cdir == NULL || idx >= za->cdir->nentry) {
_zip_error_set(error, ZIP_ER_INVAL, 0);
return NULL;
}
return za->cdir->entry[idx].filename;
}
ZIP_EXTERN const char *
zip_get_file_comment(struct zip *za, int idx, int *lenp, int flags)
{
if (idx < 0 || idx >= za->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((flags & ZIP_FL_UNCHANGED)
|| (za->entry[idx].ch_comment_len == -1)) {
if (lenp != NULL)
*lenp = za->cdir->entry[idx].comment_len;
return za->cdir->entry[idx].comment;
}
if (lenp != NULL)
*lenp = za->entry[idx].ch_comment_len;
return za->entry[idx].ch_comment;
}
ZIP_EXTERN int
zip_get_archive_flag(struct zip *za, int flag, int flags)
{
int fl;
fl = (flags & ZIP_FL_UNCHANGED) ? za->flags : za->ch_flags;
return (fl & flag) ? 1 : 0;
}
ZIP_EXTERN const char *
zip_get_archive_comment(struct zip *za, int *lenp, int flags)
{
if ((flags & ZIP_FL_UNCHANGED)
|| (za->ch_comment_len == -1)) {
if (za->cdir) {
if (lenp != NULL)
*lenp = za->cdir->comment_len;
return za->cdir->comment;
}
else {
if (lenp != NULL)
*lenp = -1;
return NULL;
}
}
if (lenp != NULL)
*lenp = za->ch_comment_len;
return za->ch_comment;
}
/* _zip_free:
frees the space allocated to a zipfile struct, and closes the
corresponding file. */
void
_zip_free(struct zip *za)
{
int i;
if (za == NULL)
return;
if (za->zn)
free(za->zn);
if (za->zp)
fclose(za->zp);
_zip_cdir_free(za->cdir);
if (za->entry) {
for (i=0; i<za->nentry; i++) {
_zip_entry_free(za->entry+i);
}
free(za->entry);
}
for (i=0; i<za->nfile; i++) {
if (za->file[i]->error.zip_err == ZIP_ER_OK) {
_zip_error_set(&za->file[i]->error, ZIP_ER_ZIPCLOSED, 0);
za->file[i]->za = NULL;
}
}
free(za->file);
free(za);
return;
}
ZIP_EXTERN ssize_t
zip_fread(struct zip_file *zf, void *outbuf, size_t toread)
{
int ret;
size_t out_before, len;
int i;
if (!zf)
return -1;
if (zf->error.zip_err != 0)
return -1;
if ((zf->flags & ZIP_ZF_EOF) || (toread == 0))
return 0;
if (zf->bytes_left == 0) {
zf->flags |= ZIP_ZF_EOF;
if (zf->flags & ZIP_ZF_CRC) {
if (zf->crc != zf->crc_orig) {
_zip_error_set(&zf->error, ZIP_ER_CRC, 0);
return -1;
}
}
return 0;
}
if ((zf->flags & ZIP_ZF_DECOMP) == 0) {
ret = _zip_file_fillbuf(outbuf, toread, zf);
if (ret > 0) {
if (zf->flags & ZIP_ZF_CRC)
zf->crc = crc32(zf->crc, (Bytef *)outbuf, ret);
zf->bytes_left -= ret;
}
return ret;
}
zf->zstr->next_out = (Bytef *)outbuf;
zf->zstr->avail_out = toread;
out_before = zf->zstr->total_out;
/* endless loop until something has been accomplished */
for (;;) {
ret = inflate(zf->zstr, Z_SYNC_FLUSH);
switch (ret) {
case Z_OK:
case Z_STREAM_END:
/* all ok */
/* Z_STREAM_END probably won't happen, since we didn't
have a header */
len = zf->zstr->total_out - out_before;
if (len >= zf->bytes_left || len >= toread) {
if (zf->flags & ZIP_ZF_CRC)
zf->crc = crc32(zf->crc, (Bytef *)outbuf, len);
zf->bytes_left -= len;
return len;
}
break;
case Z_BUF_ERROR:
if (zf->zstr->avail_in == 0) {
i = _zip_file_fillbuf(zf->buffer, BUFSIZE, zf);
if (i == 0) {
_zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
return -1;
}
else if (i < 0)
return -1;
zf->zstr->next_in = (Bytef *)zf->buffer;
zf->zstr->avail_in = i;
continue;
}
/* fallthrough */
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_STREAM_ERROR:
case Z_MEM_ERROR:
_zip_error_set(&zf->error, ZIP_ER_ZLIB, ret);
return -1;
}
}
}
ZIP_EXTERN const char *
zip_strerror(struct zip *za)
{
return _zip_error_strerror(&za->error);
}
ZIP_EXTERN void
zip_stat_init(struct zip_stat *st)
{
st->name = NULL;
st->index = -1;
st->crc = 0;
st->mtime = (time_t)-1;
st->size = -1;
st->comp_size = -1;
st->comp_method = ZIP_CM_STORE;
st->encryption_method = ZIP_EM_NONE;
}
ZIP_EXTERN int
zip_stat_index(struct zip *za, int index, int flags, struct zip_stat *st)
{
const char *name;
if (index < 0 || index >= za->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
if ((name=zip_get_name(za, index, flags)) == NULL)
return -1;
if ((flags & ZIP_FL_UNCHANGED) == 0
&& ZIP_ENTRY_DATA_CHANGED(za->entry+index)) {
if (za->entry[index].source->f(za->entry[index].source->ud,
st, sizeof(*st), ZIP_SOURCE_STAT) < 0) {
_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
return -1;
}
}
else {
if (za->cdir == NULL || index >= za->cdir->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return -1;
}
st->crc = za->cdir->entry[index].crc;
st->size = za->cdir->entry[index].uncomp_size;
st->mtime = za->cdir->entry[index].last_mod;
st->comp_size = za->cdir->entry[index].comp_size;
st->comp_method = za->cdir->entry[index].comp_method;
if (za->cdir->entry[index].bitflags & ZIP_GPBF_ENCRYPTED) {
if (za->cdir->entry[index].bitflags & ZIP_GPBF_STRONG_ENCRYPTION) {
/* XXX */
st->encryption_method = ZIP_EM_UNKNOWN;
}
else
st->encryption_method = ZIP_EM_TRAD_PKWARE;
}
else
st->encryption_method = ZIP_EM_NONE;
/* st->bitflags = za->cdir->entry[index].bitflags; */
}
st->index = index;
st->name = name;
return 0;
}
ZIP_EXTERN int
zip_stat(struct zip *za, const char *fname, int flags, struct zip_stat *st)
{
int idx;
if ((idx=zip_name_locate(za, fname, flags)) < 0)
return -1;
return zip_stat_index(za, idx, flags, st);
}
struct read_zip {
struct zip_file *zf;
struct zip_stat st;
myoff_t off, len;
};
static ssize_t read_zip(void *st, void *data, size_t len,
enum zip_source_cmd cmd);
ZIP_EXTERN struct zip_source *
zip_source_zip(struct zip *za, struct zip *srcza, int srcidx, int flags,
myoff_t start, myoff_t len)
{
struct zip_error error;
struct zip_source *zs;
struct read_zip *p;
/* XXX: ZIP_FL_RECOMPRESS */
if (za == NULL)
return NULL;
if (srcza == NULL || start < 0 || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((flags & ZIP_FL_UNCHANGED) == 0
&& ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
return NULL;
}
if (len == 0)
len = -1;
if (start == 0 && len == -1 && (flags & ZIP_FL_RECOMPRESS) == 0)
flags |= ZIP_FL_COMPRESSED;
else
flags &= ~ZIP_FL_COMPRESSED;
if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
_zip_error_copy(&error, &srcza->error);
if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
|| (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
free(p);
_zip_error_copy(&za->error, &srcza->error);
_zip_error_copy(&srcza->error, &error);
return NULL;
}
p->off = start;
p->len = len;
if ((flags & ZIP_FL_COMPRESSED) == 0) {
p->st.size = p->st.comp_size = len;
p->st.comp_method = ZIP_CM_STORE;
p->st.crc = 0;
}
if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
free(p);
return NULL;
}
return zs;
}
static ssize_t
read_zip(void *state, void *data, size_t len, enum zip_source_cmd cmd)
{
struct read_zip *z;
char b[8192], *buf;
int i, n;
z = (struct read_zip *)state;
buf = (char *)data;
switch (cmd) {
case ZIP_SOURCE_OPEN:
for (n=0; n<z->off; n+= i) {
i = (z->off-n > sizeof(b) ? sizeof(b) : z->off-n);
if ((i=zip_fread(z->zf, b, i)) < 0) {
zip_fclose(z->zf);
z->zf = NULL;
return -1;
}
}
return 0;
case ZIP_SOURCE_READ:
if (z->len != -1)
n = len > z->len ? z->len : len;
else
n = len;
if ((i=zip_fread(z->zf, buf, n)) < 0)
return -1;
if (z->len != -1)
z->len -= i;
return i;
case ZIP_SOURCE_CLOSE:
return 0;
case ZIP_SOURCE_STAT:
if (len < sizeof(z->st))
return -1;
len = sizeof(z->st);
memcpy(data, &z->st, len);
return len;
case ZIP_SOURCE_ERROR:
{
int *e;
if (len < sizeof(int)*2)
return -1;
e = (int *)data;
zip_file_error_get(z->zf, e, e+1);
}
return sizeof(int)*2;
case ZIP_SOURCE_FREE:
zip_fclose(z->zf);
free(z);
return 0;
default:
;
}
return -1;
}
ZIP_EXTERN struct zip_source *
zip_source_function(struct zip *za, zip_source_callback zcb, void *ud)
{
struct zip_source *zs;
if (za == NULL)
return NULL;
if ((zs=(struct zip_source *)malloc(sizeof(*zs))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
zs->f = zcb;
zs->ud = ud;
return zs;
}
ZIP_EXTERN void
zip_source_free(struct zip_source *source)
{
if (source == NULL)
return;
(void)source->f(source->ud, NULL, 0, ZIP_SOURCE_FREE);
free(source);
}
struct read_file {
char *fname; /* name of file to copy from */
FILE *f; /* file to copy from */
myoff_t off; /* start offset of */
myoff_t len; /* lengt of data to copy */
myoff_t remain; /* bytes remaining to be copied */
int e[2]; /* error codes */
};
static ssize_t read_file(void *state, void *data, size_t len,
enum zip_source_cmd cmd);
ZIP_EXTERN struct zip_source *
zip_source_filep(struct zip *za, FILE *file, myoff_t start, myoff_t len)
{
if (za == NULL)
return NULL;
if (file == NULL || start < 0 || len < -1) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
return _zip_source_file_or_p(za, NULL, file, start, len);
}
struct zip_source *
_zip_source_file_or_p(struct zip *za, const char *fname, FILE *file,
myoff_t start, myoff_t len)
{
struct read_file *f;
struct zip_source *zs;
if (file == NULL && fname == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
f->fname = NULL;
if (fname) {
if ((f->fname=strdup(fname)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
free(f);
return NULL;
}
}
f->f = file;
f->off = start;
f->len = (len ? len : -1);
if ((zs=zip_source_function(za, read_file, f)) == NULL) {
free(f);
return NULL;
}
return zs;
}
static ssize_t
read_file(void *state, void *data, size_t len, enum zip_source_cmd cmd)
{
struct read_file *z;
char *buf;
int i, n;
z = (struct read_file *)state;
buf = (char *)data;
switch (cmd) {
case ZIP_SOURCE_OPEN:
if (z->fname) {
if ((z->f=fopen(z->fname, "rb")) == NULL) {
z->e[0] = ZIP_ER_OPEN;
z->e[1] = errno;
return -1;
}
}
if (fseeko(z->f, z->off, SEEK_SET) < 0) {
z->e[0] = ZIP_ER_SEEK;
z->e[1] = errno;
return -1;
}
z->remain = z->len;
return 0;
case ZIP_SOURCE_READ:
if (z->remain != -1)
n = len > z->remain ? z->remain : len;
else
n = len;
if ((i=fread(buf, 1, n, z->f)) < 0) {
z->e[0] = ZIP_ER_READ;
z->e[1] = errno;
return -1;
}
if (z->remain != -1)
z->remain -= i;
return i;
case ZIP_SOURCE_CLOSE:
if (z->fname) {
fclose(z->f);
z->f = NULL;
}
return 0;
case ZIP_SOURCE_STAT:
{
struct zip_stat *st;
struct stat fst;
int err;
if (len < sizeof(*st))
return -1;
if (z->f)
err = fstat(fileno(z->f), &fst);
else
err = stat(z->fname, &fst);
if (err != 0) {
z->e[0] = ZIP_ER_READ; /* best match */
z->e[1] = errno;
return -1;
}
st = (struct zip_stat *)data;
zip_stat_init(st);
st->mtime = fst.st_mtime;
if (z->len != -1)
st->size = z->len;
else if ((fst.st_mode&S_IFMT) == S_IFREG)
st->size = fst.st_size;
return sizeof(*st);
}
case ZIP_SOURCE_ERROR:
if (len < sizeof(int)*2)
return -1;
memcpy(data, z->e, sizeof(int)*2);
return sizeof(int)*2;
case ZIP_SOURCE_FREE:
free(z->fname);
if (z->f)
fclose(z->f);
free(z);
return 0;
default:
;
}
return -1;
}
ZIP_EXTERN int
zip_name_locate(struct zip *za, const char *fname, int flags)
{
return _zip_name_locate(za, fname, flags, &za->error);
}
int
_zip_name_locate(struct zip *za, const char *fname, int flags,
struct zip_error *error)
{
int (*cmp)(const char *, const char *);
const char *fn, *p;
int i, n;
if (fname == NULL) {
_zip_error_set(error, ZIP_ER_INVAL, 0);
return -1;
}
cmp = (flags & ZIP_FL_NOCASE) ? strcasecmp : strcmp;
n = (flags & ZIP_FL_UNCHANGED) ? za->cdir->nentry : za->nentry;
for (i=0; i<n; i++) {
if (flags & ZIP_FL_UNCHANGED)
fn = za->cdir->entry[i].filename;
else
fn = _zip_get_name(za, i, flags, error);
/* newly added (partially filled) entry */
if (fn == NULL)
continue;
if (flags & ZIP_FL_NODIR) {
p = strrchr(fn, '/');
if (p)
fn = p+1;
}
if (cmp(fname, fn) == 0)
return i;
}
_zip_error_set(error, ZIP_ER_NOENT, 0);
return -1;
}
|