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
|
/** Network Kanji Filter. (PDS Version)
************************************************************************
** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
** $BO"Mm@h!'(B $B!J3t!KIY;NDL8&5f=j!!%=%U%H#38&!!;T@n!!;j(B
** $B!J(BE-Mail Address: ichikawa@flab.fujitsu.co.jp$B!K(B
** Copyright (C) 1996,1998
** Copyright (C) 2002
** $BO"Mm@h!'(B $BN05eBg3X>pJs9)3X2J(B $B2OLn(B $B??<#(B mime/X0208 support
** $B!J(BE-Mail Address: kono@ie.u-ryukyu.ac.jp$B!K(B
** $BO"Mm@h!'(B COW for DOS & Win16 & Win32 & OS/2
** $B!J(BE-Mail Address: GHG00637@niftyserve.or.p$B!K(B
**
** $B$3$N%=!<%9$N$$$+$J$kJ#<L!$2~JQ!$=$@5$b5vBz$7$^$9!#$?$@$7!"(B
** $B$=$N:]$K$O!"C/$,9W8%$7$?$r<($9$3$NItJ,$r;D$9$3$H!#(B
** $B:FG[I[$d;(;o$NIUO?$J$I$NLd$$9g$o$;$bI,MW$"$j$^$;$s!#(B
** $B1DMxMxMQ$b>e5-$KH?$7$J$$HO0O$G5v2D$7$^$9!#(B
** $B%P%$%J%j$NG[I[$N:]$K$O(Bversion message$B$rJ]B8$9$k$3$H$r>r7o$H$7$^$9!#(B
** $B$3$N%W%m%0%i%`$K$D$$$F$OFC$K2?$NJ]>Z$b$7$J$$!"0-$7$+$i$:!#(B
**
** Everyone is permitted to do anything on this program
** including copying, modifying, improving,
** as long as you don't try to pretend that you wrote it.
** i.e., the above copyright notice has to appear in all copies.
** Binary distribution requires original version messages.
** You don't have to ask before copying, redistribution or publishing.
** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
***********************************************************************/
/***********************************************************************
** UTF-8 $B%5%]!<%H$K$D$$$F(B
** $B=>Mh$N(B nkf $B$HF~$l$+$($F$=$N$^$^;H$($k$h$&$K$J$C$F$$$^$9(B
** nkf -e $B$J$I$H$7$F5/F0$9$k$H!"<+F0H=JL$G(B UTF-8 $B$HH=Dj$5$l$l$P!"(B
** $B$=$N$^$^(B euc-jp $B$KJQ49$5$l$^$9(B
**
** $B$^$@%P%0$,$"$k2DG=@-$,9b$$$G$9!#(B
** ($BFC$K<+F0H=JL!"%3!<%I:.:_!"%(%i!<=hM}7O(B)
**
** $B2?$+LdBj$r8+$D$1$?$i!"(B
** E-Mail: furukawa@tcp-ip.or.jp
** $B$^$G8fO"Mm$r$*4j$$$7$^$9!#(B
***********************************************************************/
#include "config.h"
static char *CopyRight =
"Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW, 2002-2004 Kono, Furukawa";
static char *Version =
"2.0";
static char *Patchlevel =
"4/0401/Shinji Kono";
/*
**
**
**
** USAGE: nkf [flags] [file]
**
** Flags:
** b Output is buffered (DEFAULT)
** u Output is unbuffered
**
** t no operation
**
** j Outout code is JIS 7 bit (DEFAULT SELECT)
** s Output code is MS Kanji (DEFAULT SELECT)
** e Output code is AT&T JIS (DEFAULT SELECT)
** w Output code is AT&T JIS (DEFAULT SELECT)
** l Output code is JIS 7bit and ISO8859-1 Latin-1
**
** m MIME conversion for ISO-2022-JP
** I Convert non ISO-2022-JP charactor to GETA by Pekoe <pekoe@lair.net>
** i_ Output sequence to designate JIS-kanji (DEFAULT_J)
** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)
** M MIME output conversion
**
** r {de/en}crypt ROT13/47
**
** v display Version
**
** T Text mode output (for MS-DOS)
**
** x Do not convert X0201 kana into X0208
** Z Convert X0208 alphabet to ASCII
**
** f60 fold option
**
** m MIME decode
** B try to fix broken JIS, missing Escape
** B[1-9] broken level
**
** O Output to 'nkf.out' file or last file name
** d Delete \r in line feed
** c Add \r in line feed
** -- other long option
** -- ignore following option (don't use with -O )
**
**/
#if (defined(__TURBOC__) || defined(_MSC_VER) || defined(LSI_C) || defined(__MINGW32__)) && !defined(MSDOS)
#define MSDOS
#if (defined(__Win32__) || defined(_WIN32)) && !defined(__WIN32__)
#define __WIN32__
#endif
#endif
#ifdef PERL_XS
#undef OVERWRITE
#endif
#ifndef PERL_XS
#include <stdio.h>
#endif
#if defined(MSDOS) || defined(__OS2__)
#include <fcntl.h>
#include <io.h>
#endif
#ifdef MSDOS
#ifdef LSI_C
#define setbinmode(fp) fsetbin(fp)
#else /* Microsoft C, Turbo C */
#define setbinmode(fp) setmode(fileno(fp), O_BINARY)
#endif
#else /* UNIX,OS/2 */
#define setbinmode(fp)
#endif
#ifdef _IOFBF /* SysV and MSDOS, Windows */
#define setvbuffer(fp, buf, size) setvbuf(fp, buf, _IOFBF, size)
#else /* BSD */
#define setvbuffer(fp, buf, size) setbuffer(fp, buf, size)
#endif
/*Borland C++ 4.5 EasyWin*/
#if defined(__TURBOC__) && defined(_Windows) && !defined(__WIN32__) /*Easy Win */
#define EASYWIN
#ifndef __WIN16__
#define __WIN16__
#endif
#include <windows.h>
#endif
#ifdef OVERWRITE
/* added by satoru@isoternet.org */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifndef MSDOS /* UNIX, OS/2 */
#include <unistd.h>
#include <utime.h>
#else
#if defined(_MSC_VER) || defined(__MINGW32__) /* VC++, MinGW */
#include <sys/utime.h>
#elif defined(__TURBOC__) /* BCC */
#include <utime.h>
#elif defined(LSI_C) /* LSI C */
#endif
#endif
#endif
#ifdef INT_IS_SHORT
#define int long
#endif
#define FALSE 0
#define TRUE 1
/* state of output_mode and input_mode
c2 0 means ASCII
X0201
ISO8859_1
X0208
EOF all termination
c1 32bit data
*/
#define ASCII 0
#define X0208 1
#define X0201 2
#define ISO8859_1 8
#define NO_X0201 3
/* Input Assumption */
#define JIS_INPUT 4
#define SJIS_INPUT 5
#define LATIN1_INPUT 6
#define FIXED_MIME 7
#define STRICT_MIME 8
/* MIME ENCODE */
#define ISO2022JP 9
#define JAPANESE_EUC 10
#define SHIFT_JIS 11
#define UTF8 12
#define UTF8_INPUT 13
#define UTF16_INPUT 14
#define UTF16BE_INPUT 15
#define WISH_TRUE 15
/* ASCII CODE */
#define BS 0x08
#define NL 0x0a
#define CR 0x0d
#define ESC 0x1b
#define SPACE 0x20
#define AT 0x40
#define SSP 0xa0
#define DEL 0x7f
#define SI 0x0f
#define SO 0x0e
#define SSO 0x8e
#define is_alnum(c) \
(('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
#define HOLD_SIZE 1024
#define IOBUF_SIZE 16384
#define DEFAULT_J 'B'
#define DEFAULT_R 'B'
#define SJ0162 0x00e1 /* 01 - 62 ku offset */
#define SJ6394 0x0161 /* 63 - 94 ku offset */
#define RANGE_NUM_MAX 18
#define GETA1 0x22
#define GETA2 0x2e
#if defined( UTF8_OUTPUT_ENABLE ) || defined( UTF8_INPUT_ENABLE )
#define sizeof_euc_utf8 94
#define sizeof_euc_to_utf8_1byte 94
#define sizeof_euc_to_utf8_2bytes 94
#define sizeof_utf8_to_euc_C2 64
#define sizeof_utf8_to_euc_E5B8 64
#define sizeof_utf8_to_euc_2bytes 112
#define sizeof_utf8_to_euc_3bytes 112
#endif
/* MIME preprocessor */
#ifdef EASYWIN /*Easy Win */
extern POINT _BufferSize;
#endif
/* function prototype */
#ifdef ANSI_C_PROTOTYPE
#define PROTO(x) x
#define STATIC static
#else
#define PROTO(x) ()
#define STATIC
#endif
struct input_code{
char *name;
int stat;
int score;
int index;
int buf[3];
void (*status_func)PROTO((struct input_code *, int));
int (*iconv_func)PROTO((int c2, int c1, int c0));
int _file_stat;
};
STATIC char *input_codename = "";
STATIC int noconvert PROTO((FILE *f));
STATIC int kanji_convert PROTO((FILE *f));
STATIC int h_conv PROTO((FILE *f,int c2,int c1));
STATIC int push_hold_buf PROTO((int c2));
STATIC void set_iconv PROTO((int f, int (*iconv_func)()));
STATIC int s_iconv PROTO((int c2,int c1,int c0));
STATIC int s2e_conv PROTO((int c2, int c1, int *p2, int *p1));
STATIC int e_iconv PROTO((int c2,int c1,int c0));
#ifdef UTF8_INPUT_ENABLE
STATIC int w2e_conv PROTO((int c2,int c1,int c0,int *p2,int *p1));
STATIC int w_iconv PROTO((int c2,int c1,int c0));
STATIC int w_iconv16 PROTO((int c2,int c1,int c0));
STATIC int w_iconv_common PROTO((int c1,int c0,unsigned short **pp,int psize,int *p2,int *p1));
STATIC int ww16_conv PROTO((int c2, int c1, int c0));
#endif
#ifdef UTF8_OUTPUT_ENABLE
STATIC int e2w_conv PROTO((int c2,int c1));
STATIC void w_oconv PROTO((int c2,int c1));
STATIC void w_oconv16 PROTO((int c2,int c1));
#endif
STATIC void e_oconv PROTO((int c2,int c1));
STATIC void e2s_conv PROTO((int c2, int c1, int *p2, int *p1));
STATIC void s_oconv PROTO((int c2,int c1));
STATIC void j_oconv PROTO((int c2,int c1));
STATIC void fold_conv PROTO((int c2,int c1));
STATIC void cr_conv PROTO((int c2,int c1));
STATIC void z_conv PROTO((int c2,int c1));
STATIC void rot_conv PROTO((int c2,int c1));
STATIC void hira_conv PROTO((int c2,int c1));
STATIC void base64_conv PROTO((int c2,int c1));
STATIC void iso2022jp_check_conv PROTO((int c2,int c1));
STATIC void no_connection PROTO((int c2,int c1));
STATIC int no_connection2 PROTO((int c2,int c1,int c0));
STATIC void code_score PROTO((struct input_code *ptr));
STATIC void code_status PROTO((int c));
STATIC void std_putc PROTO((int c));
STATIC int std_getc PROTO((FILE *f));
STATIC int std_ungetc PROTO((int c,FILE *f));
STATIC int broken_getc PROTO((FILE *f));
STATIC int broken_ungetc PROTO((int c,FILE *f));
STATIC int mime_begin PROTO((FILE *f));
STATIC int mime_getc PROTO((FILE *f));
STATIC int mime_ungetc PROTO((int c,FILE *f));
STATIC int mime_begin_strict PROTO((FILE *f));
STATIC int mime_getc_buf PROTO((FILE *f));
STATIC int mime_ungetc_buf PROTO((int c,FILE *f));
STATIC int mime_integrity PROTO((FILE *f,unsigned char *p));
STATIC int base64decode PROTO((int c));
STATIC void mime_putc PROTO((int c));
STATIC void open_mime PROTO((int c));
STATIC void close_mime PROTO(());
STATIC void usage PROTO(());
STATIC void version PROTO(());
STATIC void options PROTO((unsigned char *c));
#ifdef PERL_XS
STATIC void reinit PROTO(());
#endif
/* buffers */
static unsigned char stdibuf[IOBUF_SIZE];
static unsigned char stdobuf[IOBUF_SIZE];
static unsigned char hold_buf[HOLD_SIZE*2];
static int hold_count;
/* MIME preprocessor fifo */
#define MIME_BUF_SIZE (1024) /* 2^n ring buffer */
#define MIME_BUF_MASK (MIME_BUF_SIZE-1)
#define Fifo(n) mime_buf[(n)&MIME_BUF_MASK]
static unsigned char mime_buf[MIME_BUF_SIZE];
static unsigned int mime_top = 0;
static unsigned int mime_last = 0; /* decoded */
static unsigned int mime_input = 0; /* undecoded */
/* flags */
static int unbuf_f = FALSE;
static int estab_f = FALSE;
static int nop_f = FALSE;
static int binmode_f = TRUE; /* binary mode */
static int rot_f = FALSE; /* rot14/43 mode */
static int hira_f = FALSE; /* hira/kata henkan */
static int input_f = FALSE; /* non fixed input code */
static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */
static int mime_f = STRICT_MIME; /* convert MIME B base64 or Q */
static int mimebuf_f = FALSE; /* MIME buffered input */
static int broken_f = FALSE; /* convert ESC-less broken JIS */
static int iso8859_f = FALSE; /* ISO8859 through */
static int mimeout_f = FALSE; /* base64 mode */
#if defined(MSDOS) || defined(__OS2__)
static int x0201_f = TRUE; /* Assume JISX0201 kana */
#else
static int x0201_f = NO_X0201; /* Assume NO JISX0201 */
#endif
static int iso2022jp_f = FALSE; /* convert ISO-2022-JP */
#ifdef UTF8_OUTPUT_ENABLE
static int w_oconv16_begin_f= 0; /* utf-16 header */
static int w_oconv16_LE = 0; /* utf-16 little endian */
#endif
#ifdef NUMCHAR_OPTION
#define CLASS_MASK 0x0f000000
#define CLASS_UTF16 0x01000000
#endif
#ifdef INPUT_OPTION
static int cap_f = FALSE;
static int (*i_cgetc)PROTO((FILE *)) = std_getc; /* input of cgetc */
static int (*i_cungetc)PROTO((int c ,FILE *f)) = std_ungetc;
STATIC int cap_getc PROTO((FILE *f));
STATIC int cap_ungetc PROTO((int c,FILE *f));
static int url_f = FALSE;
static int (*i_ugetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
static int (*i_uungetc)PROTO((int c ,FILE *f)) = std_ungetc;
STATIC int url_getc PROTO((FILE *f));
STATIC int url_ungetc PROTO((int c,FILE *f));
static int numchar_f = FALSE;
static int (*i_ngetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
static int (*i_nungetc)PROTO((int c ,FILE *f)) = std_ungetc;
STATIC int numchar_getc PROTO((FILE *f));
STATIC int numchar_ungetc PROTO((int c,FILE *f));
#endif
#ifdef CHECK_OPTION
static int noout_f = FALSE;
STATIC void no_putc PROTO((int c));
static int debug_f = FALSE;
STATIC void debug PROTO((char *str));
#endif
static int guess_f = FALSE;
STATIC void print_guessed_code PROTO((char *filename));
STATIC void set_input_codename PROTO((char *codename));
static int is_inputcode_mixed = FALSE;
static int is_inputcode_set = FALSE;
#ifdef EXEC_IO
static int exec_f = 0;
#endif
#ifdef SHIFTJIS_CP932
STATIC int cp932_f = TRUE;
#define CP932_TABLE_BEGIN (0xfa)
#define CP932_TABLE_END (0xfc)
STATIC int cp932inv_f = FALSE;
#define CP932INV_TABLE_BEGIN (0xed)
#define CP932INV_TABLE_END (0xee)
#endif /* SHIFTJIS_CP932 */
STATIC unsigned char prefix_table[256];
STATIC void e_status PROTO((struct input_code *, int));
STATIC void s_status PROTO((struct input_code *, int));
#ifdef UTF8_INPUT_ENABLE
STATIC void w_status PROTO((struct input_code *, int));
STATIC void w16_status PROTO((struct input_code *, int));
static int utf16_mode = UTF16_INPUT;
#endif
struct input_code input_code_list[] = {
{"EUC-JP", 0, 0, 0, {0, 0, 0}, e_status, e_iconv, 0},
{"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
{"UTF-8", 0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
{"UTF-16", 0, 0, 0, {0, 0, 0}, w16_status, w_iconv16, 0},
{0}
};
static int mimeout_mode = 0;
static int base64_count = 0;
/* X0208 -> ASCII converter */
/* fold parameter */
static int f_line = 0; /* chars in line */
static int f_prev = 0;
static int fold_preserve_f = FALSE; /* preserve new lines */
static int fold_f = FALSE;
static int fold_len = 0;
/* options */
static unsigned char kanji_intro = DEFAULT_J,
ascii_intro = DEFAULT_R;
/* Folding */
#define FOLD_MARGIN 10
#define DEFAULT_FOLD 60
static int fold_margin = FOLD_MARGIN;
/* converters */
#ifdef DEFAULT_CODE_JIS
# define DEFAULT_CONV j_oconv
#endif
#ifdef DEFAULT_CODE_SJIS
# define DEFAULT_CONV s_oconv
#endif
#ifdef DEFAULT_CODE_EUC
# define DEFAULT_CONV e_oconv
#endif
#ifdef DEFAULT_CODE_UTF8
# define DEFAULT_CONV w_oconv
#endif
/* process default */
static void (*output_conv)PROTO((int c2,int c1)) = DEFAULT_CONV;
static void (*oconv)PROTO((int c2,int c1)) = no_connection;
/* s_iconv or oconv */
static int (*iconv)PROTO((int c2,int c1,int c0)) = no_connection2;
static void (*o_zconv)PROTO((int c2,int c1)) = no_connection;
static void (*o_fconv)PROTO((int c2,int c1)) = no_connection;
static void (*o_crconv)PROTO((int c2,int c1)) = no_connection;
static void (*o_rot_conv)PROTO((int c2,int c1)) = no_connection;
static void (*o_hira_conv)PROTO((int c2,int c1)) = no_connection;
static void (*o_base64conv)PROTO((int c2,int c1)) = no_connection;
static void (*o_iso2022jp_check_conv)PROTO((int c2,int c1)) = no_connection;
/* static redirections */
static void (*o_putc)PROTO((int c)) = std_putc;
static int (*i_getc)PROTO((FILE *f)) = std_getc; /* general input */
static int (*i_ungetc)PROTO((int c,FILE *f)) =std_ungetc;
static int (*i_bgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
static int (*i_bungetc)PROTO((int c ,FILE *f)) = std_ungetc;
static void (*o_mputc)PROTO((int c)) = std_putc ; /* output of mputc */
static int (*i_mgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
static int (*i_mungetc)PROTO((int c ,FILE *f)) = std_ungetc;
/* for strict mime */
static int (*i_mgetc_buf)PROTO((FILE *)) = std_getc; /* input of mgetc_buf */
static int (*i_mungetc_buf)PROTO((int c,FILE *f)) = std_ungetc;
/* Global states */
static int output_mode = ASCII, /* output kanji mode */
input_mode = ASCII, /* input kanji mode */
shift_mode = FALSE; /* TRUE shift out, or X0201 */
static int mime_decode_mode = FALSE; /* MIME mode B base64, Q hex */
/* X0201 / X0208 conversion tables */
/* X0201 kana conversion table */
/* 90-9F A0-DF */
static
unsigned char cv[]= {
0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
0x00,0x00};
/* X0201 kana conversion table for daguten */
/* 90-9F A0-DF */
static
unsigned char dv[]= {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00};
/* X0201 kana conversion table for han-daguten */
/* 90-9F A0-DF */
static
unsigned char ev[]= {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00};
/* X0208 kigou conversion table */
/* 0x8140 - 0x819e */
static
unsigned char fv[] = {
0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
} ;
#define CRLF 1
static int file_out = FALSE;
#ifdef OVERWRITE
static int overwrite = FALSE;
#endif
static int crmode_f = 0; /* CR, NL, CRLF */
#ifdef EASYWIN /*Easy Win */
static int end_check;
#endif /*Easy Win */
#ifndef PERL_XS
int
main(argc, argv)
int argc;
char **argv;
{
FILE *fin;
unsigned char *cp;
char *outfname;
char *origfname;
#ifdef EASYWIN /*Easy Win */
_BufferSize.y = 400;/*Set Scroll Buffer Size*/
#endif
for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
cp = (unsigned char *)*argv;
options(cp);
#ifdef EXEC_IO
if (exec_f){
int fds[2], pid;
if (pipe(fds) < 0 || (pid = fork()) < 0){
abort();
}
if (pid == 0){
if (exec_f > 0){
close(fds[0]);
dup2(fds[1], 1);
}else{
close(fds[1]);
dup2(fds[0], 0);
}
execvp(argv[1], &argv[1]);
}
if (exec_f > 0){
close(fds[1]);
dup2(fds[0], 0);
}else{
close(fds[0]);
dup2(fds[1], 1);
}
argc = 0;
break;
}
#endif
}
if(x0201_f == WISH_TRUE)
x0201_f = ((!iso2022jp_f)? TRUE : NO_X0201);
if (binmode_f == TRUE)
#ifdef __OS2__
if (freopen("","wb",stdout) == NULL)
return (-1);
#else
setbinmode(stdout);
#endif
if (unbuf_f)
setbuf(stdout, (char *) NULL);
else
setvbuffer(stdout, stdobuf, IOBUF_SIZE);
if (argc == 0) {
if (binmode_f == TRUE)
#ifdef __OS2__
if (freopen("","rb",stdin) == NULL) return (-1);
#else
setbinmode(stdin);
#endif
setvbuffer(stdin, stdibuf, IOBUF_SIZE);
if (nop_f)
noconvert(stdin);
else {
kanji_convert(stdin);
if (guess_f) print_guessed_code(NULL);
}
} else {
int nfiles = argc;
while (argc--) {
if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
perror(*--argv);
return(-1);
} else {
#ifdef OVERWRITE
int fd;
int fd_backup;
#endif
/* reopen file for stdout */
if (file_out == TRUE) {
#ifdef OVERWRITE
if (overwrite){
outfname = malloc(strlen(origfname)
+ strlen(".nkftmpXXXXXX")
+ 1);
if (!outfname){
perror(origfname);
return -1;
}
strcpy(outfname, origfname);
#ifdef MSDOS
{
int i;
for (i = strlen(outfname); i; --i){
if (outfname[i - 1] == '/'
|| outfname[i - 1] == '\\'){
break;
}
}
outfname[i] = '\0';
}
strcat(outfname, "ntXXXXXX");
mktemp(outfname);
fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE);
#else
strcat(outfname, ".nkftmpXXXXXX");
fd = mkstemp(outfname);
#endif
if (fd < 0
|| (fd_backup = dup(fileno(stdout))) < 0
|| dup2(fd, fileno(stdout)) < 0
){
perror(origfname);
return -1;
}
}else
#endif
if(argc == 1 ) {
outfname = *argv++;
argc--;
} else {
outfname = "nkf.out";
}
if(freopen(outfname, "w", stdout) == NULL) {
perror (outfname);
return (-1);
}
if (binmode_f == TRUE) {
#ifdef __OS2__
if (freopen("","wb",stdout) == NULL)
return (-1);
#else
setbinmode(stdout);
#endif
}
}
if (binmode_f == TRUE)
#ifdef __OS2__
if (freopen("","rb",fin) == NULL)
return (-1);
#else
setbinmode(fin);
#endif
setvbuffer(fin, stdibuf, IOBUF_SIZE);
if (nop_f)
noconvert(fin);
else {
char *filename = NULL;
kanji_convert(fin);
if (nfiles > 1) filename = origfname;
if (guess_f) print_guessed_code(filename);
}
fclose(fin);
#ifdef OVERWRITE
if (overwrite) {
struct stat sb;
#if defined(MSDOS) && !defined(__MINGW32__)
time_t tb[2];
#else
struct utimbuf tb;
#endif
fflush(stdout);
close(fd);
if (dup2(fd_backup, fileno(stdout)) < 0){
perror("dup2");
}
if (stat(origfname, &sb)) {
fprintf(stderr, "Can't stat %s\n", origfname);
}
/* $B%Q!<%_%C%7%g%s$rI|85(B */
if (chmod(outfname, sb.st_mode)) {
fprintf(stderr, "Can't set permission %s\n", outfname);
}
/* $B%?%$%`%9%?%s%W$rI|85(B */
#if defined(MSDOS) && !defined(__MINGW32__)
tb[0] = tb[1] = sb.st_mtime;
if (utime(outfname, tb)) {
fprintf(stderr, "Can't set timestamp %s\n", outfname);
}
#else
tb.actime = sb.st_atime;
tb.modtime = sb.st_mtime;
if (utime(outfname, &tb)) {
fprintf(stderr, "Can't set timestamp %s\n", outfname);
}
#endif
#ifdef MSDOS
if (unlink(origfname)){
perror(origfname);
}
#endif
if (rename(outfname, origfname)) {
perror(origfname);
fprintf(stderr, "Can't rename %s to %s\n",
outfname, origfname);
}
free(outfname);
}
#endif
}
}
}
#ifdef EASYWIN /*Easy Win */
if (file_out == FALSE)
scanf("%d",&end_check);
else
fclose(stdout);
#else /* for Other OS */
if (file_out == TRUE)
fclose(stdout);
#endif
return (0);
}
#endif
static
struct {
char *name;
char *alias;
} long_option[] = {
{"base64","jMB"},
{"euc","e"},
{"euc-input","E"},
{"fj","jm"},
{"help","v"},
{"jis","j"},
{"jis-input","J"},
{"mac","sLm"},
{"mime","jM"},
{"mime-input","m"},
{"msdos","sLw"},
{"sjis","s"},
{"sjis-input","S"},
{"unix","eLu"},
{"version","V"},
{"windows","sLw"},
{"hiragana","h1"},
{"katakana","h2"},
{"katakana-hiragana","h3"},
{"guess", "g"},
#ifdef UTF8_OUTPUT_ENABLE
{"utf8", "w"},
{"utf16", "w16"},
#endif
#ifdef UTF8_INPUT_ENABLE
{"utf8-input", "W"},
{"utf16-input", "W16"},
#endif
#ifdef OVERWRITE
{"overwrite", ""},
#endif
#ifdef INPUT_OPTION
{"cap-input", ""},
{"url-input", ""},
#endif
#ifdef NUMCHAR_OPTION
{"numchar-input", ""},
#endif
#ifdef CHECK_OPTION
{"no-output", ""},
{"debug", ""},
#endif
#ifdef SHIFTJIS_CP932
{"no-cp932", ""},
{"cp932inv", ""},
#endif
#ifdef EXEC_IO
{"exec-in", ""},
{"exec-out", ""},
#endif
{"prefix=", ""},
};
static int option_mode;
void
options(cp)
unsigned char *cp;
{
int i;
unsigned char *p;
if (option_mode==1)
return;
if (*cp++ != '-')
return;
while (*cp) {
switch (*cp++) {
case '-': /* literal options */
if (!*cp) { /* ignore the rest of arguments */
option_mode = 1;
return;
}
for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
int j;
p = (unsigned char *)long_option[i].name;
for (j=0;*p && (*p != '=') && *p == cp[j];p++, j++);
if (*p == cp[j]){
p = &cp[j];
break;
}
p = 0;
}
if (p == 0) return;
cp = (unsigned char *)long_option[i].alias;
if (!*cp){
#ifdef OVERWRITE
if (strcmp(long_option[i].name, "overwrite") == 0){
file_out = TRUE;
overwrite = TRUE;
continue;
}
#endif
#ifdef INPUT_OPTION
if (strcmp(long_option[i].name, "cap-input") == 0){
cap_f = TRUE;
continue;
}
if (strcmp(long_option[i].name, "url-input") == 0){
url_f = TRUE;
continue;
}
#endif
#ifdef NUMCHAR_OPTION
if (strcmp(long_option[i].name, "numchar-input") == 0){
numchar_f = TRUE;
continue;
}
#endif
#ifdef CHECK_OPTION
if (strcmp(long_option[i].name, "no-output") == 0){
noout_f = TRUE;
continue;
}
if (strcmp(long_option[i].name, "debug") == 0){
debug_f = TRUE;
continue;
}
#endif
#ifdef SHIFTJIS_CP932
if (strcmp(long_option[i].name, "no-cp932") == 0){
cp932_f = FALSE;
continue;
}
if (strcmp(long_option[i].name, "cp932inv") == 0){
cp932inv_f = TRUE;
continue;
}
#endif
#ifdef EXEC_IO
if (strcmp(long_option[i].name, "exec-in") == 0){
exec_f = 1;
return;
}
if (strcmp(long_option[i].name, "exec-out") == 0){
exec_f = -1;
return;
}
#endif
if (strcmp(long_option[i].name, "prefix=") == 0){
if (*p == '=' && ' ' < p[1] && p[1] < 128){
for (i = 2; ' ' < p[i] && p[i] < 128; i++){
prefix_table[p[i]] = p[1];
}
}
continue;
}
}
continue;
case 'b': /* buffered mode */
unbuf_f = FALSE;
continue;
case 'u': /* non bufferd mode */
unbuf_f = TRUE;
continue;
case 't': /* transparent mode */
nop_f = TRUE;
continue;
case 'j': /* JIS output */
case 'n':
output_conv = j_oconv;
continue;
case 'e': /* AT&T EUC output */
output_conv = e_oconv;
continue;
case 's': /* SJIS output */
output_conv = s_oconv;
continue;
case 'l': /* ISO8859 Latin-1 support, no conversion */
iso8859_f = TRUE; /* Only compatible with ISO-2022-JP */
input_f = LATIN1_INPUT;
continue;
case 'i': /* Kanji IN ESC-$-@/B */
if (*cp=='@'||*cp=='B')
kanji_intro = *cp++;
continue;
case 'o': /* ASCII IN ESC-(-J/B */
if (*cp=='J'||*cp=='B'||*cp=='H')
ascii_intro = *cp++;
continue;
case 'h':
/*
bit:1 hira -> kata
bit:2 kata -> hira
*/
if ('9'>= *cp && *cp>='0')
hira_f |= (*cp++ -'0');
else
hira_f |= 1;
continue;
case 'r':
rot_f = TRUE;
continue;
#if defined(MSDOS) || defined(__OS2__)
case 'T':
binmode_f = FALSE;
continue;
#endif
#ifndef PERL_XS
case 'V':
version();
exit(1);
break;
case 'v':
usage();
exit(1);
break;
#endif
#ifdef UTF8_OUTPUT_ENABLE
case 'w': /* UTF-8 output */
if ('1'== cp[0] && '6'==cp[1]) {
output_conv = w_oconv16; cp+=2;
if (cp[0]=='L') {
w_oconv16_begin_f=2; cp++;
w_oconv16_LE = 1;
if (cp[0] == '0'){
w_oconv16_begin_f=1; cp++;
}
} else if (cp[0] == 'B') {
w_oconv16_begin_f=2; cp++;
if (cp[0] == '0'){
w_oconv16_begin_f=1; cp++;
}
}
} else
output_conv = w_oconv;
continue;
#endif
#ifdef UTF8_INPUT_ENABLE
case 'W': /* UTF-8 input */
if ('1'== cp[0] && '6'==cp[1]) {
input_f = UTF16_INPUT;
} else
input_f = UTF8_INPUT;
continue;
#endif
/* Input code assumption */
case 'J': /* JIS input */
case 'E': /* AT&T EUC input */
input_f = JIS_INPUT;
continue;
case 'S': /* MS Kanji input */
input_f = SJIS_INPUT;
if (x0201_f==NO_X0201) x0201_f=TRUE;
continue;
case 'Z': /* Convert X0208 alphabet to asii */
/* bit:0 Convert X0208
bit:1 Convert Kankaku to one space
bit:2 Convert Kankaku to two spaces
bit:3 Convert HTML Entity
*/
if ('9'>= *cp && *cp>='0')
alpha_f |= 1<<(*cp++ -'0');
else
alpha_f |= TRUE;
continue;
case 'x': /* Convert X0201 kana to X0208 or X0201 Conversion */
x0201_f = FALSE; /* No X0201->X0208 conversion */
/* accept X0201
ESC-(-I in JIS, EUC, MS Kanji
SI/SO in JIS, EUC, MS Kanji
SSO in EUC, JIS, not in MS Kanji
MS Kanji (0xa0-0xdf)
output X0201
ESC-(-I in JIS (0x20-0x5f)
SSO in EUC (0xa0-0xdf)
0xa0-0xd in MS Kanji (0xa0-0xdf)
*/
continue;
case 'X': /* Assume X0201 kana */
/* Default value is NO_X0201 for EUC/MS-Kanji mix */
x0201_f = TRUE;
continue;
case 'F': /* prserve new lines */
fold_preserve_f = TRUE;
case 'f': /* folding -f60 or -f */
fold_f = TRUE;
fold_len = 0;
while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
fold_len *= 10;
fold_len += *cp++ - '0';
}
if (!(0<fold_len && fold_len<BUFSIZ))
fold_len = DEFAULT_FOLD;
if (*cp=='-') {
fold_margin = 0;
cp++;
while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
fold_margin *= 10;
fold_margin += *cp++ - '0';
}
}
continue;
case 'm': /* MIME support */
if (*cp=='B'||*cp=='Q') {
mime_decode_mode = *cp++;
mimebuf_f = FIXED_MIME;
} else if (*cp=='N') {
mime_f = TRUE; cp++;
} else if (*cp=='S') {
mime_f = STRICT_MIME; cp++;
} else if (*cp=='0') {
mime_f = FALSE; cp++;
}
continue;
case 'M': /* MIME output */
if (*cp=='B') {
mimeout_mode = 'B';
mimeout_f = FIXED_MIME; cp++;
} else if (*cp=='Q') {
mimeout_mode = 'Q';
mimeout_f = FIXED_MIME; cp++;
} else {
mimeout_f = TRUE;
}
continue;
case 'B': /* Broken JIS support */
/* bit:0 no ESC JIS
bit:1 allow any x on ESC-(-x or ESC-$-x
bit:2 reset to ascii on NL
*/
if ('9'>= *cp && *cp>='0')
broken_f |= 1<<(*cp++ -'0');
else
broken_f |= TRUE;
continue;
#ifndef PERL_XS
case 'O':/* for Output file */
file_out = TRUE;
continue;
#endif
case 'c':/* add cr code */
crmode_f = CRLF;
continue;
case 'd':/* delete cr code */
crmode_f = NL;
continue;
case 'I': /* ISO-2022-JP output */
iso2022jp_f = TRUE;
continue;
case 'L': /* line mode */
if (*cp=='u') { /* unix */
crmode_f = NL; cp++;
} else if (*cp=='m') { /* mac */
crmode_f = CR; cp++;
} else if (*cp=='w') { /* windows */
crmode_f = CRLF; cp++;
} else if (*cp=='0') { /* no conversion */
crmode_f = 0; cp++;
}
continue;
case 'g':
#ifndef PERL_XS
guess_f = TRUE;
#endif
continue;
case ' ':
/* module muliple options in a string are allowed for Perl moudle */
while(*cp && *cp!='-') cp++;
if(*cp=='-') cp++;
continue;
default:
/* bogus option but ignored */
continue;
}
}
}
#ifdef ANSI_C_PROTOTYPE
struct input_code * find_inputcode_byfunc(int (*iconv_func)(int c2,int c1,int c0))
#else
struct input_code * find_inputcode_byfunc(iconv_func)
int (*iconv_func)();
#endif
{
if (iconv_func){
struct input_code *p = input_code_list;
while (p->name){
if (iconv_func == p->iconv_func){
return p;
}
p++;
}
}
return 0;
}
#ifdef ANSI_C_PROTOTYPE
void set_iconv(int f, int (*iconv_func)(int c2,int c1,int c0))
#else
void set_iconv(f, iconv_func)
int f;
int (*iconv_func)();
#endif
{
#ifdef CHECK_OPTION
static int (*iconv_for_check)() = 0;
#endif
#ifdef INPUT_CODE_FIX
if (f || !input_f)
#endif
if (estab_f != f){
estab_f = f;
}
if (iconv_func
#ifdef INPUT_CODE_FIX
&& (f == -TRUE || !input_f) /* -TRUE means "FORCE" */
#endif
){
iconv = iconv_func;
}
#ifdef CHECK_OPTION
if (estab_f && iconv_for_check != iconv){
struct input_code *p = find_inputcode_byfunc(iconv);
if (p){
set_input_codename(p->name);
debug(input_codename);
}
iconv_for_check = iconv;
}
#endif
}
#define SCORE_L2 (1) /* $BBh(B2$B?e=`4A;z(B */
#define SCORE_KANA (SCORE_L2 << 1) /* $B$$$o$f$kH>3Q%+%J(B */
#define SCORE_DEPEND (SCORE_KANA << 1) /* $B5!<o0MB8J8;z(B */
#ifdef SHIFTJIS_CP932
#define SCORE_CP932 (SCORE_DEPEND << 1) /* CP932 $B$K$h$kFI$_49$((B */
#define SCORE_NO_EXIST (SCORE_CP932 << 1) /* $BB8:_$7$J$$J8;z(B */
#else
#define SCORE_NO_EXIST (SCORE_DEPEND << 1) /* $BB8:_$7$J$$J8;z(B */
#endif
#define SCORE_iMIME (SCORE_NO_EXIST << 1) /* MIME $B$K$h$k;XDj(B */
#define SCORE_ERROR (SCORE_iMIME << 1) /* $B%(%i!<(B */
#define SCORE_INIT (SCORE_iMIME)
int score_table_A0[] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
};
int score_table_F0[] = {
SCORE_L2, SCORE_L2, SCORE_L2, SCORE_L2,
SCORE_L2, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST, SCORE_ERROR,
};
void set_code_score(ptr, score)
struct input_code *ptr;
int score;
{
if (ptr){
ptr->score |= score;
}
}
void clr_code_score(ptr, score)
struct input_code *ptr;
int score;
{
if (ptr){
ptr->score &= ~score;
}
}
void code_score(ptr)
struct input_code *ptr;
{
int c2 = ptr->buf[0];
int c1 = ptr->buf[1];
if (c2 < 0){
set_code_score(ptr, SCORE_ERROR);
}else if (c2 == SSO){
set_code_score(ptr, SCORE_KANA);
#ifdef UTF8_OUTPUT_ENABLE
}else if (!e2w_conv(c2, c1)){
set_code_score(ptr, SCORE_NO_EXIST);
#endif
}else if ((c2 & 0x70) == 0x20){
set_code_score(ptr, score_table_A0[c2 & 0x0f]);
}else if ((c2 & 0x70) == 0x70){
set_code_score(ptr, score_table_F0[c2 & 0x0f]);
}else if ((c2 & 0x70) >= 0x50){
set_code_score(ptr, SCORE_L2);
}
}
void status_disable(ptr)
struct input_code *ptr;
{
ptr->stat = -1;
ptr->buf[0] = -1;
code_score(ptr);
if (iconv == ptr->iconv_func) set_iconv(FALSE, 0);
}
void status_push_ch(ptr, c)
struct input_code *ptr;
int c;
{
ptr->buf[ptr->index++] = c;
}
void status_clear(ptr)
struct input_code *ptr;
{
ptr->stat = 0;
ptr->index = 0;
}
void status_reset(ptr)
struct input_code *ptr;
{
status_clear(ptr);
ptr->score = SCORE_INIT;
}
void status_reinit(ptr)
struct input_code *ptr;
{
status_reset(ptr);
ptr->_file_stat = 0;
}
void status_check(ptr, c)
struct input_code *ptr;
int c;
{
if (c <= DEL && estab_f){
status_reset(ptr);
}
}
void s_status(ptr, c)
struct input_code *ptr;
int c;
{
switch(ptr->stat){
case -1:
status_check(ptr, c);
break;
case 0:
if (c <= DEL){
break;
#ifdef NUMCHAR_OPTION
}else if ((c & CLASS_MASK) == CLASS_UTF16){
break;
#endif
}else if (0xa1 <= c && c <= 0xdf){
status_push_ch(ptr, SSO);
status_push_ch(ptr, c);
code_score(ptr);
status_clear(ptr);
}else if ((0x81 <= c && c < 0xa0) || (0xe0 <= c && c <= 0xef)){
ptr->stat = 1;
status_push_ch(ptr, c);
#ifdef SHIFTJIS_CP932
}else if (cp932_f
&& CP932_TABLE_BEGIN <= c && c <= CP932_TABLE_END){
ptr->stat = 2;
status_push_ch(ptr, c);
#endif /* SHIFTJIS_CP932 */
}else{
status_disable(ptr);
}
break;
case 1:
if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
status_push_ch(ptr, c);
s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]);
code_score(ptr);
status_clear(ptr);
}else{
status_disable(ptr);
}
break;
#ifdef SHIFTJIS_CP932
case 2:
if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
status_push_ch(ptr, c);
if (s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]) == 0){
set_code_score(ptr, SCORE_CP932);
status_clear(ptr);
break;
}
}
status_disable(ptr);
break;
#endif /* SHIFTJIS_CP932 */
}
}
void e_status(ptr, c)
struct input_code *ptr;
int c;
{
switch (ptr->stat){
case -1:
status_check(ptr, c);
break;
case 0:
if (c <= DEL){
break;
#ifdef NUMCHAR_OPTION
}else if ((c & CLASS_MASK) == CLASS_UTF16){
break;
#endif
}else if (SSO == c || (0xa1 <= c && c <= 0xfe)){
ptr->stat = 1;
status_push_ch(ptr, c);
}else{
status_disable(ptr);
}
break;
case 1:
if (0xa1 <= c && c <= 0xfe){
status_push_ch(ptr, c);
code_score(ptr);
status_clear(ptr);
}else{
status_disable(ptr);
}
break;
}
}
#ifdef UTF8_INPUT_ENABLE
void w16_status(ptr, c)
struct input_code *ptr;
int c;
{
switch (ptr->stat){
case -1:
break;
case 0:
if (ptr->_file_stat == 0){
if (c == 0xfe || c == 0xff){
ptr->stat = c;
status_push_ch(ptr, c);
ptr->_file_stat = 1;
}else{
status_disable(ptr);
ptr->_file_stat = -1;
}
}else if (ptr->_file_stat > 0){
ptr->stat = 1;
status_push_ch(ptr, c);
}else if (ptr->_file_stat < 0){
status_disable(ptr);
}
break;
case 1:
if (c == EOF){
status_disable(ptr);
ptr->_file_stat = -1;
}else{
status_push_ch(ptr, c);
status_clear(ptr);
}
break;
case 0xfe:
case 0xff:
if (ptr->stat != c && (c == 0xfe || c == 0xff)){
status_push_ch(ptr, c);
status_clear(ptr);
}else{
status_disable(ptr);
ptr->_file_stat = -1;
}
break;
}
}
void w_status(ptr, c)
struct input_code *ptr;
int c;
{
switch (ptr->stat){
case -1:
status_check(ptr, c);
break;
case 0:
if (c <= DEL){
break;
#ifdef NUMCHAR_OPTION
}else if ((c & CLASS_MASK) == CLASS_UTF16){
break;
#endif
}else if (0xc0 <= c && c <= 0xdf){
ptr->stat = 1;
status_push_ch(ptr, c);
}else if (0xe0 <= c && c <= 0xef){
ptr->stat = 2;
status_push_ch(ptr, c);
}else{
status_disable(ptr);
}
break;
case 1:
case 2:
if (0x80 <= c && c <= 0xbf){
status_push_ch(ptr, c);
if (ptr->index > ptr->stat){
int bom = (ptr->buf[0] == 0xef && ptr->buf[1] == 0xbb
&& ptr->buf[2] == 0xbf);
w2e_conv(ptr->buf[0], ptr->buf[1], ptr->buf[2],
&ptr->buf[0], &ptr->buf[1]);
if (!bom){
code_score(ptr);
}
status_clear(ptr);
}
}else{
status_disable(ptr);
}
break;
}
}
#endif
void
code_status(c)
int c;
{
int action_flag = 1;
struct input_code *result = 0;
struct input_code *p = input_code_list;
while (p->name){
(p->status_func)(p, c);
if (p->stat > 0){
action_flag = 0;
}else if(p->stat == 0){
if (result){
action_flag = 0;
}else{
result = p;
}
}
++p;
}
if (action_flag){
if (result && !estab_f){
set_iconv(TRUE, result->iconv_func);
}else if (c <= DEL){
struct input_code *ptr = input_code_list;
while (ptr->name){
status_reset(ptr);
++ptr;
}
}
}
}
#ifdef PERL_XS
#define STD_GC_BUFSIZE (256)
int std_gc_buf[STD_GC_BUFSIZE];
int std_gc_ndx;
#endif
int
std_getc(f)
FILE *f;
{
#ifdef PERL_XS
if (std_gc_ndx){
return std_gc_buf[--std_gc_ndx];
}
#endif
return getc(f);
}
int
std_ungetc(c,f)
int c;
FILE *f;
{
#ifdef PERL_XS
if (std_gc_ndx == STD_GC_BUFSIZE){
return EOF;
}
std_gc_buf[std_gc_ndx++] = c;
return c;
#endif
return ungetc(c,f);
}
void
std_putc(c)
int c;
{
if(c!=EOF)
putchar(c);
}
int
noconvert(f)
FILE *f;
{
int c;
while ((c = (*i_getc)(f)) != EOF)
(*o_putc)(c);
return 1;
}
void
module_connection()
{
oconv = output_conv;
o_putc = std_putc;
/* replace continucation module, from output side */
/* output redicrection */
#ifdef CHECK_OPTION
if (noout_f || guess_f){
o_putc = no_putc;
}
#endif
if (mimeout_f) {
o_mputc = o_putc;
o_putc = mime_putc;
if (mimeout_f == TRUE) {
o_base64conv = oconv; oconv = base64_conv;
}
/* base64_count = 0; */
}
if (crmode_f) {
o_crconv = oconv; oconv = cr_conv;
}
if (rot_f) {
o_rot_conv = oconv; oconv = rot_conv;
}
if (iso2022jp_f) {
o_iso2022jp_check_conv = oconv; oconv = iso2022jp_check_conv;
}
if (hira_f) {
o_hira_conv = oconv; oconv = hira_conv;
}
if (fold_f) {
o_fconv = oconv; oconv = fold_conv;
f_line = 0;
}
if (alpha_f || x0201_f) {
o_zconv = oconv; oconv = z_conv;
}
i_getc = std_getc;
i_ungetc = std_ungetc;
/* input redicrection */
#ifdef INPUT_OPTION
if (cap_f){
i_cgetc = i_getc; i_getc = cap_getc;
i_cungetc = i_ungetc; i_ungetc= cap_ungetc;
}
if (url_f){
i_ugetc = i_getc; i_getc = url_getc;
i_uungetc = i_ungetc; i_ungetc= url_ungetc;
}
#endif
#ifdef NUMCHAR_OPTION
if (numchar_f){
i_ngetc = i_getc; i_getc = numchar_getc;
i_nungetc = i_ungetc; i_ungetc= numchar_ungetc;
}
#endif
if (mime_f && mimebuf_f==FIXED_MIME) {
i_mgetc = i_getc; i_getc = mime_getc;
i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
}
if (broken_f & 1) {
i_bgetc = i_getc; i_getc = broken_getc;
i_bungetc = i_ungetc; i_ungetc = broken_ungetc;
}
if (input_f == JIS_INPUT || input_f == LATIN1_INPUT) {
set_iconv(-TRUE, e_iconv);
} else if (input_f == SJIS_INPUT) {
set_iconv(-TRUE, s_iconv);
#ifdef UTF8_INPUT_ENABLE
} else if (input_f == UTF8_INPUT) {
set_iconv(-TRUE, w_iconv);
} else if (input_f == UTF16_INPUT) {
set_iconv(-TRUE, w_iconv16);
#endif
} else {
set_iconv(FALSE, e_iconv);
}
{
struct input_code *p = input_code_list;
while (p->name){
status_reinit(p++);
}
}
}
/*
Conversion main loop. Code detection only.
*/
int
kanji_convert(f)
FILE *f;
{
int c1,
c2, c3;
module_connection();
c2 = 0;
input_mode = ASCII;
output_mode = ASCII;
shift_mode = FALSE;
#define NEXT continue /* no output, get next */
#define SEND ; /* output c1 and c2, get next */
#define LAST break /* end of loop, go closing */
while ((c1 = (*i_getc)(f)) != EOF) {
code_status(c1);
if (c2) {
/* second byte */
if (c2 > DEL) {
/* in case of 8th bit is on */
if (!estab_f) {
/* in case of not established yet */
/* It is still ambiguious */
if (h_conv(f, c2, c1)==EOF)
LAST;
else
c2 = 0;
NEXT;
} else
/* in case of already established */
if (c1 < AT) {
/* ignore bogus code */
c2 = 0;
NEXT;
} else
SEND;
} else
/* second byte, 7 bit code */
/* it might be kanji shitfted */
if ((c1 == DEL) || (c1 <= SPACE)) {
/* ignore bogus first code */
c2 = 0;
NEXT;
} else
SEND;
} else {
/* first byte */
if (
#ifdef UTF8_INPUT_ENABLE
iconv == w_iconv16
#else
0
#endif
) {
c2 = c1;
c1 = (*i_getc)(f);
SEND;
#ifdef NUMCHAR_OPTION
} else if ((c1 & CLASS_MASK) == CLASS_UTF16){
SEND;
#endif
} else if (c1 > DEL) {
/* 8 bit code */
if (!estab_f && !iso8859_f) {
/* not established yet */
c2 = c1;
NEXT;
} else { /* estab_f==TRUE */
if (iso8859_f) {
c2 = ISO8859_1;
c1 &= 0x7f;
SEND;
} else if (SSP<=c1 && c1<0xe0 && iconv == s_iconv) {
/* SJIS X0201 Case... */
if(iso2022jp_f && x0201_f==NO_X0201) {
(*oconv)(GETA1, GETA2);
NEXT;
} else {
c2 = X0201;
c1 &= 0x7f;
SEND;
}
} else if (c1==SSO && iconv != s_iconv) {
/* EUC X0201 Case */
c1 = (*i_getc)(f); /* skip SSO */
code_status(c1);
if (SSP<=c1 && c1<0xe0) {
if(iso2022jp_f && x0201_f==NO_X0201) {
(*oconv)(GETA1, GETA2);
NEXT;
} else {
c2 = X0201;
c1 &= 0x7f;
SEND;
}
} else { /* bogus code, skip SSO and one byte */
NEXT;
}
} else {
/* already established */
c2 = c1;
NEXT;
}
}
} else if ((c1 > SPACE) && (c1 != DEL)) {
/* in case of Roman characters */
if (shift_mode) {
/* output 1 shifted byte */
if (iso8859_f) {
c2 = ISO8859_1;
SEND;
} else if (SPACE<=c1 && c1<(0xe0&0x7f) ){
/* output 1 shifted byte */
if(iso2022jp_f && x0201_f==NO_X0201) {
(*oconv)(GETA1, GETA2);
NEXT;
} else {
c2 = X0201;
SEND;
}
} else {
/* look like bogus code */
NEXT;
}
} else if (input_mode == X0208) {
/* in case of Kanji shifted */
c2 = c1;
NEXT;
} else if (c1 == '=' && mime_f && !mime_decode_mode ) {
/* Check MIME code */
if ((c1 = (*i_getc)(f)) == EOF) {
(*oconv)(0, '=');
LAST;
} else if (c1 == '?') {
/* =? is mime conversion start sequence */
if(mime_f == STRICT_MIME) {
/* check in real detail */
if (mime_begin_strict(f) == EOF)
LAST;
else
NEXT;
} else if (mime_begin(f) == EOF)
LAST;
else
NEXT;
} else {
(*oconv)(0, '=');
(*i_ungetc)(c1,f);
NEXT;
}
} else {
/* normal ASCII code */
SEND;
}
} else if (c1 == SI) {
shift_mode = FALSE;
NEXT;
} else if (c1 == SO) {
shift_mode = TRUE;
NEXT;
} else if (c1 == ESC ) {
if ((c1 = (*i_getc)(f)) == EOF) {
/* (*oconv)(0, ESC); don't send bogus code */
LAST;
} else if (c1 == '$') {
if ((c1 = (*i_getc)(f)) == EOF) {
/*
(*oconv)(0, ESC); don't send bogus code
(*oconv)(0, '$'); */
LAST;
} else if (c1 == '@'|| c1 == 'B') {
/* This is kanji introduction */
input_mode = X0208;
shift_mode = FALSE;
set_input_codename("ISO-2022-JP");
debug(input_codename);
NEXT;
} else if (c1 == '(') {
if ((c1 = (*i_getc)(f)) == EOF) {
/* don't send bogus code
(*oconv)(0, ESC);
(*oconv)(0, '$');
(*oconv)(0, '(');
*/
LAST;
} else if (c1 == '@'|| c1 == 'B') {
/* This is kanji introduction */
input_mode = X0208;
shift_mode = FALSE;
NEXT;
} else {
/* could be some special code */
(*oconv)(0, ESC);
(*oconv)(0, '$');
(*oconv)(0, '(');
(*oconv)(0, c1);
NEXT;
}
} else if (broken_f&0x2) {
/* accept any ESC-(-x as broken code ... */
input_mode = X0208;
shift_mode = FALSE;
NEXT;
} else {
(*oconv)(0, ESC);
(*oconv)(0, '$');
(*oconv)(0, c1);
NEXT;
}
} else if (c1 == '(') {
if ((c1 = (*i_getc)(f)) == EOF) {
/* don't send bogus code
(*oconv)(0, ESC);
(*oconv)(0, '('); */
LAST;
} else {
if (c1 == 'I') {
/* This is X0201 kana introduction */
input_mode = X0201; shift_mode = X0201;
NEXT;
} else if (c1 == 'B' || c1 == 'J' || c1 == 'H') {
/* This is X0208 kanji introduction */
input_mode = ASCII; shift_mode = FALSE;
NEXT;
} else if (broken_f&0x2) {
input_mode = ASCII; shift_mode = FALSE;
NEXT;
} else {
(*oconv)(0, ESC);
(*oconv)(0, '(');
/* maintain various input_mode here */
SEND;
}
}
} else if ( c1 == 'N' || c1 == 'n' ){
/* SS2 */
c3 = (*i_getc)(f); /* skip SS2 */
if ( (SPACE<=c3 && c3 < 0x60) || (0xa0<=c3 && c3 < 0xe0)){
c1 = c3;
c2 = X0201;
SEND;
}else{
(*i_ungetc)(c3, f);
/* lonely ESC */
(*oconv)(0, ESC);
SEND;
}
} else {
/* lonely ESC */
(*oconv)(0, ESC);
SEND;
}
} else if ((c1 == NL || c1 == CR) && broken_f&4) {
input_mode = ASCII; set_iconv(FALSE, 0);
SEND;
} else
SEND;
}
/* send: */
if (input_mode == X0208)
(*oconv)(c2, c1); /* this is JIS, not SJIS/EUC case */
else if (input_mode)
(*oconv)(input_mode, c1); /* other special case */
else if ((*iconv)(c2, c1, 0) < 0){ /* can be EUC/SJIS */
int c0 = (*i_getc)(f);
if (c0 != EOF){
code_status(c0);
(*iconv)(c2, c1, c0);
}
}
c2 = 0;
continue;
/* goto next_word */
}
/* epilogue */
(*iconv)(EOF, 0, 0);
return 1;
}
int
h_conv(f, c2, c1)
FILE *f;
int c1,
c2;
{
int wc,c3;
/** it must NOT be in the kanji shifte sequence */
/** it must NOT be written in JIS7 */
/** and it must be after 2 byte 8bit code */
hold_count = 0;
push_hold_buf(c2);
push_hold_buf(c1);
c2 = 0;
while ((c1 = (*i_getc)(f)) != EOF) {
if (c1 == ESC){
(*i_ungetc)(c1,f);
break;
}
code_status(c1);
if (push_hold_buf(c1) == EOF || estab_f){
break;
}
}
if (!estab_f){
struct input_code *p = input_code_list;
struct input_code *result = p;
if (c1 == EOF){
code_status(c1);
}
while (p->name){
if (p->score < result->score){
result = p;
}
++p;
}
set_iconv(FALSE, result->iconv_func);
}
/** now,
** 1) EOF is detected, or
** 2) Code is established, or
** 3) Buffer is FULL (but last word is pushed)
**
** in 1) and 3) cases, we continue to use
** Kanji codes by oconv and leave estab_f unchanged.
**/
c3=c1;
wc = 0;
while (wc < hold_count){
c2 = hold_buf[wc++];
if (c2 <= DEL
#ifdef NUMCHAR_OPTION
|| (c2 & CLASS_MASK) == CLASS_UTF16
#endif
){
(*iconv)(0, c2, 0);
continue;
}else if (iconv == s_iconv && 0xa1 <= c2 && c2 <= 0xdf){
(*iconv)(X0201, c2, 0);
continue;
}
if (wc < hold_count){
c1 = hold_buf[wc++];
}else{
c1 = (*i_getc)(f);
if (c1 == EOF){
c3 = EOF;
break;
}
code_status(c1);
}
if ((*iconv)(c2, c1, 0) < 0){
int c0;
if (wc < hold_count){
c0 = hold_buf[wc++];
}else{
c0 = (*i_getc)(f);
if (c0 == EOF){
c3 = EOF;
break;
}
code_status(c0);
}
(*iconv)(c2, c1, c0);
c1 = c0;
}
}
return c3;
}
int
push_hold_buf(c2)
int c2;
{
if (hold_count >= HOLD_SIZE*2)
return (EOF);
hold_buf[hold_count++] = c2;
return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
}
int s2e_conv(c2, c1, p2, p1)
int c2, c1;
int *p2, *p1;
{
#ifdef SHIFTJIS_CP932
if (cp932_f && CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END){
extern unsigned short shiftjis_cp932[3][189];
c1 = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
if (c1 == 0) return 1;
c2 = c1 >> 8;
c1 &= 0xff;
}
#endif /* SHIFTJIS_CP932 */
c2 = c2 + c2 - ((c2 <= 0x9f) ? SJ0162 : SJ6394);
if (c1 < 0x9f)
c1 = c1 - ((c1 > DEL) ? SPACE : 0x1f);
else {
c1 = c1 - 0x7e;
c2++;
}
if (p2) *p2 = c2;
if (p1) *p1 = c1;
return 0;
}
int
s_iconv(c2, c1, c0)
int c2,
c1, c0;
{
if (c2 == X0201) {
c1 &= 0x7f;
} else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
/* NOP */
} else {
int ret = s2e_conv(c2, c1, &c2, &c1);
if (ret) return ret;
}
(*oconv)(c2, c1);
return 0;
}
int
e_iconv(c2, c1, c0)
int c2,
c1, c0;
{
if (c2 == X0201) {
c1 &= 0x7f;
} else if (c2 == SSO){
c2 = X0201;
c1 &= 0x7f;
} else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
/* NOP */
} else {
c1 &= 0x7f;
c2 &= 0x7f;
}
(*oconv)(c2, c1);
return 0;
}
#ifdef UTF8_INPUT_ENABLE
int
w2e_conv(c2, c1, c0, p2, p1)
int c2, c1, c0;
int *p2, *p1;
{
extern unsigned short * utf8_to_euc_2bytes[];
extern unsigned short ** utf8_to_euc_3bytes[];
int ret = 0;
if (0xc0 <= c2 && c2 <= 0xef) {
unsigned short **pp;
if (0xe0 <= c2) {
if (c0 == 0) return -1;
pp = utf8_to_euc_3bytes[c2 - 0x80];
ret = w_iconv_common(c1, c0, pp, sizeof_utf8_to_euc_C2, p2, p1);
} else {
ret = w_iconv_common(c2, c1, utf8_to_euc_2bytes, sizeof_utf8_to_euc_2bytes, p2, p1);
}
#ifdef NUMCHAR_OPTION
if (ret){
if (p2) *p2 = 0;
if (p1) *p1 = CLASS_UTF16 | ww16_conv(c2, c1, c0);
ret = 0;
}
#endif
return ret;
} else if (c2 == X0201) {
c1 &= 0x7f;
}
if (p2) *p2 = c2;
if (p1) *p1 = c1;
return ret;
}
int
w_iconv(c2, c1, c0)
int c2,
c1, c0;
{
int ret = w2e_conv(c2, c1, c0, &c2, &c1);
if (ret == 0){
(*oconv)(c2, c1);
}
return ret;
}
void
w16w_conv(val, p2, p1, p0)
unsigned short val;
int *p2, *p1, *p0;
{
if (val < 0x80){
*p2 = val;
*p1 = 0;
*p0 = 0;
}else if (val < 0x800){
*p2 = 0xc0 | (val >> 6);
*p1 = 0x80 | (val & 0x3f);
*p0 = 0;
}else{
*p2 = 0xe0 | (val >> 12);
*p1 = 0x80 | ((val >> 6) & 0x3f);
*p0 = 0x80 | (val & 0x3f);
}
}
int
ww16_conv(c2, c1, c0)
int c2, c1, c0;
{
unsigned short val;
if (c2 >= 0xe0){
val = (c2 & 0x0f) << 12;
val |= (c1 & 0x3f) << 6;
val |= (c0 & 0x3f);
}else if (c2 >= 0xc0){
val = (c2 & 0x1f) << 6;
val |= (c1 & 0x3f) << 6;
}else{
val = c2;
}
return val;
}
int
w16e_conv(val, p2, p1)
unsigned short val;
int *p2, *p1;
{
extern unsigned short * utf8_to_euc_2bytes[];
extern unsigned short ** utf8_to_euc_3bytes[];
int c2, c1, c0;
unsigned short **pp;
int psize;
int ret = 0;
w16w_conv(val, &c2, &c1, &c0);
if (c1){
if (c0){
pp = utf8_to_euc_3bytes[c2 - 0x80];
psize = sizeof_utf8_to_euc_C2;
ret = w_iconv_common(c1, c0, pp, psize, p2, p1);
}else{
pp = utf8_to_euc_2bytes;
psize = sizeof_utf8_to_euc_2bytes;
ret = w_iconv_common(c2, c1, pp, psize, p2, p1);
}
#ifdef NUMCHAR_OPTION
if (ret){
*p2 = 0;
*p1 = CLASS_UTF16 | val;
ret = 0;
}
#endif
}
return ret;
}
int
w_iconv16(c2, c1, c0)
int c2, c1,c0;
{
int ret;
if (c2==0376 && c1==0377){
utf16_mode = UTF16_INPUT;
return 0;
} else if (c2==0377 && c1==0376){
utf16_mode = UTF16BE_INPUT;
return 0;
}
if (c2 != EOF && utf16_mode == UTF16BE_INPUT) {
int tmp;
tmp=c1; c1=c2; c2=tmp;
}
if ((c2==0 && c1 < 0x80) || c2==EOF) {
(*oconv)(c2, c1);
return 0;
}
ret = w16e_conv(((c2<<8)&0xff00) + c1, &c2, &c1);
if (ret) return ret;
(*oconv)(c2, c1);
return 0;
}
int
w_iconv_common(c1, c0, pp, psize, p2, p1)
int c1,c0;
unsigned short **pp;
int psize;
int *p2, *p1;
{
int c2;
unsigned short *p ;
unsigned short val;
if (pp == 0) return 1;
c1 -= 0x80;
if (c1 < 0 || psize <= c1) return 1;
p = pp[c1];
if (p == 0) return 1;
c0 -= 0x80;
if (c0 < 0 || sizeof_utf8_to_euc_E5B8 <= c0) return 1;
val = p[c0];
if (val == 0) return 1;
c2 = val >> 8;
if (c2 == SO) c2 = X0201;
c1 = val & 0x7f;
if (p2) *p2 = c2;
if (p1) *p1 = c1;
return 0;
}
#endif
#ifdef UTF8_OUTPUT_ENABLE
int
e2w_conv(c2, c1)
int c2, c1;
{
extern unsigned short euc_to_utf8_1byte[];
extern unsigned short * euc_to_utf8_2bytes[];
unsigned short *p;
if (c2 == X0201) {
p = euc_to_utf8_1byte;
} else {
c2 &= 0x7f;
c2 = (c2&0x7f) - 0x21;
if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
p = euc_to_utf8_2bytes[c2];
else
return 0;
}
if (!p) return 0;
c1 = (c1 & 0x7f) - 0x21;
if (0<=c1 && c1<sizeof_euc_to_utf8_1byte)
return p[c1];
return 0;
}
void
w_oconv(c2, c1)
int c2,
c1;
{
int c0;
#ifdef NUMCHAR_OPTION
if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
w16w_conv(c1, &c2, &c1, &c0);
(*o_putc)(c2);
if (c1){
(*o_putc)(c1);
if (c0) (*o_putc)(c0);
}
}
#endif
if (c2 == EOF) {
(*o_putc)(EOF);
return;
} else if (c2 == 0) {
output_mode = ASCII;
(*o_putc)(c1);
} else if (c2 == ISO8859_1) {
output_mode = ISO8859_1;
(*o_putc)(c1 | 0x080);
} else {
output_mode = UTF8;
w16w_conv((unsigned short)e2w_conv(c2, c1), &c2, &c1, &c0);
(*o_putc)(c2);
if (c1){
(*o_putc)(c1);
if (c0) (*o_putc)(c0);
}
}
}
void
w_oconv16(c2, c1)
int c2,
c1;
{
if (c2 == EOF) {
(*o_putc)(EOF);
return;
}
if (w_oconv16_begin_f==2) {
if (w_oconv16_LE){
(*o_putc)((unsigned char)'\377');
(*o_putc)('\376');
}else{
(*o_putc)('\376');
(*o_putc)((unsigned char)'\377');
}
w_oconv16_begin_f=1;
}
if (c2 == ISO8859_1) {
c2 = 0;
c1 |= 0x80;
#ifdef NUMCHAR_OPTION
} else if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16) {
c2 = (c1 >> 8) & 0xff;
c1 &= 0xff;
#endif
} else if (c2) {
unsigned short val = (unsigned short)e2w_conv(c2, c1);
c2 = (val >> 8) & 0xff;
c1 = val & 0xff;
}
if (w_oconv16_LE){
(*o_putc)(c1);
(*o_putc)(c2);
}else{
(*o_putc)(c2);
(*o_putc)(c1);
}
}
#endif
void
e_oconv(c2, c1)
int c2,
c1;
{
#ifdef NUMCHAR_OPTION
if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
w16e_conv(c1, &c2, &c1);
}
#endif
if (c2 == EOF) {
(*o_putc)(EOF);
return;
} else if (c2 == 0) {
output_mode = ASCII;
(*o_putc)(c1);
} else if (c2 == X0201) {
output_mode = JAPANESE_EUC;
(*o_putc)(SSO); (*o_putc)(c1|0x80);
} else if (c2 == ISO8859_1) {
output_mode = ISO8859_1;
(*o_putc)(c1 | 0x080);
} else {
if ((c1<0x21 || 0x7e<c1) ||
(c2<0x21 || 0x7e<c2)) {
set_iconv(FALSE, 0);
return; /* too late to rescue this char */
}
output_mode = JAPANESE_EUC;
(*o_putc)(c2 | 0x080);
(*o_putc)(c1 | 0x080);
}
}
void
e2s_conv(c2, c1, p2, p1)
int c2, c1, *p2, *p1;
{
if (p2) *p2 = ((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1);
if (p1) *p1 = c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
}
void
s_oconv(c2, c1)
int c2,
c1;
{
#ifdef NUMCHAR_OPTION
if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
w16e_conv(c1, &c2, &c1);
}
#endif
if (c2 == EOF) {
(*o_putc)(EOF);
return;
} else if (c2 == 0) {
output_mode = ASCII;
(*o_putc)(c1);
} else if (c2 == X0201) {
output_mode = SHIFT_JIS;
(*o_putc)(c1|0x80);
} else if (c2 == ISO8859_1) {
output_mode = ISO8859_1;
(*o_putc)(c1 | 0x080);
} else {
if ((c1<0x20 || 0x7e<c1) ||
(c2<0x20 || 0x7e<c2)) {
set_iconv(FALSE, 0);
return; /* too late to rescue this char */
}
output_mode = SHIFT_JIS;
e2s_conv(c2, c1, &c2, &c1);
#ifdef SHIFTJIS_CP932
if (cp932inv_f
&& CP932INV_TABLE_BEGIN <= c2 && c2 <= CP932INV_TABLE_END){
extern unsigned short cp932inv[2][189];
int c = cp932inv[c2 - CP932INV_TABLE_BEGIN][c1 - 0x40];
if (c){
c2 = c >> 8;
c1 = c & 0xff;
}
}
#endif /* SHIFTJIS_CP932 */
(*o_putc)(c2);
if (prefix_table[(unsigned char)c1]){
(*o_putc)(prefix_table[(unsigned char)c1]);
}
(*o_putc)(c1);
}
}
void
j_oconv(c2, c1)
int c2,
c1;
{
#ifdef NUMCHAR_OPTION
if ((c1 & CLASS_MASK) == CLASS_UTF16){
w16e_conv(c1, &c2, &c1);
}
#endif
if (c2 == EOF) {
if (output_mode !=ASCII && output_mode!=ISO8859_1) {
(*o_putc)(ESC);
(*o_putc)('(');
(*o_putc)(ascii_intro);
output_mode = ASCII;
}
(*o_putc)(EOF);
} else if (c2==X0201) {
if (output_mode!=X0201) {
output_mode = X0201;
(*o_putc)(ESC);
(*o_putc)('(');
(*o_putc)('I');
}
(*o_putc)(c1);
} else if (c2==ISO8859_1) {
/* iso8859 introduction, or 8th bit on */
/* Can we convert in 7bit form using ESC-'-'-A ?
Is this popular? */
output_mode = ISO8859_1;
(*o_putc)(c1|0x80);
} else if (c2 == 0) {
if (output_mode !=ASCII && output_mode!=ISO8859_1) {
(*o_putc)(ESC);
(*o_putc)('(');
(*o_putc)(ascii_intro);
output_mode = ASCII;
}
(*o_putc)(c1);
} else {
if (output_mode != X0208) {
output_mode = X0208;
(*o_putc)(ESC);
(*o_putc)('$');
(*o_putc)(kanji_intro);
}
if (c1<0x20 || 0x7e<c1)
return;
if (c2<0x20 || 0x7e<c2)
return;
(*o_putc)(c2);
(*o_putc)(c1);
}
}
void
base64_conv(c2, c1)
int c2,
c1;
{
if (base64_count>50 && !mimeout_mode && c2==0 && c1==SPACE) {
(*o_putc)(NL);
} else if (base64_count>66 && mimeout_mode) {
(*o_base64conv)(EOF,0);
(*o_putc)(NL);
(*o_putc)('\t'); base64_count += 7;
}
(*o_base64conv)(c2,c1);
}
static int broken_buf[3];
static int broken_counter = 0;
static int broken_last = 0;
int
broken_getc(f)
FILE *f;
{
int c,c1;
if (broken_counter>0) {
return broken_buf[--broken_counter];
}
c= (*i_bgetc)(f);
if (c=='$' && broken_last != ESC
&& (input_mode==ASCII || input_mode==X0201)) {
c1= (*i_bgetc)(f);
broken_last = 0;
if (c1=='@'|| c1=='B') {
broken_buf[0]=c1; broken_buf[1]=c;
broken_counter=2;
return ESC;
} else {
(*i_bungetc)(c1,f);
return c;
}
} else if (c=='(' && broken_last != ESC
&& (input_mode==X0208 || input_mode==X0201)) { /* ) */
c1= (*i_bgetc)(f);
broken_last = 0;
if (c1=='J'|| c1=='B') {
broken_buf[0]=c1; broken_buf[1]=c;
broken_counter=2;
return ESC;
} else {
(*i_bungetc)(c1,f);
return c;
}
} else {
broken_last = c;
return c;
}
}
int
broken_ungetc(c,f)
int c;
FILE *f;
{
if (broken_counter<2)
broken_buf[broken_counter++]=c;
return c;
}
static int prev_cr = 0;
void
cr_conv(c2,c1)
int c2,c1;
{
if (prev_cr) {
prev_cr = 0;
if (! (c2==0&&c1==NL) ) {
cr_conv(0,'\n');
}
}
if (c2) {
(*o_crconv)(c2,c1);
} else if (c1=='\r') {
prev_cr = c1;
} else if (c1=='\n') {
if (crmode_f==CRLF) {
(*o_crconv)(0,'\r');
} else if (crmode_f==CR) {
(*o_crconv)(0,'\r');
return;
}
(*o_crconv)(0,NL);
} else if (c1!='\032' || crmode_f!=NL){
(*o_crconv)(c2,c1);
}
}
/*
Return value of fold_conv()
\n add newline and output char
\r add newline and output nothing
' ' space
0 skip
1 (or else) normal output
fold state in prev (previous character)
>0x80 Japanese (X0208/X0201)
<0x80 ASCII
\n new line
' ' space
This fold algorthm does not preserve heading space in a line.
This is the main difference from fmt.
*/
#define char_size(c2,c1) (c2?2:1)
void
fold_conv(c2,c1)
int c2,c1;
{
int prev0;
int fold_state=0;
if (c1== '\r' && !fold_preserve_f) {
fold_state=0; /* ignore cr */
}else if (c1== '\n'&&f_prev=='\r' && fold_preserve_f) {
f_prev = '\n';
fold_state=0; /* ignore cr */
} else if (c1== BS) {
if (f_line>0) f_line--;
fold_state = 1;
} else if (c2==EOF && f_line != 0) { /* close open last line */
fold_state = '\n';
} else if ((c1=='\n' && !fold_preserve_f)
|| ((c1=='\r'||(c1=='\n'&&f_prev!='\r'))
&& fold_preserve_f)) {
/* new line */
if (fold_preserve_f) {
f_prev = c1;
f_line = 0;
fold_state = '\r';
} else if ((f_prev == c1 && !fold_preserve_f)
|| (f_prev == '\n' && fold_preserve_f)
) { /* duplicate newline */
if (f_line) {
f_line = 0;
fold_state = '\n'; /* output two newline */
} else {
f_line = 0;
fold_state = 1;
}
} else {
if (f_prev&0x80) { /* Japanese? */
f_prev = c1;
fold_state = 0; /* ignore given single newline */
} else if (f_prev==' ') {
fold_state = 0;
} else {
f_prev = c1;
if (++f_line<=fold_len)
fold_state = ' ';
else {
f_line = 0;
fold_state = '\r'; /* fold and output nothing */
}
}
}
} else if (c1=='\f') {
f_prev = '\n';
if (f_line==0)
fold_state = 1;
f_line = 0;
fold_state = '\n'; /* output newline and clear */
} else if ( (c2==0 && c1==' ')||
(c2==0 && c1=='\t')||
(c2=='!'&& c1=='!')) {
/* X0208 kankaku or ascii space */
if (f_prev == ' ') {
fold_state = 0; /* remove duplicate spaces */
} else {
f_prev = ' ';
if (++f_line<=fold_len)
fold_state = ' '; /* output ASCII space only */
else {
f_prev = ' '; f_line = 0;
fold_state = '\r'; /* fold and output nothing */
}
}
} else {
prev0 = f_prev; /* we still need this one... , but almost done */
f_prev = c1;
if (c2 || c2==X0201)
f_prev |= 0x80; /* this is Japanese */
f_line += char_size(c2,c1);
if (f_line<=fold_len) { /* normal case */
fold_state = 1;
} else {
if (f_line>=fold_len+fold_margin) { /* too many kinsou suspension */
f_line = char_size(c2,c1);
fold_state = '\n'; /* We can't wait, do fold now */
} else if (c2==X0201) {
/* simple kinsoku rules return 1 means no folding */
if (c1==(0xde&0x7f)) fold_state = 1; /* $B!+(B*/
else if (c1==(0xdf&0x7f)) fold_state = 1; /* $B!,(B*/
else if (c1==(0xa4&0x7f)) fold_state = 1; /* $B!#(B*/
else if (c1==(0xa3&0x7f)) fold_state = 1; /* $B!$(B*/
else if (c1==(0xa1&0x7f)) fold_state = 1; /* $B!W(B*/
else if (c1==(0xb0&0x7f)) fold_state = 1; /* - */
else if (SPACE<=c1 && c1<=(0xdf&0x7f)) { /* X0201 */
f_line = 1;
fold_state = '\n';/* add one new f_line before this character */
} else {
f_line = 1;
fold_state = '\n';/* add one new f_line before this character */
}
} else if (c2==0) {
/* kinsoku point in ASCII */
if ( c1==')'|| /* { [ ( */
c1==']'||
c1=='}'||
c1=='.'||
c1==','||
c1=='!'||
c1=='?'||
c1=='/'||
c1==':'||
c1==';' ) {
fold_state = 1;
/* just after special */
} else if (!is_alnum(prev0)) {
f_line = char_size(c2,c1);
fold_state = '\n';
} else if ((prev0==' ') || /* ignored new f_line */
(prev0=='\n')|| /* ignored new f_line */
(prev0&0x80)) { /* X0208 - ASCII */
f_line = char_size(c2,c1);
fold_state = '\n';/* add one new f_line before this character */
} else {
fold_state = 1; /* default no fold in ASCII */
}
} else {
if (c2=='!') {
if (c1=='"') fold_state = 1; /* $B!"(B */
else if (c1=='#') fold_state = 1; /* $B!#(B */
else if (c1=='W') fold_state = 1; /* $B!W(B */
else if (c1=='K') fold_state = 1; /* $B!K(B */
else if (c1=='$') fold_state = 1; /* $B!$(B */
else if (c1=='%') fold_state = 1; /* $B!%(B */
else if (c1=='\'') fold_state = 1; /* $B!\(B */
else if (c1=='(') fold_state = 1; /* $B!((B */
else if (c1==')') fold_state = 1; /* $B!)(B */
else if (c1=='*') fold_state = 1; /* $B!*(B */
else if (c1=='+') fold_state = 1; /* $B!+(B */
else if (c1==',') fold_state = 1; /* $B!,(B */
/* default no fold in kinsoku */
else {
fold_state = '\n';
f_line = char_size(c2,c1);
/* add one new f_line before this character */
}
} else {
f_line = char_size(c2,c1);
fold_state = '\n';
/* add one new f_line before this character */
}
}
}
}
/* terminator process */
switch(fold_state) {
case '\n':
(*o_fconv)(0,'\n');
(*o_fconv)(c2,c1);
break;
case 0:
return;
case '\r':
(*o_fconv)(0,'\n');
break;
case '\t':
case ' ':
(*o_fconv)(0,' ');
break;
default:
(*o_fconv)(c2,c1);
}
}
int z_prev2=0,z_prev1=0;
void
z_conv(c2,c1)
int c2,c1;
{
/* if (c2) c1 &= 0x7f; assertion */
if (x0201_f && z_prev2==X0201) { /* X0201 */
if (c1==(0xde&0x7f)) { /* $BByE@(B */
z_prev2=0;
(*o_zconv)(dv[(z_prev1-SPACE)*2],dv[(z_prev1-SPACE)*2+1]);
return;
} else if (c1==(0xdf&0x7f)&&ev[(z_prev1-SPACE)*2]) { /* $BH>ByE@(B */
z_prev2=0;
(*o_zconv)(ev[(z_prev1-SPACE)*2],ev[(z_prev1-SPACE)*2+1]);
return;
} else {
z_prev2=0;
(*o_zconv)(cv[(z_prev1-SPACE)*2],cv[(z_prev1-SPACE)*2+1]);
}
}
if (c2==EOF) {
(*o_zconv)(c2,c1);
return;
}
if (x0201_f && c2==X0201) {
if (dv[(c1-SPACE)*2]||ev[(c1-SPACE)*2]) {
/* wait for $BByE@(B or $BH>ByE@(B */
z_prev1 = c1; z_prev2 = c2;
return;
} else {
(*o_zconv)(cv[(c1-SPACE)*2],cv[(c1-SPACE)*2+1]);
return;
}
}
/* JISX0208 Alphabet */
if (alpha_f && c2 == 0x23 ) {
c2 = 0;
} else if (alpha_f && c2 == 0x21 ) {
/* JISX0208 Kigou */
if (0x21==c1) {
if (alpha_f&0x2) {
c1 = ' ';
c2 = 0;
} else if (alpha_f&0x4) {
(*o_zconv)(0,' ');
(*o_zconv)(0,' ');
return;
}
} else if (0x20<c1 && c1<0x7f && fv[c1-0x20]) {
c1 = fv[c1-0x20];
c2 = 0;
if (alpha_f&0x8) {
char *entity = 0;
switch (c1){
case '>': entity = ">"; break;
case '<': entity = "<"; break;
case '\"': entity = """; break;
case '&': entity = "&"; break;
}
if (entity){
while (*entity) (*o_zconv)(0, *entity++);
return;
}
}
}
}
(*o_zconv)(c2,c1);
}
#define rot13(c) ( \
( c < 'A' ) ? c: \
(c <= 'M') ? (c + 13): \
(c <= 'Z') ? (c - 13): \
(c < 'a') ? (c): \
(c <= 'm') ? (c + 13): \
(c <= 'z') ? (c - 13): \
(c) \
)
#define rot47(c) ( \
( c < '!' ) ? c: \
( c <= 'O' ) ? (c + 47) : \
( c <= '~' ) ? (c - 47) : \
c \
)
void
rot_conv(c2,c1)
int c2,c1;
{
if (c2==0 || c2==X0201 || c2==ISO8859_1) {
c1 = rot13(c1);
} else if (c2) {
c1 = rot47(c1);
c2 = rot47(c2);
}
(*o_rot_conv)(c2,c1);
}
void
hira_conv(c2,c1)
int c2,c1;
{
if ((hira_f & 1) && c2==0x25 && 0x20<c1 && c1<0x74) {
c2 = 0x24;
} else if ((hira_f & 2) && c2==0x24 && 0x20<c1 && c1<0x74) {
c2 = 0x25;
}
(*o_hira_conv)(c2,c1);
}
void
iso2022jp_check_conv(c2,c1)
int c2, c1;
{
static int range[RANGE_NUM_MAX][2] = {
{0x222f, 0x2239,},
{0x2242, 0x2249,},
{0x2251, 0x225b,},
{0x226b, 0x2271,},
{0x227a, 0x227d,},
{0x2321, 0x232f,},
{0x233a, 0x2340,},
{0x235b, 0x2360,},
{0x237b, 0x237e,},
{0x2474, 0x247e,},
{0x2577, 0x257e,},
{0x2639, 0x2640,},
{0x2659, 0x267e,},
{0x2742, 0x2750,},
{0x2772, 0x277e,},
{0x2841, 0x287e,},
{0x4f54, 0x4f7e,},
{0x7425, 0x747e},
};
int i;
int start, end, c;
if(c2 >= 0x00 && c2 <= 0x20 && c1 >= 0x7f && c1 <= 0xff) {
c2 = GETA1;
c1 = GETA2;
}
if((c2 >= 0x29 && c2 <= 0x2f) || (c2 >= 0x75 && c2 <= 0x7e)) {
c2 = GETA1;
c1 = GETA2;
}
for (i = 0; i < RANGE_NUM_MAX; i++) {
start = range[i][0];
end = range[i][1];
c = (c2 << 8) + c1;
if (c >= start && c <= end) {
c2 = GETA1;
c1 = GETA2;
}
}
(*o_iso2022jp_check_conv)(c2,c1);
}
/* This converts =?ISO-2022-JP?B?HOGE HOGE?= */
unsigned char *mime_pattern[] = {
(unsigned char *)"\075?EUC-JP?B?",
(unsigned char *)"\075?SHIFT_JIS?B?",
(unsigned char *)"\075?ISO-8859-1?Q?",
(unsigned char *)"\075?ISO-8859-1?B?",
(unsigned char *)"\075?ISO-2022-JP?B?",
(unsigned char *)"\075?ISO-2022-JP?Q?",
#if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
(unsigned char *)"\075?UTF-8?B?",
#endif
(unsigned char *)"\075?US-ASCII?Q?",
NULL
};
/* $B3:Ev$9$k%3!<%I$NM%@hEY$r>e$2$k$?$a$NL\0u(B */
int (*mime_priority_func[])PROTO((int c2, int c1, int c0)) = {
e_iconv, s_iconv, 0, 0, 0, 0,
#if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
w_iconv,
#endif
0,
};
int mime_encode[] = {
JAPANESE_EUC, SHIFT_JIS,ISO8859_1, ISO8859_1, X0208, X0201,
#if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
UTF8,
#endif
ASCII,
0
};
int mime_encode_method[] = {
'B', 'B','Q', 'B', 'B', 'Q',
#if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
'B',
#endif
'Q',
0
};
#define MAXRECOVER 20
/* I don't trust portablity of toupper */
#define nkf_toupper(c) (('a'<=c && c<='z')?(c-('a'-'A')):c)
#define nkf_isdigit(c) ('0'<=c && c<='9')
#define nkf_isxdigit(c) (nkf_isdigit(c) || ('a'<=c && c<='f') || ('A'<=c && c <= 'F'))
void
switch_mime_getc()
{
if (i_getc!=mime_getc) {
i_mgetc = i_getc; i_getc = mime_getc;
i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
if(mime_f==STRICT_MIME) {
i_mgetc_buf = i_mgetc; i_mgetc = mime_getc_buf;
i_mungetc_buf = i_mungetc; i_mungetc = mime_ungetc_buf;
}
}
}
void
unswitch_mime_getc()
{
if(mime_f==STRICT_MIME) {
i_mgetc = i_mgetc_buf;
i_mungetc = i_mungetc_buf;
}
i_getc = i_mgetc;
i_ungetc = i_mungetc;
}
int
mime_begin_strict(f)
FILE *f;
{
int c1 = 0;
int i,j,k;
unsigned char *p,*q;
int r[MAXRECOVER]; /* recovery buffer, max mime pattern lenght */
mime_decode_mode = FALSE;
/* =? has been checked */
j = 0;
p = mime_pattern[j];
r[0]='='; r[1]='?';
for(i=2;p[i]>' ';i++) { /* start at =? */
if ( ((r[i] = c1 = (*i_getc)(f))==EOF) || nkf_toupper(c1) != p[i] ) {
/* pattern fails, try next one */
q = p;
while ((p = mime_pattern[++j])) {
for(k=2;k<i;k++) /* assume length(p) > i */
if (p[k]!=q[k]) break;
if (k==i && nkf_toupper(c1)==p[k]) break;
}
if (p) continue; /* found next one, continue */
/* all fails, output from recovery buffer */
(*i_ungetc)(c1,f);
for(j=0;j<i;j++) {
(*oconv)(0,r[j]);
}
return c1;
}
}
mime_decode_mode = p[i-2];
clr_code_score(find_inputcode_byfunc(mime_priority_func[j]), SCORE_iMIME);
if (mime_decode_mode=='B') {
mimebuf_f = unbuf_f;
if (!unbuf_f) {
/* do MIME integrity check */
return mime_integrity(f,mime_pattern[j]);
}
}
switch_mime_getc();
mimebuf_f = TRUE;
return c1;
}
int
mime_getc_buf(f)
FILE *f;
{
/* we don't keep eof of Fifo, becase it contains ?= as
a terminator. It was checked in mime_integrity. */
return ((mimebuf_f)?
(*i_mgetc_buf)(f):Fifo(mime_input++));
}
int
mime_ungetc_buf(c,f)
FILE *f;
int c;
{
if (mimebuf_f)
(*i_mungetc_buf)(c,f);
else
Fifo(--mime_input)=c;
return c;
}
int
mime_begin(f)
FILE *f;
{
int c1;
int i,k;
/* In NONSTRICT mode, only =? is checked. In case of failure, we */
/* re-read and convert again from mime_buffer. */
/* =? has been checked */
k = mime_last;
Fifo(mime_last++)='='; Fifo(mime_last++)='?';
for(i=2;i<MAXRECOVER;i++) { /* start at =? */
/* We accept any character type even if it is breaked by new lines */
c1 = (*i_getc)(f); Fifo(mime_last++)= c1 ;
if (c1=='\n'||c1==' '||c1=='\r'||
c1=='-'||c1=='_'||is_alnum(c1) ) continue;
if (c1=='=') {
/* Failed. But this could be another MIME preemble */
(*i_ungetc)(c1,f);
mime_last--;
break;
}
if (c1!='?') break;
else {
/* c1=='?' */
c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
if (!(++i<MAXRECOVER) || c1==EOF) break;
if (c1=='b'||c1=='B') {
mime_decode_mode = 'B';
} else if (c1=='q'||c1=='Q') {
mime_decode_mode = 'Q';
} else {
break;
}
c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
if (!(++i<MAXRECOVER) || c1==EOF) break;
if (c1!='?') {
mime_decode_mode = FALSE;
}
break;
}
}
switch_mime_getc();
if (!mime_decode_mode) {
/* false MIME premble, restart from mime_buffer */
mime_decode_mode = 1; /* no decode, but read from the mime_buffer */
/* Since we are in MIME mode until buffer becomes empty, */
/* we never go into mime_begin again for a while. */
return c1;
}
/* discard mime preemble, and goto MIME mode */
mime_last = k;
/* do no MIME integrity check */
return c1; /* used only for checking EOF */
}
#ifdef CHECK_OPTION
void
no_putc(c)
int c;
{
;
}
void debug(str)
char *str;
{
if (debug_f){
fprintf(stderr, "%s\n", str);
}
}
#endif
void
set_input_codename (codename)
char *codename;
{
if (guess_f &&
is_inputcode_set &&
strcmp(codename, "") != 0 &&
strcmp(codename, input_codename) != 0)
{
is_inputcode_mixed = TRUE;
}
input_codename = codename;
is_inputcode_set = TRUE;
}
void
print_guessed_code (filename)
char *filename;
{
char *codename = "BINARY";
if (!is_inputcode_mixed) {
if (strcmp(input_codename, "") == 0) {
codename = "ASCII";
} else {
codename = input_codename;
}
}
if (filename != NULL) printf("%s:", filename);
printf("%s\n", codename);
}
int
hex2bin(x)
int x;
{
if (nkf_isdigit(x)) return x - '0';
return nkf_toupper(x) - 'A' + 10;
}
#ifdef INPUT_OPTION
#ifdef ANSI_C_PROTOTYPE
int hex_getc(int ch, FILE *f, int (*g)(FILE *f), int (*u)(int c, FILE *f))
#else
int
hex_getc(ch, f, g, u)
int ch;
FILE *f;
int (*g)();
int (*u)();
#endif
{
int c1, c2, c3;
c1 = (*g)(f);
if (c1 != ch){
return c1;
}
c2 = (*g)(f);
if (!nkf_isxdigit(c2)){
(*u)(c2, f);
return c1;
}
c3 = (*g)(f);
if (!nkf_isxdigit(c3)){
(*u)(c2, f);
(*u)(c3, f);
return c1;
}
return (hex2bin(c2) << 4) | hex2bin(c3);
}
int
cap_getc(f)
FILE *f;
{
return hex_getc(':', f, i_cgetc, i_cungetc);
}
int
cap_ungetc(c, f)
int c;
FILE *f;
{
return (*i_cungetc)(c, f);
}
int
url_getc(f)
FILE *f;
{
return hex_getc('%', f, i_ugetc, i_uungetc);
}
int
url_ungetc(c, f)
int c;
FILE *f;
{
return (*i_uungetc)(c, f);
}
#endif
#ifdef NUMCHAR_OPTION
int
numchar_getc(f)
FILE *f;
{
int (*g)() = i_ngetc;
int (*u)() = i_nungetc;
int i = 0, j;
int buf[8];
long c = -1;
buf[i] = (*g)(f);
if (buf[i] == '&'){
buf[++i] = (*g)(f);
if (buf[i] == '#'){
c = 0;
buf[++i] = (*g)(f);
if (buf[i] == 'x' || buf[i] == 'X'){
for (j = 0; j < 5; j++){
buf[++i] = (*g)(f);
if (!nkf_isxdigit(buf[i])){
if (buf[i] != ';'){
c = -1;
}
break;
}
c <<= 4;
c |= hex2bin(buf[i]);
}
}else{
for (j = 0; j < 6; j++){
if (j){
buf[++i] = (*g)(f);
}
if (!nkf_isdigit(buf[i])){
if (buf[i] != ';'){
c = -1;
}
break;
}
c *= 10;
c += hex2bin(buf[i]);
}
}
}
}
if (c != -1){
return CLASS_UTF16 | c;
}
while (i > 0){
(*u)(buf[i], f);
--i;
}
return buf[0];
}
int
numchar_ungetc(c, f)
int c;
FILE *f;
{
return (*i_nungetc)(c, f);
}
#endif
int
mime_getc(f)
FILE *f;
{
int c1, c2, c3, c4, cc;
int t1, t2, t3, t4, mode, exit_mode;
if (mime_top != mime_last) { /* Something is in FIFO */
return Fifo(mime_top++);
}
if (mime_decode_mode==1 ||mime_decode_mode==FALSE) {
mime_decode_mode=FALSE;
unswitch_mime_getc();
return (*i_getc)(f);
}
if (mimebuf_f == FIXED_MIME)
exit_mode = mime_decode_mode;
else
exit_mode = FALSE;
if (mime_decode_mode == 'Q') {
if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
restart_mime_q:
if (c1=='_') return ' ';
if (c1!='=' && c1!='?') {
return c1;
}
mime_decode_mode = exit_mode; /* prepare for quit */
if (c1<=' ') return c1;
if ((c2 = (*i_mgetc)(f)) == EOF) return (EOF);
if (c1=='?'&&c2=='=' && mimebuf_f != FIXED_MIME) {
/* end Q encoding */
input_mode = exit_mode;
while((c1=(*i_getc)(f))!=EOF && c1==SPACE
/* && (c1==NL||c1==TAB||c1=='\r') */ ) ;
return c1;
}
if (c1=='='&&c2<' ') { /* this is soft wrap */
while((c1 = (*i_mgetc)(f)) <=' ') {
if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
}
mime_decode_mode = 'Q'; /* still in MIME */
goto restart_mime_q;
}
if (c1=='?') {
mime_decode_mode = 'Q'; /* still in MIME */
(*i_mungetc)(c2,f);
return c1;
}
if ((c3 = (*i_mgetc)(f)) == EOF) return (EOF);
if (c2<=' ') return c2;
mime_decode_mode = 'Q'; /* still in MIME */
#define hex(c) (('0'<=c&&c<='9')?(c-'0'):\
('A'<=c&&c<='F')?(c-'A'+10):('a'<=c&&c<='f')?(c-'a'+10):0)
return ((hex(c2)<<4) + hex(c3));
}
if (mime_decode_mode != 'B') {
mime_decode_mode = FALSE;
return (*i_mgetc)(f);
}
/* Base64 encoding */
/*
MIME allows line break in the middle of
Base64, but we are very pessimistic in decoding
in unbuf mode because MIME encoded code may broken by
less or editor's control sequence (such as ESC-[-K in unbuffered
mode. ignore incomplete MIME.
*/
mode = mime_decode_mode;
mime_decode_mode = exit_mode; /* prepare for quit */
while ((c1 = (*i_mgetc)(f))<=' ') {
if (c1==EOF)
return (EOF);
}
mime_c2_retry:
if ((c2 = (*i_mgetc)(f))<=' ') {
if (c2==EOF)
return (EOF);
if (mime_f != STRICT_MIME) goto mime_c2_retry;
if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
return c2;
}
if ((c1 == '?') && (c2 == '=')) {
input_mode = ASCII;
while((c1=(*i_getc)(f))!=EOF && c1==SPACE
/* && (c1==NL||c1==TAB||c1=='\r') */ ) ;
return c1;
}
mime_c3_retry:
if ((c3 = (*i_mgetc)(f))<=' ') {
if (c3==EOF)
return (EOF);
if (mime_f != STRICT_MIME) goto mime_c3_retry;
if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
return c3;
}
mime_c4_retry:
if ((c4 = (*i_mgetc)(f))<=' ') {
if (c4==EOF)
return (EOF);
if (mime_f != STRICT_MIME) goto mime_c4_retry;
if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
return c4;
}
mime_decode_mode = mode; /* still in MIME sigh... */
/* BASE 64 decoding */
t1 = 0x3f & base64decode(c1);
t2 = 0x3f & base64decode(c2);
t3 = 0x3f & base64decode(c3);
t4 = 0x3f & base64decode(c4);
cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03);
if (c2 != '=') {
Fifo(mime_last++) = cc;
cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f);
if (c3 != '=') {
Fifo(mime_last++) = cc;
cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f);
if (c4 != '=')
Fifo(mime_last++) = cc;
}
} else {
return c1;
}
return Fifo(mime_top++);
}
int
mime_ungetc(c,f)
int c;
FILE *f;
{
Fifo(--mime_top) = c;
return c;
}
int
mime_integrity(f,p)
FILE *f;
unsigned char *p;
{
int c,d;
unsigned int q;
/* In buffered mode, read until =? or NL or buffer full
*/
mime_input = mime_top;
mime_last = mime_top;
while(*p) Fifo(mime_input++) = *p++;
d = 0;
q = mime_input;
while((c=(*i_getc)(f))!=EOF) {
if (((mime_input-mime_top)&MIME_BUF_MASK)==0) {
break; /* buffer full */
}
if (c=='=' && d=='?') {
/* checked. skip header, start decode */
Fifo(mime_input++) = c;
/* mime_last_input = mime_input; */
mime_input = q;
switch_mime_getc();
return 1;
}
if (!( (c=='+'||c=='/'|| c=='=' || c=='?' || is_alnum(c))))
break;
/* Should we check length mod 4? */
Fifo(mime_input++) = c;
d=c;
}
/* In case of Incomplete MIME, no MIME decode */
Fifo(mime_input++) = c;
mime_last = mime_input; /* point undecoded buffer */
mime_decode_mode = 1; /* no decode on Fifo last in mime_getc */
switch_mime_getc(); /* anyway we need buffered getc */
return 1;
}
int
base64decode(c)
int c;
{
int i;
if (c > '@') {
if (c < '[') {
i = c - 'A'; /* A..Z 0-25 */
} else {
i = c - 'G' /* - 'a' + 26 */ ; /* a..z 26-51 */
}
} else if (c > '/') {
i = c - '0' + '4' /* - '0' + 52 */ ; /* 0..9 52-61 */
} else if (c == '+') {
i = '>' /* 62 */ ; /* + 62 */
} else {
i = '?' /* 63 */ ; /* / 63 */
}
return (i);
}
static char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int b64c;
void
open_mime(mode)
int mode;
{
unsigned char *p;
int i;
p = mime_pattern[0];
for(i=0;mime_encode[i];i++) {
if (mode == mime_encode[i]) {
p = mime_pattern[i];
break;
}
}
mimeout_mode = mime_encode_method[i];
/* (*o_mputc)(' '); */
while(*p) {
(*o_mputc)(*p++);
base64_count ++;
}
}
void
close_mime()
{
(*o_mputc)('?');
(*o_mputc)('=');
(*o_mputc)(' ');
base64_count += 3;
mimeout_mode = 0;
}
#define itoh4(c) (c>=10?c+'A'-10:c+'0')
void
mime_putc(c)
int c;
{
if (mimeout_f==FIXED_MIME) {
if (base64_count>71) {
(*o_mputc)('\n');
base64_count=0;
}
} else if (c==NL) {
base64_count=0;
}
if (c!=EOF) {
if ( c<=DEL &&(output_mode==ASCII ||output_mode == ISO8859_1 )
&& mimeout_f!=FIXED_MIME) {
if (mimeout_mode=='Q') {
if (c<=SPACE) {
close_mime();
}
(*o_mputc)(c);
return;
}
if (mimeout_mode!='B' || c!=SPACE) {
if (mimeout_mode) {
mime_putc(EOF);
mimeout_mode=0;
}
(*o_mputc)(c);
base64_count ++;
return;
}
} else if (!mimeout_mode && mimeout_f!=FIXED_MIME) {
open_mime(output_mode);
}
} else { /* c==EOF */
switch(mimeout_mode) {
case 'Q':
case 'B':
break;
case 2:
(*o_mputc)(basis_64[((b64c & 0x3)<< 4)]);
(*o_mputc)('=');
(*o_mputc)('=');
base64_count += 3;
break;
case 1:
(*o_mputc)(basis_64[((b64c & 0xF) << 2)]);
(*o_mputc)('=');
base64_count += 2;
break;
}
if (mimeout_mode) {
if (mimeout_f!=FIXED_MIME) {
close_mime();
} else if (mimeout_mode != 'Q')
mimeout_mode = 'B';
}
return;
}
switch(mimeout_mode) {
case 'Q':
if(c>=DEL) {
(*o_mputc)('=');
(*o_mputc)(itoh4(((c>>4)&0xf)));
(*o_mputc)(itoh4((c&0xf)));
} else {
(*o_mputc)(c);
}
break;
case 'B':
b64c=c;
(*o_mputc)(basis_64[c>>2]);
mimeout_mode=2;
base64_count ++;
break;
case 2:
(*o_mputc)(basis_64[((b64c & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
b64c=c;
mimeout_mode=1;
base64_count ++;
break;
case 1:
(*o_mputc)(basis_64[((b64c & 0xF) << 2) | ((c & 0xC0) >>6)]);
(*o_mputc)(basis_64[c & 0x3F]);
mimeout_mode='B';
base64_count += 2;
break;
}
}
#ifdef PERL_XS
void
reinit()
{
unbuf_f = FALSE;
estab_f = FALSE;
nop_f = FALSE;
binmode_f = TRUE;
rot_f = FALSE;
hira_f = FALSE;
input_f = FALSE;
alpha_f = FALSE;
mime_f = STRICT_MIME;
mimebuf_f = FALSE;
broken_f = FALSE;
iso8859_f = FALSE;
#if defined(MSDOS) || defined(__OS2__)
x0201_f = TRUE;
#else
x0201_f = NO_X0201;
#endif
iso2022jp_f = FALSE;
kanji_intro = DEFAULT_J;
ascii_intro = DEFAULT_R;
output_conv = DEFAULT_CONV;
oconv = DEFAULT_CONV;
i_mgetc = std_getc;
i_mungetc = std_ungetc;
i_mgetc_buf = std_getc;
i_mungetc_buf = std_ungetc;
i_getc= std_getc;
i_ungetc=std_ungetc;
i_bgetc= std_getc;
i_bungetc= std_ungetc;
o_putc = std_putc;
o_mputc = std_putc;
o_crconv = no_connection;
o_rot_conv = no_connection;
o_iso2022jp_check_conv = no_connection;
o_hira_conv = no_connection;
o_fconv = no_connection;
o_zconv = no_connection;
i_getc = std_getc;
i_ungetc = std_ungetc;
i_mgetc = std_getc;
i_mungetc = std_ungetc;
output_mode = ASCII;
input_mode = ASCII;
shift_mode = FALSE;
mime_decode_mode = FALSE;
file_out = FALSE;
mimeout_mode = 0;
mimeout_f = FALSE;
base64_count = 0;
option_mode = 0;
crmode_f = 0;
{
struct input_code *p = input_code_list;
while (p->name){
status_reinit(p++);
}
}
#ifdef UTF8_OUTPUT_ENABLE
if (w_oconv16_begin_f) {
w_oconv16_begin_f = 2;
}
#endif
f_line = 0;
f_prev = 0;
fold_preserve_f = FALSE;
fold_f = FALSE;
fold_len = 0;
fold_margin = FOLD_MARGIN;
broken_counter = 0;
broken_last = 0;
z_prev2=0,z_prev1=0;
{
int i;
for (i = 0; i < 256; i++){
prefix_table[i] = 0;
}
}
input_codename = "";
is_inputcode_mixed = FALSE;
is_inputcode_set = FALSE;
}
#endif
void
no_connection(c2,c1)
int c2,c1;
{
no_connection2(c2,c1,0);
}
int
no_connection2(c2,c1,c0)
int c2,c1,c0;
{
fprintf(stderr,"nkf internal module connection failure.\n");
exit(1);
}
#ifndef PERL_XS
void
usage()
{
fprintf(stderr,"USAGE: nkf(nkf32,wnkf,nkf2) -[flags] [in file] .. [out file for -O flag]\n");
fprintf(stderr,"Flags:\n");
fprintf(stderr,"b,u Output is buffered (DEFAULT),Output is unbuffered\n");
#ifdef DEFAULT_CODE_SJIS
fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS (DEFAULT), AT&T JIS (EUC), UTF-8\n");
#endif
#ifdef DEFAULT_CODE_JIS
fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit (DEFAULT), Shift JIS, AT&T JIS (EUC), UTF-8\n");
#endif
#ifdef DEFAULT_CODE_EUC
fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC) (DEFAULT), UTF-8\n");
#endif
#ifdef DEFAULT_CODE_UTF8
fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC), UTF-8 (DEFAULT)\n");
#endif
fprintf(stderr,"J,S,E,W Input assumption is JIS 7 bit , Shift JIS, AT&T JIS (EUC), UTF-8\n");
fprintf(stderr,"t no conversion\n");
fprintf(stderr,"i_/o_ Output sequence to designate JIS-kanji/ASCII (DEFAULT B)\n");
fprintf(stderr,"r {de/en}crypt ROT13/47\n");
fprintf(stderr,"h 1 hirakana->katakana, 2 katakana->hirakana,3 both\n");
fprintf(stderr,"v Show this usage. V: show version\n");
fprintf(stderr,"m[BQN0] MIME decode [B:base64,Q:quoted,N:non-strict,0:no decode]\n");
fprintf(stderr,"M[BQ] MIME encode [B:base64 Q:quoted]\n");
fprintf(stderr,"l ISO8859-1 (Latin-1) support\n");
fprintf(stderr,"f/F Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n");
fprintf(stderr,"Z[0-3] Convert X0208 alphabet to ASCII 1: Kankaku to space,2: 2 spaces,\n");
fprintf(stderr," 3: Convert HTML Entity\n");
fprintf(stderr,"X,x Assume X0201 kana in MS-Kanji, -x preserves X0201\n");
fprintf(stderr,"B[0-2] Broken input 0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n");
#ifdef MSDOS
fprintf(stderr,"T Text mode output\n");
#endif
fprintf(stderr,"O Output to File (DEFAULT 'nkf.out')\n");
fprintf(stderr,"d,c Delete \\r in line feed and \\032, Add \\r in line feed\n");
fprintf(stderr,"I Convert non ISO-2022-JP charactor to GETA\n");
fprintf(stderr,"-L[uwm] line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n");
fprintf(stderr,"long name options\n");
fprintf(stderr," --fj,--unix,--mac,--windows convert for the system\n");
fprintf(stderr," --jis,--euc,--sjis,--utf8,--utf16,--mime,--base64 convert for the code\n");
#ifdef OVERWRITE
fprintf(stderr," --overwrite Overwrite original listed files by filtered result\n");
#endif
fprintf(stderr," -g, --guess Guess the input code\n");
fprintf(stderr," --help,--version\n");
version();
}
void
version()
{
fprintf(stderr,"Network Kanji Filter Version %s (%s) "
#if defined(MSDOS) && !defined(__WIN32__) && !defined(__WIN16__)
"for DOS"
#endif
#if defined(MSDOS) && defined(__WIN16__)
"for Win16"
#endif
#if defined(MSDOS) && defined(__WIN32__)
"for Win32"
#endif
#ifdef __OS2__
"for OS/2"
#endif
,Version,Patchlevel);
fprintf(stderr,"\n%s\n",CopyRight);
}
#endif
/**
** $B%Q%C%A@):n<T(B
** void@merope.pleiades.or.jp (Kusakabe Youichi)
** NIDE Naoyuki <nide@ics.nara-wu.ac.jp>
** ohta@src.ricoh.co.jp (Junn Ohta)
** inouet@strl.nhk.or.jp (Tomoyuki Inoue)
** kiri@pulser.win.or.jp (Tetsuaki Kiriyama)
** Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp>
** a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe)
** kono@ie.u-ryukyu.ac.jp (Shinji Kono)
** GHG00637@nifty-serve.or.jp (COW)
**
**/
/* end */
|