summaryrefslogtreecommitdiff
path: root/transcode.c
blob: 2c0b30ca0e9b4b8453a57afd270daa262d56229a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
/**********************************************************************

  transcode.c -

  $Author$
  created at: Tue Oct 30 16:10:22 JST 2007

  Copyright (C) 2007 Martin Duerst

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

#include "internal.h"
#include "transcode_data.h"
#include <ctype.h>

#define ENABLE_ECONV_NEWLINE_OPTION 1

/* VALUE rb_cEncoding = rb_define_class("Encoding", rb_cObject); */
VALUE rb_eUndefinedConversionError;
VALUE rb_eInvalidByteSequenceError;
VALUE rb_eConverterNotFoundError;

VALUE rb_cEncodingConverter;

static VALUE sym_invalid, sym_undef, sym_replace, sym_fallback, sym_aref;
static VALUE sym_xml, sym_text, sym_attr;
static VALUE sym_universal_newline;
static VALUE sym_crlf_newline;
static VALUE sym_cr_newline;
#ifdef ENABLE_ECONV_NEWLINE_OPTION
static VALUE sym_newline, sym_universal, sym_crlf, sym_cr, sym_lf;
#endif
static VALUE sym_partial_input;

static VALUE sym_invalid_byte_sequence;
static VALUE sym_undefined_conversion;
static VALUE sym_destination_buffer_full;
static VALUE sym_source_buffer_empty;
static VALUE sym_finished;
static VALUE sym_after_output;
static VALUE sym_incomplete_input;

static unsigned char *
allocate_converted_string(const char *sname, const char *dname,
        const unsigned char *str, size_t len,
        unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
        size_t *dst_len_ptr);

/* dynamic structure, one per conversion (similar to iconv_t) */
/* may carry conversion state (e.g. for iso-2022-jp) */
typedef struct rb_transcoding {
    const rb_transcoder *transcoder;

    int flags;

    int resume_position;
    unsigned int next_table;
    VALUE next_info;
    unsigned char next_byte;
    unsigned int output_index;

    ssize_t recognized_len; /* already interpreted */
    ssize_t readagain_len; /* not yet interpreted */
    union {
        unsigned char ary[8]; /* max_input <= sizeof(ary) */
        unsigned char *ptr; /* length: max_input */
    } readbuf; /* recognized_len + readagain_len used */

    ssize_t writebuf_off;
    ssize_t writebuf_len;
    union {
        unsigned char ary[8]; /* max_output <= sizeof(ary) */
        unsigned char *ptr; /* length: max_output */
    } writebuf;

    union rb_transcoding_state_t { /* opaque data for stateful encoding */
        void *ptr;
        char ary[sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*)];
        double dummy_for_alignment;
    } state;
} rb_transcoding;
#define TRANSCODING_READBUF(tc) \
    ((tc)->transcoder->max_input <= (int)sizeof((tc)->readbuf.ary) ? \
     (tc)->readbuf.ary : \
     (tc)->readbuf.ptr)
#define TRANSCODING_WRITEBUF(tc) \
    ((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
     (tc)->writebuf.ary : \
     (tc)->writebuf.ptr)
#define TRANSCODING_WRITEBUF_SIZE(tc) \
    ((tc)->transcoder->max_output <= (int)sizeof((tc)->writebuf.ary) ? \
     sizeof((tc)->writebuf.ary) : \
     (size_t)(tc)->transcoder->max_output)
#define TRANSCODING_STATE_EMBED_MAX ((int)sizeof(union rb_transcoding_state_t))
#define TRANSCODING_STATE(tc) \
    ((tc)->transcoder->state_size <= (int)sizeof((tc)->state) ? \
     (tc)->state.ary : \
     (tc)->state.ptr)

typedef struct {
    struct rb_transcoding *tc;
    unsigned char *out_buf_start;
    unsigned char *out_data_start;
    unsigned char *out_data_end;
    unsigned char *out_buf_end;
    rb_econv_result_t last_result;
} rb_econv_elem_t;

struct rb_econv_t {
    int flags;
    int started; /* bool */

    const char *source_encoding_name;
    const char *destination_encoding_name;

    const unsigned char *replacement_str;
    size_t replacement_len;
    const char *replacement_enc;

    unsigned char *in_buf_start;
    unsigned char *in_data_start;
    unsigned char *in_data_end;
    unsigned char *in_buf_end;
    rb_econv_elem_t *elems;
    int replacement_allocated; /* bool */
    int num_allocated;
    int num_trans;
    int num_finished;
    struct rb_transcoding *last_tc;

    /* last error */
    struct {
        rb_econv_result_t result;
        struct rb_transcoding *error_tc;
        const char *source_encoding;
        const char *destination_encoding;
        const unsigned char *error_bytes_start;
        size_t error_bytes_len;
        size_t readagain_len;
    } last_error;

    /* The following fields are only for Encoding::Converter.
     * rb_econv_open set them NULL. */
    rb_encoding *source_encoding;
    rb_encoding *destination_encoding;
};

/*
 *  Dispatch data and logic
 */

#define DECORATOR_P(sname, dname) (*(sname) == '\0')

typedef struct {
    const char *sname;
    const char *dname;
    const char *lib; /* null means no need to load a library */
    const rb_transcoder *transcoder;
} transcoder_entry_t;

static st_table *transcoder_table;

static transcoder_entry_t *
make_transcoder_entry(const char *sname, const char *dname)
{
    st_data_t val;
    st_table *table2;

    if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
        val = (st_data_t)st_init_strcasetable();
        st_add_direct(transcoder_table, (st_data_t)sname, val);
    }
    table2 = (st_table *)val;
    if (!st_lookup(table2, (st_data_t)dname, &val)) {
        transcoder_entry_t *entry = ALLOC(transcoder_entry_t);
        entry->sname = sname;
        entry->dname = dname;
        entry->lib = NULL;
        entry->transcoder = NULL;
        val = (st_data_t)entry;
        st_add_direct(table2, (st_data_t)dname, val);
    }
    return (transcoder_entry_t *)val;
}

static transcoder_entry_t *
get_transcoder_entry(const char *sname, const char *dname)
{
    st_data_t val;
    st_table *table2;

    if (!st_lookup(transcoder_table, (st_data_t)sname, &val)) {
        return NULL;
    }
    table2 = (st_table *)val;
    if (!st_lookup(table2, (st_data_t)dname, &val)) {
        return NULL;
    }
    return (transcoder_entry_t *)val;
}

void
rb_register_transcoder(const rb_transcoder *tr)
{
    const char *const sname = tr->src_encoding;
    const char *const dname = tr->dst_encoding;

    transcoder_entry_t *entry;

    entry = make_transcoder_entry(sname, dname);
    if (entry->transcoder) {
	rb_raise(rb_eArgError, "transcoder from %s to %s has been already registered",
		 sname, dname);
    }

    entry->transcoder = tr;
}

static void
declare_transcoder(const char *sname, const char *dname, const char *lib)
{
    transcoder_entry_t *entry;

    entry = make_transcoder_entry(sname, dname);
    entry->lib = lib;
}

static const char transcoder_lib_prefix[] = "enc/trans/";

void
rb_declare_transcoder(const char *enc1, const char *enc2, const char *lib)
{
    if (!lib) {
	rb_raise(rb_eArgError, "invalid library name - (null)");
    }
    declare_transcoder(enc1, enc2, lib);
}

#define encoding_equal(enc1, enc2) (STRCASECMP((enc1), (enc2)) == 0)

typedef struct search_path_queue_tag {
    struct search_path_queue_tag *next;
    const char *enc;
} search_path_queue_t;

typedef struct {
    st_table *visited;
    search_path_queue_t *queue;
    search_path_queue_t **queue_last_ptr;
    const char *base_enc;
} search_path_bfs_t;

static int
transcode_search_path_i(st_data_t key, st_data_t val, st_data_t arg)
{
    const char *dname = (const char *)key;
    search_path_bfs_t *bfs = (search_path_bfs_t *)arg;
    search_path_queue_t *q;

    if (st_lookup(bfs->visited, (st_data_t)dname, &val)) {
        return ST_CONTINUE;
    }

    q = ALLOC(search_path_queue_t);
    q->enc = dname;
    q->next = NULL;
    *bfs->queue_last_ptr = q;
    bfs->queue_last_ptr = &q->next;

    st_add_direct(bfs->visited, (st_data_t)dname, (st_data_t)bfs->base_enc);
    return ST_CONTINUE;
}

static int
transcode_search_path(const char *sname, const char *dname,
    void (*callback)(const char *sname, const char *dname, int depth, void *arg),
    void *arg)
{
    search_path_bfs_t bfs;
    search_path_queue_t *q;
    st_data_t val;
    st_table *table2;
    int found;
    int pathlen = -1;

    if (encoding_equal(sname, dname))
        return -1;

    q = ALLOC(search_path_queue_t);
    q->enc = sname;
    q->next = NULL;
    bfs.queue_last_ptr = &q->next;
    bfs.queue = q;

    bfs.visited = st_init_strcasetable();
    st_add_direct(bfs.visited, (st_data_t)sname, (st_data_t)NULL);

    while (bfs.queue) {
        q = bfs.queue;
        bfs.queue = q->next;
        if (!bfs.queue)
            bfs.queue_last_ptr = &bfs.queue;

        if (!st_lookup(transcoder_table, (st_data_t)q->enc, &val)) {
            xfree(q);
            continue;
        }
        table2 = (st_table *)val;

        if (st_lookup(table2, (st_data_t)dname, &val)) {
            st_add_direct(bfs.visited, (st_data_t)dname, (st_data_t)q->enc);
            xfree(q);
            found = 1;
            goto cleanup;
        }

        bfs.base_enc = q->enc;
        st_foreach(table2, transcode_search_path_i, (st_data_t)&bfs);
        bfs.base_enc = NULL;

        xfree(q);
    }
    found = 0;

  cleanup:
    while (bfs.queue) {
        q = bfs.queue;
        bfs.queue = q->next;
        xfree(q);
    }

    if (found) {
        const char *enc = dname;
        int depth;
        pathlen = 0;
        while (1) {
            st_lookup(bfs.visited, (st_data_t)enc, &val);
            if (!val)
                break;
            pathlen++;
            enc = (const char *)val;
        }
        depth = pathlen;
        enc = dname;
        while (1) {
            st_lookup(bfs.visited, (st_data_t)enc, &val);
            if (!val)
                break;
            callback((const char *)val, enc, --depth, arg);
            enc = (const char *)val;
        }
    }

    st_free_table(bfs.visited);

    return pathlen; /* is -1 if not found */
}

static const rb_transcoder *
load_transcoder_entry(transcoder_entry_t *entry)
{
    if (entry->transcoder)
        return entry->transcoder;

    if (entry->lib) {
        const char *const lib = entry->lib;
        const size_t len = strlen(lib);
        const size_t total_len = sizeof(transcoder_lib_prefix) - 1 + len;
        const VALUE fn = rb_str_new(0, total_len);
        char *const path = RSTRING_PTR(fn);
	const int safe = rb_safe_level();

        memcpy(path, transcoder_lib_prefix, sizeof(transcoder_lib_prefix) - 1);
        memcpy(path + sizeof(transcoder_lib_prefix) - 1, lib, len);
        rb_str_set_len(fn, total_len);
        FL_UNSET(fn, FL_TAINT);
        OBJ_FREEZE(fn);
        rb_require_safe(fn, safe > 3 ? 3 : safe);
    }

    if (entry->transcoder)
        return entry->transcoder;

    return NULL;
}

static const char*
get_replacement_character(const char *encname, size_t *len_ret, const char **repl_encname_ptr)
{
    if (encoding_equal(encname, "UTF-8")) {
        *len_ret = 3;
        *repl_encname_ptr = "UTF-8";
        return "\xEF\xBF\xBD";
    }
    else {
        *len_ret = 1;
        *repl_encname_ptr = "US-ASCII";
        return "?";
    }
}

/*
 *  Transcoding engine logic
 */

static const unsigned char *
transcode_char_start(rb_transcoding *tc,
                         const unsigned char *in_start,
                         const unsigned char *inchar_start,
                         const unsigned char *in_p,
                         size_t *char_len_ptr)
{
    const unsigned char *ptr;
    if (inchar_start - in_start < tc->recognized_len) {
        MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len,
               inchar_start, unsigned char, in_p - inchar_start);
        ptr = TRANSCODING_READBUF(tc);
    }
    else {
        ptr = inchar_start - tc->recognized_len;
    }
    *char_len_ptr = tc->recognized_len + (in_p - inchar_start);
    return ptr;
}

static rb_econv_result_t
transcode_restartable0(const unsigned char **in_pos, unsigned char **out_pos,
                      const unsigned char *in_stop, unsigned char *out_stop,
                      rb_transcoding *tc,
                      const int opt)
{
    const rb_transcoder *tr = tc->transcoder;
    int unitlen = tr->input_unit_length;
    ssize_t readagain_len = 0;

    const unsigned char *inchar_start;
    const unsigned char *in_p;

    unsigned char *out_p;

    in_p = inchar_start = *in_pos;

    out_p = *out_pos;

#define SUSPEND(ret, num) \
    do { \
        tc->resume_position = (num); \
        if (0 < in_p - inchar_start) \
            MEMMOVE(TRANSCODING_READBUF(tc)+tc->recognized_len, \
                   inchar_start, unsigned char, in_p - inchar_start); \
        *in_pos = in_p; \
        *out_pos = out_p; \
        tc->recognized_len += in_p - inchar_start; \
        if (readagain_len) { \
            tc->recognized_len -= readagain_len; \
            tc->readagain_len = readagain_len; \
        } \
        return (ret); \
        resume_label ## num:; \
    } while (0)
#define SUSPEND_OBUF(num) \
    do { \
        while (out_stop - out_p < 1) { SUSPEND(econv_destination_buffer_full, num); } \
    } while (0)

#define SUSPEND_AFTER_OUTPUT(num) \
    if ((opt & ECONV_AFTER_OUTPUT) && *out_pos != out_p) { \
        SUSPEND(econv_after_output, num); \
    }

#define next_table (tc->next_table)
#define next_info (tc->next_info)
#define next_byte (tc->next_byte)
#define writebuf_len (tc->writebuf_len)
#define writebuf_off (tc->writebuf_off)

    switch (tc->resume_position) {
      case 0: break;
      case 1: goto resume_label1;
      case 2: goto resume_label2;
      case 3: goto resume_label3;
      case 4: goto resume_label4;
      case 5: goto resume_label5;
      case 6: goto resume_label6;
      case 7: goto resume_label7;
      case 8: goto resume_label8;
      case 9: goto resume_label9;
      case 10: goto resume_label10;
      case 11: goto resume_label11;
      case 12: goto resume_label12;
      case 13: goto resume_label13;
      case 14: goto resume_label14;
      case 15: goto resume_label15;
      case 16: goto resume_label16;
      case 17: goto resume_label17;
      case 18: goto resume_label18;
      case 19: goto resume_label19;
      case 20: goto resume_label20;
      case 21: goto resume_label21;
      case 22: goto resume_label22;
      case 23: goto resume_label23;
      case 24: goto resume_label24;
      case 25: goto resume_label25;
      case 26: goto resume_label26;
      case 27: goto resume_label27;
      case 28: goto resume_label28;
      case 29: goto resume_label29;
      case 30: goto resume_label30;
      case 31: goto resume_label31;
      case 32: goto resume_label32;
      case 33: goto resume_label33;
      case 34: goto resume_label34;
    }

    while (1) {
        inchar_start = in_p;
        tc->recognized_len = 0;
	next_table = tr->conv_tree_start;

        SUSPEND_AFTER_OUTPUT(24);

        if (in_stop <= in_p) {
            if (!(opt & ECONV_PARTIAL_INPUT))
                break;
            SUSPEND(econv_source_buffer_empty, 7);
            continue;
        }

#define BYTE_ADDR(index) (tr->byte_array + (index))
#define WORD_ADDR(index) (tr->word_array + INFO2WORDINDEX(index))
#define BL_BASE BYTE_ADDR(BYTE_LOOKUP_BASE(WORD_ADDR(next_table)))
#define BL_INFO WORD_ADDR(BYTE_LOOKUP_INFO(WORD_ADDR(next_table)))
#define BL_MIN_BYTE     (BL_BASE[0])
#define BL_MAX_BYTE     (BL_BASE[1])
#define BL_OFFSET(byte) (BL_BASE[2+(byte)-BL_MIN_BYTE])
#define BL_ACTION(byte) (BL_INFO[BL_OFFSET((byte))])

	next_byte = (unsigned char)*in_p++;
      follow_byte:
        if (next_byte < BL_MIN_BYTE || BL_MAX_BYTE < next_byte)
            next_info = INVALID;
        else {
            next_info = (VALUE)BL_ACTION(next_byte);
        }
      follow_info:
	switch (next_info & 0x1F) {
	  case NOMAP:
            {
                const unsigned char *p = inchar_start;
                writebuf_off = 0;
                while (p < in_p) {
                    TRANSCODING_WRITEBUF(tc)[writebuf_off++] = (unsigned char)*p++;
                }
                writebuf_len = writebuf_off;
                writebuf_off = 0;
                while (writebuf_off < writebuf_len) {
                    SUSPEND_OBUF(3);
                    *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
                }
            }
            continue;
	  case 0x00: case 0x04: case 0x08: case 0x0C:
	  case 0x10: case 0x14: case 0x18: case 0x1C:
            SUSPEND_AFTER_OUTPUT(25);
	    while (in_p >= in_stop) {
                if (!(opt & ECONV_PARTIAL_INPUT))
                    goto incomplete;
                SUSPEND(econv_source_buffer_empty, 5);
	    }
	    next_byte = (unsigned char)*in_p++;
	    next_table = (unsigned int)next_info;
	    goto follow_byte;
	  case ZERObt: /* drop input */
	    continue;
	  case ONEbt:
            SUSPEND_OBUF(9); *out_p++ = getBT1(next_info);
	    continue;
	  case TWObt:
            SUSPEND_OBUF(10); *out_p++ = getBT1(next_info);
            SUSPEND_OBUF(21); *out_p++ = getBT2(next_info);
	    continue;
	  case THREEbt:
            SUSPEND_OBUF(11); *out_p++ = getBT1(next_info);
            SUSPEND_OBUF(15); *out_p++ = getBT2(next_info);
            SUSPEND_OBUF(16); *out_p++ = getBT3(next_info);
	    continue;
	  case FOURbt:
            SUSPEND_OBUF(12); *out_p++ = getBT0(next_info);
            SUSPEND_OBUF(17); *out_p++ = getBT1(next_info);
            SUSPEND_OBUF(18); *out_p++ = getBT2(next_info);
            SUSPEND_OBUF(19); *out_p++ = getBT3(next_info);
	    continue;
	  case GB4bt:
            SUSPEND_OBUF(29); *out_p++ = getGB4bt0(next_info);
            SUSPEND_OBUF(30); *out_p++ = getGB4bt1(next_info);
            SUSPEND_OBUF(31); *out_p++ = getGB4bt2(next_info);
            SUSPEND_OBUF(32); *out_p++ = getGB4bt3(next_info);
	    continue;
          case STR1:
            tc->output_index = 0;
            while (tc->output_index < STR1_LENGTH(BYTE_ADDR(STR1_BYTEINDEX(next_info)))) {
                SUSPEND_OBUF(28); *out_p++ = BYTE_ADDR(STR1_BYTEINDEX(next_info))[1+tc->output_index];
                tc->output_index++;
            }
            continue;
	  case FUNii:
	    next_info = (VALUE)(*tr->func_ii)(TRANSCODING_STATE(tc), next_info);
	    goto follow_info;
	  case FUNsi:
            {
                const unsigned char *char_start;
                size_t char_len;
                char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
                next_info = (VALUE)(*tr->func_si)(TRANSCODING_STATE(tc), char_start, (size_t)char_len);
                goto follow_info;
            }
	  case FUNio:
            SUSPEND_OBUF(13);
            if (tr->max_output <= out_stop - out_p)
                out_p += tr->func_io(TRANSCODING_STATE(tc),
                    next_info, out_p, out_stop - out_p);
            else {
                writebuf_len = tr->func_io(TRANSCODING_STATE(tc),
                    next_info,
                    TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
                writebuf_off = 0;
                while (writebuf_off < writebuf_len) {
                    SUSPEND_OBUF(20);
                    *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
                }
            }
	    break;
	  case FUNso:
            {
                const unsigned char *char_start;
                size_t char_len;
                SUSPEND_OBUF(14);
                if (tr->max_output <= out_stop - out_p) {
                    char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
                    out_p += tr->func_so(TRANSCODING_STATE(tc),
                        char_start, (size_t)char_len,
                        out_p, out_stop - out_p);
                }
                else {
                    char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
                    writebuf_len = tr->func_so(TRANSCODING_STATE(tc),
                        char_start, (size_t)char_len,
                        TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
                    writebuf_off = 0;
                    while (writebuf_off < writebuf_len) {
                        SUSPEND_OBUF(22);
                        *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
                    }
                }
                break;
            }
      case FUNsio:
            {
                const unsigned char *char_start;
                size_t char_len;
                SUSPEND_OBUF(33);
                if (tr->max_output <= out_stop - out_p) {
                    char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
                    out_p += tr->func_sio(TRANSCODING_STATE(tc),
                        char_start, (size_t)char_len, next_info,
                        out_p, out_stop - out_p);
                }
                else {
                    char_start = transcode_char_start(tc, *in_pos, inchar_start, in_p, &char_len);
                    writebuf_len = tr->func_sio(TRANSCODING_STATE(tc),
                        char_start, (size_t)char_len, next_info,
                        TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
                    writebuf_off = 0;
                    while (writebuf_off < writebuf_len) {
                        SUSPEND_OBUF(34);
                        *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
                    }
                }
                break;
            }
	  case INVALID:
            if (tc->recognized_len + (in_p - inchar_start) <= unitlen) {
                if (tc->recognized_len + (in_p - inchar_start) < unitlen)
                    SUSPEND_AFTER_OUTPUT(26);
                while ((opt & ECONV_PARTIAL_INPUT) && tc->recognized_len + (in_stop - inchar_start) < unitlen) {
                    in_p = in_stop;
                    SUSPEND(econv_source_buffer_empty, 8);
                }
                if (tc->recognized_len + (in_stop - inchar_start) <= unitlen) {
                    in_p = in_stop;
                }
                else {
                    in_p = inchar_start + (unitlen - tc->recognized_len);
                }
            }
            else {
                ssize_t invalid_len; /* including the last byte which causes invalid */
                ssize_t discard_len;
                invalid_len = tc->recognized_len + (in_p - inchar_start);
                discard_len = ((invalid_len - 1) / unitlen) * unitlen;
                readagain_len = invalid_len - discard_len;
            }
            goto invalid;
	  case UNDEF:
	    goto undef;
	  default:
	    rb_raise(rb_eRuntimeError, "unknown transcoding instruction");
	}
	continue;

      invalid:
        SUSPEND(econv_invalid_byte_sequence, 1);
        continue;

      incomplete:
        SUSPEND(econv_incomplete_input, 27);
        continue;

      undef:
        SUSPEND(econv_undefined_conversion, 2);
        continue;
    }

    /* cleanup */
    if (tr->finish_func) {
        SUSPEND_OBUF(4);
        if (tr->max_output <= out_stop - out_p) {
            out_p += tr->finish_func(TRANSCODING_STATE(tc),
                out_p, out_stop - out_p);
        }
        else {
            writebuf_len = tr->finish_func(TRANSCODING_STATE(tc),
                TRANSCODING_WRITEBUF(tc), TRANSCODING_WRITEBUF_SIZE(tc));
            writebuf_off = 0;
            while (writebuf_off < writebuf_len) {
                SUSPEND_OBUF(23);
                *out_p++ = TRANSCODING_WRITEBUF(tc)[writebuf_off++];
            }
        }
    }
    while (1)
        SUSPEND(econv_finished, 6);
#undef SUSPEND
#undef next_table
#undef next_info
#undef next_byte
#undef writebuf_len
#undef writebuf_off
}

static rb_econv_result_t
transcode_restartable(const unsigned char **in_pos, unsigned char **out_pos,
                      const unsigned char *in_stop, unsigned char *out_stop,
                      rb_transcoding *tc,
                      const int opt)
{
    if (tc->readagain_len) {
        unsigned char *readagain_buf = ALLOCA_N(unsigned char, tc->readagain_len);
        const unsigned char *readagain_pos = readagain_buf;
        const unsigned char *readagain_stop = readagain_buf + tc->readagain_len;
        rb_econv_result_t res;

        MEMCPY(readagain_buf, TRANSCODING_READBUF(tc) + tc->recognized_len,
               unsigned char, tc->readagain_len);
        tc->readagain_len = 0;
        res = transcode_restartable0(&readagain_pos, out_pos, readagain_stop, out_stop, tc, opt|ECONV_PARTIAL_INPUT);
        if (res != econv_source_buffer_empty) {
            MEMCPY(TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len,
                   readagain_pos, unsigned char, readagain_stop - readagain_pos);
            tc->readagain_len += readagain_stop - readagain_pos;
            return res;
        }
    }
    return transcode_restartable0(in_pos, out_pos, in_stop, out_stop, tc, opt);
}

static rb_transcoding *
rb_transcoding_open_by_transcoder(const rb_transcoder *tr, int flags)
{
    rb_transcoding *tc;

    tc = ALLOC(rb_transcoding);
    tc->transcoder = tr;
    tc->flags = flags;
    if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
        tc->state.ptr = xmalloc(tr->state_size);
    if (tr->state_init_func) {
        (tr->state_init_func)(TRANSCODING_STATE(tc)); /* xxx: check return value */
    }
    tc->resume_position = 0;
    tc->recognized_len = 0;
    tc->readagain_len = 0;
    tc->writebuf_len = 0;
    tc->writebuf_off = 0;
    if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
        tc->readbuf.ptr = xmalloc(tr->max_input);
    }
    if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
        tc->writebuf.ptr = xmalloc(tr->max_output);
    }
    return tc;
}

static rb_econv_result_t
rb_transcoding_convert(rb_transcoding *tc,
  const unsigned char **input_ptr, const unsigned char *input_stop,
  unsigned char **output_ptr, unsigned char *output_stop,
  int flags)
{
    return transcode_restartable(
                input_ptr, output_ptr,
                input_stop, output_stop,
                tc, flags);
}

static void
rb_transcoding_close(rb_transcoding *tc)
{
    const rb_transcoder *tr = tc->transcoder;
    if (tr->state_fini_func) {
        (tr->state_fini_func)(TRANSCODING_STATE(tc)); /* check return value? */
    }
    if (TRANSCODING_STATE_EMBED_MAX < tr->state_size)
        xfree(tc->state.ptr);
    if ((int)sizeof(tc->readbuf.ary) < tr->max_input)
        xfree(tc->readbuf.ptr);
    if ((int)sizeof(tc->writebuf.ary) < tr->max_output)
        xfree(tc->writebuf.ptr);
    xfree(tc);
}

static size_t
rb_transcoding_memsize(rb_transcoding *tc)
{
    size_t size = sizeof(rb_transcoding);
    const rb_transcoder *tr = tc->transcoder;

    if (TRANSCODING_STATE_EMBED_MAX < tr->state_size) {
	size += tr->state_size;
    }
    if ((int)sizeof(tc->readbuf.ary) < tr->max_input) {
	size += tr->max_input;
    }
    if ((int)sizeof(tc->writebuf.ary) < tr->max_output) {
	size += tr->max_output;
    }
    return size;
}

static rb_econv_t *
rb_econv_alloc(int n_hint)
{
    rb_econv_t *ec;

    if (n_hint <= 0)
        n_hint = 1;

    ec = ALLOC(rb_econv_t);
    ec->flags = 0;
    ec->source_encoding_name = NULL;
    ec->destination_encoding_name = NULL;
    ec->started = 0;
    ec->replacement_str = NULL;
    ec->replacement_len = 0;
    ec->replacement_enc = NULL;
    ec->replacement_allocated = 0;
    ec->in_buf_start = NULL;
    ec->in_data_start = NULL;
    ec->in_data_end = NULL;
    ec->in_buf_end = NULL;
    ec->num_allocated = n_hint;
    ec->num_trans = 0;
    ec->elems = ALLOC_N(rb_econv_elem_t, ec->num_allocated);
    ec->num_finished = 0;
    ec->last_tc = NULL;
    ec->last_error.result = econv_source_buffer_empty;
    ec->last_error.error_tc = NULL;
    ec->last_error.source_encoding = NULL;
    ec->last_error.destination_encoding = NULL;
    ec->last_error.error_bytes_start = NULL;
    ec->last_error.error_bytes_len = 0;
    ec->last_error.readagain_len = 0;
    ec->source_encoding = NULL;
    ec->destination_encoding = NULL;
    return ec;
}

static int
rb_econv_add_transcoder_at(rb_econv_t *ec, const rb_transcoder *tr, int i)
{
    int n, j;
    int bufsize = 4096;
    unsigned char *p;

    if (ec->num_trans == ec->num_allocated) {
        n = ec->num_allocated * 2;
        REALLOC_N(ec->elems, rb_econv_elem_t, n);
        ec->num_allocated = n;
    }

    p = xmalloc(bufsize);

    MEMMOVE(ec->elems+i+1, ec->elems+i, rb_econv_elem_t, ec->num_trans-i);

    ec->elems[i].tc = rb_transcoding_open_by_transcoder(tr, 0);
    ec->elems[i].out_buf_start = p;
    ec->elems[i].out_buf_end = p + bufsize;
    ec->elems[i].out_data_start = p;
    ec->elems[i].out_data_end = p;
    ec->elems[i].last_result = econv_source_buffer_empty;

    ec->num_trans++;

    if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
        for (j = ec->num_trans-1; i <= j; j--) {
            rb_transcoding *tc = ec->elems[j].tc;
            const rb_transcoder *tr2 = tc->transcoder;
            if (!DECORATOR_P(tr2->src_encoding, tr2->dst_encoding)) {
                ec->last_tc = tc;
                break;
            }
        }

    return 0;
}

static rb_econv_t *
rb_econv_open_by_transcoder_entries(int n, transcoder_entry_t **entries)
{
    rb_econv_t *ec;
    int i, ret;

    for (i = 0; i < n; i++) {
        const rb_transcoder *tr;
        tr = load_transcoder_entry(entries[i]);
        if (!tr)
            return NULL;
    }

    ec = rb_econv_alloc(n);

    for (i = 0; i < n; i++) {
        const rb_transcoder *tr = load_transcoder_entry(entries[i]);
        ret = rb_econv_add_transcoder_at(ec, tr, ec->num_trans);
        if (ret == -1) {
            rb_econv_close(ec);
            return NULL;
        }
    }

    return ec;
}

struct trans_open_t {
    transcoder_entry_t **entries;
    int num_additional;
};

static void
trans_open_i(const char *sname, const char *dname, int depth, void *arg)
{
    struct trans_open_t *toarg = arg;

    if (!toarg->entries) {
        toarg->entries = ALLOC_N(transcoder_entry_t *, depth+1+toarg->num_additional);
    }
    toarg->entries[depth] = get_transcoder_entry(sname, dname);
}

static rb_econv_t *
rb_econv_open0(const char *sname, const char *dname, int ecflags)
{
    transcoder_entry_t **entries = NULL;
    int num_trans;
    rb_econv_t *ec;

    int sidx, didx;

    if (*sname) {
        sidx = rb_enc_find_index(sname);
        if (0 <= sidx) {
            rb_enc_from_index(sidx);
        }
    }

    if (*dname) {
        didx = rb_enc_find_index(dname);
        if (0 <= didx) {
            rb_enc_from_index(didx);
        }
    }

    if (*sname == '\0' && *dname == '\0') {
        num_trans = 0;
        entries = NULL;
	sname = dname = "";
    }
    else {
        struct trans_open_t toarg;
        toarg.entries = NULL;
        toarg.num_additional = 0;
        num_trans = transcode_search_path(sname, dname, trans_open_i, (void *)&toarg);
        entries = toarg.entries;
        if (num_trans < 0) {
            xfree(entries);
            return NULL;
        }
    }

    ec = rb_econv_open_by_transcoder_entries(num_trans, entries);
    xfree(entries);
    if (!ec)
        return NULL;

    ec->flags = ecflags;
    ec->source_encoding_name = sname;
    ec->destination_encoding_name = dname;

    return ec;
}

#define MAX_ECFLAGS_DECORATORS 32

static int
decorator_names(int ecflags, const char **decorators_ret)
{
    int num_decorators;

    switch (ecflags & ECONV_NEWLINE_DECORATOR_MASK) {
      case ECONV_UNIVERSAL_NEWLINE_DECORATOR:
      case ECONV_CRLF_NEWLINE_DECORATOR:
      case ECONV_CR_NEWLINE_DECORATOR:
      case 0:
	break;
      default:
        return -1;
    }

    if ((ecflags & ECONV_XML_TEXT_DECORATOR) &&
        (ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR))
        return -1;

    num_decorators = 0;

    if (ecflags & ECONV_XML_TEXT_DECORATOR)
        decorators_ret[num_decorators++] = "xml_text_escape";
    if (ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR)
        decorators_ret[num_decorators++] = "xml_attr_content_escape";
    if (ecflags & ECONV_XML_ATTR_QUOTE_DECORATOR)
        decorators_ret[num_decorators++] = "xml_attr_quote";

    if (ecflags & ECONV_CRLF_NEWLINE_DECORATOR)
        decorators_ret[num_decorators++] = "crlf_newline";
    if (ecflags & ECONV_CR_NEWLINE_DECORATOR)
        decorators_ret[num_decorators++] = "cr_newline";
    if (ecflags & ECONV_UNIVERSAL_NEWLINE_DECORATOR)
        decorators_ret[num_decorators++] = "universal_newline";

    return num_decorators;
}

rb_econv_t *
rb_econv_open(const char *sname, const char *dname, int ecflags)
{
    rb_econv_t *ec;
    int num_decorators;
    const char *decorators[MAX_ECFLAGS_DECORATORS];
    int i;

    num_decorators = decorator_names(ecflags, decorators);
    if (num_decorators == -1)
        return NULL;

    ec = rb_econv_open0(sname, dname, ecflags & ECONV_ERROR_HANDLER_MASK);
    if (!ec)
        return NULL;

    for (i = 0; i < num_decorators; i++)
        if (rb_econv_decorate_at_last(ec, decorators[i]) == -1) {
            rb_econv_close(ec);
            return NULL;
        }

    ec->flags |= ecflags & ~ECONV_ERROR_HANDLER_MASK;

    return ec;
}

static int
trans_sweep(rb_econv_t *ec,
    const unsigned char **input_ptr, const unsigned char *input_stop,
    unsigned char **output_ptr, unsigned char *output_stop,
    int flags,
    int start)
{
    int try;
    int i, f;

    const unsigned char **ipp, *is, *iold;
    unsigned char **opp, *os, *oold;
    rb_econv_result_t res;

    try = 1;
    while (try) {
        try = 0;
        for (i = start; i < ec->num_trans; i++) {
            rb_econv_elem_t *te = &ec->elems[i];

            if (i == 0) {
                ipp = input_ptr;
                is = input_stop;
            }
            else {
                rb_econv_elem_t *prev_te = &ec->elems[i-1];
                ipp = (const unsigned char **)&prev_te->out_data_start;
                is = prev_te->out_data_end;
            }

            if (i == ec->num_trans-1) {
                opp = output_ptr;
                os = output_stop;
            }
            else {
                if (te->out_buf_start != te->out_data_start) {
                    ssize_t len = te->out_data_end - te->out_data_start;
                    ssize_t off = te->out_data_start - te->out_buf_start;
                    MEMMOVE(te->out_buf_start, te->out_data_start, unsigned char, len);
                    te->out_data_start = te->out_buf_start;
                    te->out_data_end -= off;
                }
                opp = &te->out_data_end;
                os = te->out_buf_end;
            }

            f = flags;
            if (ec->num_finished != i)
                f |= ECONV_PARTIAL_INPUT;
            if (i == 0 && (flags & ECONV_AFTER_OUTPUT)) {
                start = 1;
                flags &= ~ECONV_AFTER_OUTPUT;
            }
            if (i != 0)
                f &= ~ECONV_AFTER_OUTPUT;
            iold = *ipp;
            oold = *opp;
            te->last_result = res = rb_transcoding_convert(te->tc, ipp, is, opp, os, f);
            if (iold != *ipp || oold != *opp)
                try = 1;

            switch (res) {
              case econv_invalid_byte_sequence:
              case econv_incomplete_input:
              case econv_undefined_conversion:
              case econv_after_output:
                return i;

              case econv_destination_buffer_full:
              case econv_source_buffer_empty:
                break;

              case econv_finished:
                ec->num_finished = i+1;
                break;
            }
        }
    }
    return -1;
}

static rb_econv_result_t
rb_trans_conv(rb_econv_t *ec,
    const unsigned char **input_ptr, const unsigned char *input_stop,
    unsigned char **output_ptr, unsigned char *output_stop,
    int flags,
    int *result_position_ptr)
{
    int i;
    int needreport_index;
    int sweep_start;

    unsigned char empty_buf;
    unsigned char *empty_ptr = &empty_buf;

    if (!input_ptr) {
        input_ptr = (const unsigned char **)&empty_ptr;
        input_stop = empty_ptr;
    }

    if (!output_ptr) {
        output_ptr = &empty_ptr;
        output_stop = empty_ptr;
    }

    if (ec->elems[0].last_result == econv_after_output)
        ec->elems[0].last_result = econv_source_buffer_empty;

    needreport_index = -1;
    for (i = ec->num_trans-1; 0 <= i; i--) {
        switch (ec->elems[i].last_result) {
          case econv_invalid_byte_sequence:
          case econv_incomplete_input:
          case econv_undefined_conversion:
          case econv_after_output:
          case econv_finished:
            sweep_start = i+1;
            needreport_index = i;
            goto found_needreport;

          case econv_destination_buffer_full:
          case econv_source_buffer_empty:
            break;

          default:
            rb_bug("unexpected transcode last result");
        }
    }

    /* /^[sd]+$/ is confirmed.  but actually /^s*d*$/. */

    if (ec->elems[ec->num_trans-1].last_result == econv_destination_buffer_full &&
        (flags & ECONV_AFTER_OUTPUT)) {
        rb_econv_result_t res;

        res = rb_trans_conv(ec, NULL, NULL, output_ptr, output_stop,
                (flags & ~ECONV_AFTER_OUTPUT)|ECONV_PARTIAL_INPUT,
                result_position_ptr);

        if (res == econv_source_buffer_empty)
            return econv_after_output;
        return res;
    }

    sweep_start = 0;

  found_needreport:

    do {
        needreport_index = trans_sweep(ec, input_ptr, input_stop, output_ptr, output_stop, flags, sweep_start);
        sweep_start = needreport_index + 1;
    } while (needreport_index != -1 && needreport_index != ec->num_trans-1);

    for (i = ec->num_trans-1; 0 <= i; i--) {
        if (ec->elems[i].last_result != econv_source_buffer_empty) {
            rb_econv_result_t res = ec->elems[i].last_result;
            if (res == econv_invalid_byte_sequence ||
                res == econv_incomplete_input ||
                res == econv_undefined_conversion ||
                res == econv_after_output) {
                ec->elems[i].last_result = econv_source_buffer_empty;
            }
            if (result_position_ptr)
                *result_position_ptr = i;
            return res;
        }
    }
    if (result_position_ptr)
        *result_position_ptr = -1;
    return econv_source_buffer_empty;
}

static rb_econv_result_t
rb_econv_convert0(rb_econv_t *ec,
    const unsigned char **input_ptr, const unsigned char *input_stop,
    unsigned char **output_ptr, unsigned char *output_stop,
    int flags)
{
    rb_econv_result_t res;
    int result_position;
    int has_output = 0;

    memset(&ec->last_error, 0, sizeof(ec->last_error));

    if (ec->num_trans == 0) {
        size_t len;
        if (ec->in_buf_start && ec->in_data_start != ec->in_data_end) {
            if (output_stop - *output_ptr < ec->in_data_end - ec->in_data_start) {
                len = output_stop - *output_ptr;
                memcpy(*output_ptr, ec->in_data_start, len);
                *output_ptr = output_stop;
                ec->in_data_start += len;
                res = econv_destination_buffer_full;
                goto gotresult;
            }
            len = ec->in_data_end - ec->in_data_start;
            memcpy(*output_ptr, ec->in_data_start, len);
            *output_ptr += len;
            ec->in_data_start = ec->in_data_end = ec->in_buf_start;
            if (flags & ECONV_AFTER_OUTPUT) {
                res = econv_after_output;
                goto gotresult;
            }
        }
        if (output_stop - *output_ptr < input_stop - *input_ptr) {
            len = output_stop - *output_ptr;
        }
        else {
            len = input_stop - *input_ptr;
        }
        if (0 < len && (flags & ECONV_AFTER_OUTPUT)) {
            *(*output_ptr)++ = *(*input_ptr)++;
            res = econv_after_output;
            goto gotresult;
        }
        memcpy(*output_ptr, *input_ptr, len);
        *output_ptr += len;
        *input_ptr += len;
        if (*input_ptr != input_stop)
            res = econv_destination_buffer_full;
        else if (flags & ECONV_PARTIAL_INPUT)
            res = econv_source_buffer_empty;
        else
            res = econv_finished;
        goto gotresult;
    }

    if (ec->elems[ec->num_trans-1].out_data_start) {
        unsigned char *data_start = ec->elems[ec->num_trans-1].out_data_start;
        unsigned char *data_end = ec->elems[ec->num_trans-1].out_data_end;
        if (data_start != data_end) {
            size_t len;
            if (output_stop - *output_ptr < data_end - data_start) {
                len = output_stop - *output_ptr;
                memcpy(*output_ptr, data_start, len);
                *output_ptr = output_stop;
                ec->elems[ec->num_trans-1].out_data_start += len;
                res = econv_destination_buffer_full;
                goto gotresult;
            }
            len = data_end - data_start;
            memcpy(*output_ptr, data_start, len);
            *output_ptr += len;
            ec->elems[ec->num_trans-1].out_data_start =
                ec->elems[ec->num_trans-1].out_data_end =
                ec->elems[ec->num_trans-1].out_buf_start;
            has_output = 1;
        }
    }

    if (ec->in_buf_start &&
        ec->in_data_start != ec->in_data_end) {
        res = rb_trans_conv(ec, (const unsigned char **)&ec->in_data_start, ec->in_data_end, output_ptr, output_stop,
                (flags&~ECONV_AFTER_OUTPUT)|ECONV_PARTIAL_INPUT, &result_position);
        if (res != econv_source_buffer_empty)
            goto gotresult;
    }

    if (has_output &&
        (flags & ECONV_AFTER_OUTPUT) &&
        *input_ptr != input_stop) {
        input_stop = *input_ptr;
        res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
        if (res == econv_source_buffer_empty)
            res = econv_after_output;
    }
    else if ((flags & ECONV_AFTER_OUTPUT) ||
        ec->num_trans == 1) {
        res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
    }
    else {
        flags |= ECONV_AFTER_OUTPUT;
        do {
            res = rb_trans_conv(ec, input_ptr, input_stop, output_ptr, output_stop, flags, &result_position);
        } while (res == econv_after_output);
    }

  gotresult:
    ec->last_error.result = res;
    if (res == econv_invalid_byte_sequence ||
        res == econv_incomplete_input ||
        res == econv_undefined_conversion) {
        rb_transcoding *error_tc = ec->elems[result_position].tc;
        ec->last_error.error_tc = error_tc;
        ec->last_error.source_encoding = error_tc->transcoder->src_encoding;
        ec->last_error.destination_encoding = error_tc->transcoder->dst_encoding;
        ec->last_error.error_bytes_start = TRANSCODING_READBUF(error_tc);
        ec->last_error.error_bytes_len = error_tc->recognized_len;
        ec->last_error.readagain_len = error_tc->readagain_len;
    }

    return res;
}

static int output_replacement_character(rb_econv_t *ec);

static int
output_hex_charref(rb_econv_t *ec)
{
    int ret;
    unsigned char utfbuf[1024];
    const unsigned char *utf;
    size_t utf_len;
    int utf_allocated = 0;
    char charef_buf[16];
    const unsigned char *p;

    if (encoding_equal(ec->last_error.source_encoding, "UTF-32BE")) {
        utf = ec->last_error.error_bytes_start;
        utf_len = ec->last_error.error_bytes_len;
    }
    else {
        utf = allocate_converted_string(ec->last_error.source_encoding, "UTF-32BE",
                ec->last_error.error_bytes_start, ec->last_error.error_bytes_len,
                utfbuf, sizeof(utfbuf),
                &utf_len);
        if (!utf)
            return -1;
        if (utf != utfbuf && utf != ec->last_error.error_bytes_start)
            utf_allocated = 1;
    }

    if (utf_len % 4 != 0)
        goto fail;

    p = utf;
    while (4 <= utf_len) {
        unsigned int u = 0;
        u += p[0] << 24;
        u += p[1] << 16;
        u += p[2] << 8;
        u += p[3];
        snprintf(charef_buf, sizeof(charef_buf), "&#x%X;", u);

        ret = rb_econv_insert_output(ec, (unsigned char *)charef_buf, strlen(charef_buf), "US-ASCII");
        if (ret == -1)
            goto fail;

        p += 4;
        utf_len -= 4;
    }

    if (utf_allocated)
        xfree((void *)utf);
    return 0;

  fail:
    if (utf_allocated)
        xfree((void *)utf);
    return -1;
}

rb_econv_result_t
rb_econv_convert(rb_econv_t *ec,
    const unsigned char **input_ptr, const unsigned char *input_stop,
    unsigned char **output_ptr, unsigned char *output_stop,
    int flags)
{
    rb_econv_result_t ret;

    unsigned char empty_buf;
    unsigned char *empty_ptr = &empty_buf;

    ec->started = 1;

    if (!input_ptr) {
        input_ptr = (const unsigned char **)&empty_ptr;
        input_stop = empty_ptr;
    }

    if (!output_ptr) {
        output_ptr = &empty_ptr;
        output_stop = empty_ptr;
    }

  resume:
    ret = rb_econv_convert0(ec, input_ptr, input_stop, output_ptr, output_stop, flags);

    if (ret == econv_invalid_byte_sequence ||
        ret == econv_incomplete_input) {
	/* deal with invalid byte sequence */
	/* todo: add more alternative behaviors */
        switch (ec->flags & ECONV_INVALID_MASK) {
          case ECONV_INVALID_REPLACE:
	    if (output_replacement_character(ec) == 0)
                goto resume;
	}
    }

    if (ret == econv_undefined_conversion) {
	/* valid character in source encoding
	 * but no related character(s) in destination encoding */
	/* todo: add more alternative behaviors */
        switch (ec->flags & ECONV_UNDEF_MASK) {
          case ECONV_UNDEF_REPLACE:
	    if (output_replacement_character(ec) == 0)
                goto resume;
            break;

          case ECONV_UNDEF_HEX_CHARREF:
            if (output_hex_charref(ec) == 0)
                goto resume;
            break;
        }
    }

    return ret;
}

const char *
rb_econv_encoding_to_insert_output(rb_econv_t *ec)
{
    rb_transcoding *tc = ec->last_tc;
    const rb_transcoder *tr;

    if (tc == NULL)
        return "";

    tr = tc->transcoder;

    if (tr->asciicompat_type == asciicompat_encoder)
        return tr->src_encoding;
    return tr->dst_encoding;
}

static unsigned char *
allocate_converted_string(const char *sname, const char *dname,
        const unsigned char *str, size_t len,
        unsigned char *caller_dst_buf, size_t caller_dst_bufsize,
        size_t *dst_len_ptr)
{
    unsigned char *dst_str;
    size_t dst_len;
    size_t dst_bufsize;

    rb_econv_t *ec;
    rb_econv_result_t res;

    const unsigned char *sp;
    unsigned char *dp;

    if (caller_dst_buf)
        dst_bufsize = caller_dst_bufsize;
    else if (len == 0)
        dst_bufsize = 1;
    else
        dst_bufsize = len;

    ec = rb_econv_open(sname, dname, 0);
    if (ec == NULL)
        return NULL;
    if (caller_dst_buf)
        dst_str = caller_dst_buf;
    else
        dst_str = xmalloc(dst_bufsize);
    dst_len = 0;
    sp = str;
    dp = dst_str+dst_len;
    res = rb_econv_convert(ec, &sp, str+len, &dp, dst_str+dst_bufsize, 0);
    dst_len = dp - dst_str;
    while (res == econv_destination_buffer_full) {
        if (SIZE_MAX/2 < dst_bufsize) {
            goto fail;
        }
        dst_bufsize *= 2;
        if (dst_str == caller_dst_buf) {
            unsigned char *tmp;
            tmp = xmalloc(dst_bufsize);
            memcpy(tmp, dst_str, dst_bufsize/2);
            dst_str = tmp;
        }
        else {
            dst_str = xrealloc(dst_str, dst_bufsize);
        }
        dp = dst_str+dst_len;
        res = rb_econv_convert(ec, &sp, str+len, &dp, dst_str+dst_bufsize, 0);
        dst_len = dp - dst_str;
    }
    if (res != econv_finished) {
        goto fail;
    }
    rb_econv_close(ec);
    *dst_len_ptr = dst_len;
    return dst_str;

  fail:
    if (dst_str != caller_dst_buf)
        xfree(dst_str);
    rb_econv_close(ec);
    return NULL;
}

/* result: 0:success -1:failure */
int
rb_econv_insert_output(rb_econv_t *ec,
    const unsigned char *str, size_t len, const char *str_encoding)
{
    const char *insert_encoding = rb_econv_encoding_to_insert_output(ec);
    unsigned char insert_buf[4096];
    const unsigned char *insert_str = NULL;
    size_t insert_len;

    int last_trans_index;
    rb_transcoding *tc;

    unsigned char **buf_start_p;
    unsigned char **data_start_p;
    unsigned char **data_end_p;
    unsigned char **buf_end_p;

    size_t need;

    ec->started = 1;

    if (len == 0)
        return 0;

    if (encoding_equal(insert_encoding, str_encoding)) {
        insert_str = str;
        insert_len = len;
    }
    else {
        insert_str = allocate_converted_string(str_encoding, insert_encoding,
                str, len, insert_buf, sizeof(insert_buf), &insert_len);
        if (insert_str == NULL)
            return -1;
    }

    need = insert_len;

    last_trans_index = ec->num_trans-1;
    if (ec->num_trans == 0) {
        tc = NULL;
        buf_start_p = &ec->in_buf_start;
        data_start_p = &ec->in_data_start;
        data_end_p = &ec->in_data_end;
        buf_end_p = &ec->in_buf_end;
    }
    else if (ec->elems[last_trans_index].tc->transcoder->asciicompat_type == asciicompat_encoder) {
        tc = ec->elems[last_trans_index].tc;
        need += tc->readagain_len;
        if (need < insert_len)
            goto fail;
        if (last_trans_index == 0) {
            buf_start_p = &ec->in_buf_start;
            data_start_p = &ec->in_data_start;
            data_end_p = &ec->in_data_end;
            buf_end_p = &ec->in_buf_end;
        }
        else {
            rb_econv_elem_t *ee = &ec->elems[last_trans_index-1];
            buf_start_p = &ee->out_buf_start;
            data_start_p = &ee->out_data_start;
            data_end_p = &ee->out_data_end;
            buf_end_p = &ee->out_buf_end;
        }
    }
    else {
        rb_econv_elem_t *ee = &ec->elems[last_trans_index];
        buf_start_p = &ee->out_buf_start;
        data_start_p = &ee->out_data_start;
        data_end_p = &ee->out_data_end;
        buf_end_p = &ee->out_buf_end;
        tc = ec->elems[last_trans_index].tc;
    }

    if (*buf_start_p == NULL) {
        unsigned char *buf = xmalloc(need);
        *buf_start_p = buf;
        *data_start_p = buf;
        *data_end_p = buf;
        *buf_end_p = buf+need;
    }
    else if ((size_t)(*buf_end_p - *data_end_p) < need) {
        MEMMOVE(*buf_start_p, *data_start_p, unsigned char, *data_end_p - *data_start_p);
        *data_end_p = *buf_start_p + (*data_end_p - *data_start_p);
        *data_start_p = *buf_start_p;
        if ((size_t)(*buf_end_p - *data_end_p) < need) {
            unsigned char *buf;
            size_t s = (*data_end_p - *buf_start_p) + need;
            if (s < need)
                goto fail;
            buf = xrealloc(*buf_start_p, s);
            *data_start_p = buf;
            *data_end_p = buf + (*data_end_p - *buf_start_p);
            *buf_start_p = buf;
            *buf_end_p = buf + s;
        }
    }

    memcpy(*data_end_p, insert_str, insert_len);
    *data_end_p += insert_len;
    if (tc && tc->transcoder->asciicompat_type == asciicompat_encoder) {
        memcpy(*data_end_p, TRANSCODING_READBUF(tc)+tc->recognized_len, tc->readagain_len);
        *data_end_p += tc->readagain_len;
        tc->readagain_len = 0;
    }

    if (insert_str != str && insert_str != insert_buf)
        xfree((void*)insert_str);
    return 0;

  fail:
    if (insert_str != str && insert_str != insert_buf)
        xfree((void*)insert_str);
    return -1;
}

void
rb_econv_close(rb_econv_t *ec)
{
    int i;

    if (ec->replacement_allocated) {
        xfree((void *)ec->replacement_str);
    }
    for (i = 0; i < ec->num_trans; i++) {
        rb_transcoding_close(ec->elems[i].tc);
        if (ec->elems[i].out_buf_start)
            xfree(ec->elems[i].out_buf_start);
    }
    xfree(ec->in_buf_start);
    xfree(ec->elems);
    xfree(ec);
}

size_t
rb_econv_memsize(rb_econv_t *ec)
{
    size_t size = sizeof(rb_econv_t);
    int i;

    if (ec->replacement_allocated) {
	size += ec->replacement_len;
    }
    for (i = 0; i < ec->num_trans; i++) {
	size += rb_transcoding_memsize(ec->elems[i].tc);

	if (ec->elems[i].out_buf_start) {
            size += ec->elems[i].out_buf_end - ec->elems[i].out_buf_start;
	}
    }
    size += ec->in_buf_end - ec->in_buf_start;
    size += sizeof(rb_econv_elem_t) * ec->num_allocated;

    return size;
}

int
rb_econv_putbackable(rb_econv_t *ec)
{
    if (ec->num_trans == 0)
        return 0;
#if SIZEOF_SIZE_T > SIZEOF_INT
    if (ec->elems[0].tc->readagain_len > INT_MAX) return INT_MAX;
#endif
    return (int)ec->elems[0].tc->readagain_len;
}

void
rb_econv_putback(rb_econv_t *ec, unsigned char *p, int n)
{
    rb_transcoding *tc;
    if (ec->num_trans == 0 || n == 0)
        return;
    tc = ec->elems[0].tc;
    memcpy(p, TRANSCODING_READBUF(tc) + tc->recognized_len + tc->readagain_len - n, n);
    tc->readagain_len -= n;
}

struct asciicompat_encoding_t {
    const char *ascii_compat_name;
    const char *ascii_incompat_name;
};

static int
asciicompat_encoding_i(st_data_t key, st_data_t val, st_data_t arg)
{
    struct asciicompat_encoding_t *data = (struct asciicompat_encoding_t *)arg;
    transcoder_entry_t *entry = (transcoder_entry_t *)val;
    const rb_transcoder *tr;

    if (DECORATOR_P(entry->sname, entry->dname))
        return ST_CONTINUE;
    tr = load_transcoder_entry(entry);
    if (tr && tr->asciicompat_type == asciicompat_decoder) {
        data->ascii_compat_name = tr->dst_encoding;
        return ST_STOP;
    }
    return ST_CONTINUE;
}

const char *
rb_econv_asciicompat_encoding(const char *ascii_incompat_name)
{
    st_data_t v;
    st_table *table2;
    struct asciicompat_encoding_t data;

    if (!st_lookup(transcoder_table, (st_data_t)ascii_incompat_name, &v))
        return NULL;
    table2 = (st_table *)v;

    /*
     * Assumption:
     * There is at most one transcoder for
     * converting from ASCII incompatible encoding.
     *
     * For ISO-2022-JP, there is ISO-2022-JP -> stateless-ISO-2022-JP and no others.
     */
    if (table2->num_entries != 1)
        return NULL;

    data.ascii_incompat_name = ascii_incompat_name;
    data.ascii_compat_name = NULL;
    st_foreach(table2, asciicompat_encoding_i, (st_data_t)&data);
    return data.ascii_compat_name;
}

VALUE
rb_econv_append(rb_econv_t *ec, const char *ss, long len, VALUE dst, int flags)
{
    unsigned const char *sp, *se;
    unsigned char *ds, *dp, *de;
    rb_econv_result_t res;
    int max_output;

    if (NIL_P(dst)) {
        dst = rb_str_buf_new(len);
        if (ec->destination_encoding)
            rb_enc_associate(dst, ec->destination_encoding);
    }

    if (ec->last_tc)
        max_output = ec->last_tc->transcoder->max_output;
    else
        max_output = 1;

    do {
        long dlen = RSTRING_LEN(dst);
        if (rb_str_capacity(dst) - dlen < (size_t)len + max_output) {
            unsigned long new_capa = (unsigned long)dlen + len + max_output;
            if (LONG_MAX < new_capa)
                rb_raise(rb_eArgError, "too long string");
            rb_str_resize(dst, new_capa);
            rb_str_set_len(dst, dlen);
        }
        sp = (const unsigned char *)ss;
        se = sp + len;
        ds = (unsigned char *)RSTRING_PTR(dst);
        de = ds + rb_str_capacity(dst);
        dp = ds += dlen;
        res = rb_econv_convert(ec, &sp, se, &dp, de, flags);
        len -= (const char *)sp - ss;
        ss  = (const char *)sp;
        rb_str_set_len(dst, dlen + (dp - ds));
        rb_econv_check_error(ec);
    } while (res == econv_destination_buffer_full);

    return dst;
}

VALUE
rb_econv_substr_append(rb_econv_t *ec, VALUE src, long off, long len, VALUE dst, int flags)
{
    src = rb_str_new_frozen(src);
    dst = rb_econv_append(ec, RSTRING_PTR(src) + off, len, dst, flags);
    RB_GC_GUARD(src);
    OBJ_INFECT_RAW(dst, src);
    return dst;
}

VALUE
rb_econv_str_append(rb_econv_t *ec, VALUE src, VALUE dst, int flags)
{
    return rb_econv_substr_append(ec, src, 0, RSTRING_LEN(src), dst, flags);
}

VALUE
rb_econv_substr_convert(rb_econv_t *ec, VALUE src, long byteoff, long bytesize, int flags)
{
    return rb_econv_substr_append(ec, src, byteoff, bytesize, Qnil, flags);
}

VALUE
rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags)
{
    return rb_econv_substr_append(ec, src, 0, RSTRING_LEN(src), Qnil, flags);
}

static int
rb_econv_add_converter(rb_econv_t *ec, const char *sname, const char *dname, int n)
{
    transcoder_entry_t *entry;
    const rb_transcoder *tr;

    if (ec->started != 0)
        return -1;

    entry = get_transcoder_entry(sname, dname);
    if (!entry)
        return -1;

    tr = load_transcoder_entry(entry);
    if (!tr) return -1;

    return rb_econv_add_transcoder_at(ec, tr, n);
}

static int
rb_econv_decorate_at(rb_econv_t *ec, const char *decorator_name, int n)
{
    return rb_econv_add_converter(ec, "", decorator_name, n);
}

int
rb_econv_decorate_at_first(rb_econv_t *ec, const char *decorator_name)
{
    const rb_transcoder *tr;

    if (ec->num_trans == 0)
        return rb_econv_decorate_at(ec, decorator_name, 0);

    tr = ec->elems[0].tc->transcoder;

    if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
        tr->asciicompat_type == asciicompat_decoder)
        return rb_econv_decorate_at(ec, decorator_name, 1);

    return rb_econv_decorate_at(ec, decorator_name, 0);
}

int
rb_econv_decorate_at_last(rb_econv_t *ec, const char *decorator_name)
{
    const rb_transcoder *tr;

    if (ec->num_trans == 0)
        return rb_econv_decorate_at(ec, decorator_name, 0);

    tr = ec->elems[ec->num_trans-1].tc->transcoder;

    if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
        tr->asciicompat_type == asciicompat_encoder)
        return rb_econv_decorate_at(ec, decorator_name, ec->num_trans-1);

    return rb_econv_decorate_at(ec, decorator_name, ec->num_trans);
}

void
rb_econv_binmode(rb_econv_t *ec)
{
    const char *dname = 0;

    switch (ec->flags & ECONV_NEWLINE_DECORATOR_MASK) {
      case ECONV_UNIVERSAL_NEWLINE_DECORATOR:
	dname = "universal_newline";
	break;
      case ECONV_CRLF_NEWLINE_DECORATOR:
	dname = "crlf_newline";
	break;
      case ECONV_CR_NEWLINE_DECORATOR:
	dname = "cr_newline";
	break;
    }

    if (dname) {
        const rb_transcoder *transcoder = get_transcoder_entry("", dname)->transcoder;
        int num_trans = ec->num_trans;
	int i, j = 0;

	for (i=0; i < num_trans; i++) {
	    if (transcoder == ec->elems[i].tc->transcoder) {
		rb_transcoding_close(ec->elems[i].tc);
		xfree(ec->elems[i].out_buf_start);
		ec->num_trans--;
	    }
	    else
		ec->elems[j++] = ec->elems[i];
	}
    }

    ec->flags &= ~ECONV_NEWLINE_DECORATOR_MASK;
}

static VALUE
econv_description(const char *sname, const char *dname, int ecflags, VALUE mesg)
{
    int has_description = 0;

    if (NIL_P(mesg))
        mesg = rb_str_new(NULL, 0);

    if (*sname != '\0' || *dname != '\0') {
        if (*sname == '\0')
            rb_str_cat2(mesg, dname);
        else if (*dname == '\0')
            rb_str_cat2(mesg, sname);
        else
            rb_str_catf(mesg, "%s to %s", sname, dname);
        has_description = 1;
    }

    if (ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
                   ECONV_XML_TEXT_DECORATOR|
                   ECONV_XML_ATTR_CONTENT_DECORATOR|
                   ECONV_XML_ATTR_QUOTE_DECORATOR)) {
        const char *pre = "";
        if (has_description)
            rb_str_cat2(mesg, " with ");
        if (ecflags & ECONV_UNIVERSAL_NEWLINE_DECORATOR)  {
            rb_str_cat2(mesg, pre); pre = ",";
            rb_str_cat2(mesg, "universal_newline");
        }
        if (ecflags & ECONV_CRLF_NEWLINE_DECORATOR) {
            rb_str_cat2(mesg, pre); pre = ",";
            rb_str_cat2(mesg, "crlf_newline");
        }
        if (ecflags & ECONV_CR_NEWLINE_DECORATOR) {
            rb_str_cat2(mesg, pre); pre = ",";
            rb_str_cat2(mesg, "cr_newline");
        }
        if (ecflags & ECONV_XML_TEXT_DECORATOR) {
            rb_str_cat2(mesg, pre); pre = ",";
            rb_str_cat2(mesg, "xml_text");
        }
        if (ecflags & ECONV_XML_ATTR_CONTENT_DECORATOR) {
            rb_str_cat2(mesg, pre); pre = ",";
            rb_str_cat2(mesg, "xml_attr_content");
        }
        if (ecflags & ECONV_XML_ATTR_QUOTE_DECORATOR) {
            rb_str_cat2(mesg, pre); pre = ",";
            rb_str_cat2(mesg, "xml_attr_quote");
        }
        has_description = 1;
    }
    if (!has_description) {
        rb_str_cat2(mesg, "no-conversion");
    }

    return mesg;
}

VALUE
rb_econv_open_exc(const char *sname, const char *dname, int ecflags)
{
    VALUE mesg, exc;
    mesg = rb_str_new_cstr("code converter not found (");
    econv_description(sname, dname, ecflags, mesg);
    rb_str_cat2(mesg, ")");
    exc = rb_exc_new3(rb_eConverterNotFoundError, mesg);
    return exc;
}

static VALUE
make_econv_exception(rb_econv_t *ec)
{
    VALUE mesg, exc;
    if (ec->last_error.result == econv_invalid_byte_sequence ||
        ec->last_error.result == econv_incomplete_input) {
        const char *err = (const char *)ec->last_error.error_bytes_start;
        size_t error_len = ec->last_error.error_bytes_len;
        VALUE bytes = rb_str_new(err, error_len);
        VALUE dumped = rb_str_dump(bytes);
        size_t readagain_len = ec->last_error.readagain_len;
        VALUE bytes2 = Qnil;
        VALUE dumped2;
        int idx;
        if (ec->last_error.result == econv_incomplete_input) {
            mesg = rb_sprintf("incomplete %s on %s",
                    StringValueCStr(dumped),
                    ec->last_error.source_encoding);
        }
        else if (readagain_len) {
            bytes2 = rb_str_new(err+error_len, readagain_len);
            dumped2 = rb_str_dump(bytes2);
            mesg = rb_sprintf("%s followed by %s on %s",
                    StringValueCStr(dumped),
                    StringValueCStr(dumped2),
                    ec->last_error.source_encoding);
        }
        else {
            mesg = rb_sprintf("%s on %s",
                    StringValueCStr(dumped),
                    ec->last_error.source_encoding);
        }

        exc = rb_exc_new3(rb_eInvalidByteSequenceError, mesg);
        rb_ivar_set(exc, rb_intern("error_bytes"), bytes);
        rb_ivar_set(exc, rb_intern("readagain_bytes"), bytes2);
        rb_ivar_set(exc, rb_intern("incomplete_input"), ec->last_error.result == econv_incomplete_input ? Qtrue : Qfalse);

      set_encs:
        rb_ivar_set(exc, rb_intern("source_encoding_name"), rb_str_new2(ec->last_error.source_encoding));
        rb_ivar_set(exc, rb_intern("destination_encoding_name"), rb_str_new2(ec->last_error.destination_encoding));
        idx = rb_enc_find_index(ec->last_error.source_encoding);
        if (0 <= idx)
            rb_ivar_set(exc, rb_intern("source_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
        idx = rb_enc_find_index(ec->last_error.destination_encoding);
        if (0 <= idx)
            rb_ivar_set(exc, rb_intern("destination_encoding"), rb_enc_from_encoding(rb_enc_from_index(idx)));
        return exc;
    }
    if (ec->last_error.result == econv_undefined_conversion) {
        VALUE bytes = rb_str_new((const char *)ec->last_error.error_bytes_start,
                                 ec->last_error.error_bytes_len);
        VALUE dumped = Qnil;
        int idx;
        if (strcmp(ec->last_error.source_encoding, "UTF-8") == 0) {
            rb_encoding *utf8 = rb_utf8_encoding();
            const char *start, *end;
            int n;
            start = (const char *)ec->last_error.error_bytes_start;
            end = start + ec->last_error.error_bytes_len;
            n = rb_enc_precise_mbclen(start, end, utf8);
            if (MBCLEN_CHARFOUND_P(n) &&
                (size_t)MBCLEN_CHARFOUND_LEN(n) == ec->last_error.error_bytes_len) {
                unsigned int cc = rb_enc_mbc_to_codepoint(start, end, utf8);
                dumped = rb_sprintf("U+%04X", cc);
            }
        }
        if (dumped == Qnil)
            dumped = rb_str_dump(bytes);
        if (strcmp(ec->last_error.source_encoding,
                   ec->source_encoding_name) == 0 &&
            strcmp(ec->last_error.destination_encoding,
                   ec->destination_encoding_name) == 0) {
            mesg = rb_sprintf("%s from %s to %s",
                    StringValueCStr(dumped),
                    ec->last_error.source_encoding,
                    ec->last_error.destination_encoding);
        }
        else {
            int i;
            mesg = rb_sprintf("%s to %s in conversion from %s",
                    StringValueCStr(dumped),
                    ec->last_error.destination_encoding,
                    ec->source_encoding_name);
            for (i = 0; i < ec->num_trans; i++) {
                const rb_transcoder *tr = ec->elems[i].tc->transcoder;
                if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding))
                    rb_str_catf(mesg, " to %s",
                                ec->elems[i].tc->transcoder->dst_encoding);
            }
        }
        exc = rb_exc_new3(rb_eUndefinedConversionError, mesg);
        idx = rb_enc_find_index(ec->last_error.source_encoding);
        if (0 <= idx)
            rb_enc_associate_index(bytes, idx);
        rb_ivar_set(exc, rb_intern("error_char"), bytes);
        goto set_encs;
    }
    return Qnil;
}

static void
more_output_buffer(
        VALUE destination,
        unsigned char *(*resize_destination)(VALUE, size_t, size_t),
        int max_output,
        unsigned char **out_start_ptr,
        unsigned char **out_pos,
        unsigned char **out_stop_ptr)
{
    size_t len = (*out_pos - *out_start_ptr);
    size_t new_len = (len + max_output) * 2;
    *out_start_ptr = resize_destination(destination, len, new_len);
    *out_pos = *out_start_ptr + len;
    *out_stop_ptr = *out_start_ptr + new_len;
}

static int
make_replacement(rb_econv_t *ec)
{
    rb_transcoding *tc;
    const rb_transcoder *tr;
    const unsigned char *replacement;
    const char *repl_enc;
    const char *ins_enc;
    size_t len;

    if (ec->replacement_str)
        return 0;

    ins_enc = rb_econv_encoding_to_insert_output(ec);

    tc = ec->last_tc;
    if (*ins_enc) {
        tr = tc->transcoder;
        rb_enc_find(tr->dst_encoding);
        replacement = (const unsigned char *)get_replacement_character(ins_enc, &len, &repl_enc);
    }
    else {
        replacement = (unsigned char *)"?";
        len = 1;
        repl_enc = "";
    }

    ec->replacement_str = replacement;
    ec->replacement_len = len;
    ec->replacement_enc = repl_enc;
    ec->replacement_allocated = 0;
    return 0;
}

int
rb_econv_set_replacement(rb_econv_t *ec,
    const unsigned char *str, size_t len, const char *encname)
{
    unsigned char *str2;
    size_t len2;
    const char *encname2;

    encname2 = rb_econv_encoding_to_insert_output(ec);

    if (!*encname2 || encoding_equal(encname, encname2)) {
        str2 = xmalloc(len);
        MEMCPY(str2, str, unsigned char, len); /* xxx: str may be invalid */
        len2 = len;
        encname2 = encname;
    }
    else {
        str2 = allocate_converted_string(encname, encname2, str, len, NULL, 0, &len2);
        if (!str2)
            return -1;
    }

    if (ec->replacement_allocated) {
        xfree((void *)ec->replacement_str);
    }
    ec->replacement_allocated = 1;
    ec->replacement_str = str2;
    ec->replacement_len = len2;
    ec->replacement_enc = encname2;
    return 0;
}

static int
output_replacement_character(rb_econv_t *ec)
{
    int ret;

    if (make_replacement(ec) == -1)
        return -1;

    ret = rb_econv_insert_output(ec, ec->replacement_str, ec->replacement_len, ec->replacement_enc);
    if (ret == -1)
        return -1;

    return 0;
}

#if 1
#define hash_fallback rb_hash_aref

static VALUE
proc_fallback(VALUE fallback, VALUE c)
{
    return rb_proc_call(fallback, rb_ary_new4(1, &c));
}

static VALUE
method_fallback(VALUE fallback, VALUE c)
{
    return rb_method_call(1, &c, fallback);
}

static VALUE
aref_fallback(VALUE fallback, VALUE c)
{
    return rb_funcall3(fallback, sym_aref, 1, &c);
}

static void
transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
	       const unsigned char *in_stop, unsigned char *out_stop,
               VALUE destination,
               unsigned char *(*resize_destination)(VALUE, size_t, size_t),
               const char *src_encoding,
               const char *dst_encoding,
               int ecflags,
               VALUE ecopts)
{
    rb_econv_t *ec;
    rb_transcoding *last_tc;
    rb_econv_result_t ret;
    unsigned char *out_start = *out_pos;
    int max_output;
    VALUE exc;
    VALUE fallback = Qnil;
    VALUE (*fallback_func)(VALUE, VALUE) = 0;

    ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
    if (!ec)
        rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));

    if (!NIL_P(ecopts) && RB_TYPE_P(ecopts, T_HASH)) {
	fallback = rb_hash_aref(ecopts, sym_fallback);
	if (RB_TYPE_P(fallback, T_HASH)) {
	    fallback_func = hash_fallback;
	}
	else if (rb_obj_is_proc(fallback)) {
	    fallback_func = proc_fallback;
	}
	else if (rb_obj_is_method(fallback)) {
	    fallback_func = method_fallback;
	}
	else {
	    fallback_func = aref_fallback;
	}
    }
    last_tc = ec->last_tc;
    max_output = last_tc ? last_tc->transcoder->max_output : 1;

  resume:
    ret = rb_econv_convert(ec, in_pos, in_stop, out_pos, out_stop, 0);

    if (!NIL_P(fallback) && ret == econv_undefined_conversion) {
	VALUE rep = rb_enc_str_new(
		(const char *)ec->last_error.error_bytes_start,
		ec->last_error.error_bytes_len,
		rb_enc_find(ec->last_error.source_encoding));
	rep = (*fallback_func)(fallback, rep);
	if (rep != Qundef && !NIL_P(rep)) {
	    StringValue(rep);
	    ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(rep),
		    RSTRING_LEN(rep), rb_enc_name(rb_enc_get(rep)));
	    if ((int)ret == -1) {
		rb_raise(rb_eArgError, "too big fallback string");
	    }
	    goto resume;
	}
    }

    if (ret == econv_invalid_byte_sequence ||
        ret == econv_incomplete_input ||
        ret == econv_undefined_conversion) {
        exc = make_econv_exception(ec);
        rb_econv_close(ec);
	rb_exc_raise(exc);
    }

    if (ret == econv_destination_buffer_full) {
        more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
        goto resume;
    }

    rb_econv_close(ec);
    return;
}
#else
/* sample transcode_loop implementation in byte-by-byte stream style */
static void
transcode_loop(const unsigned char **in_pos, unsigned char **out_pos,
	       const unsigned char *in_stop, unsigned char *out_stop,
               VALUE destination,
               unsigned char *(*resize_destination)(VALUE, size_t, size_t),
               const char *src_encoding,
               const char *dst_encoding,
               int ecflags,
               VALUE ecopts)
{
    rb_econv_t *ec;
    rb_transcoding *last_tc;
    rb_econv_result_t ret;
    unsigned char *out_start = *out_pos;
    const unsigned char *ptr;
    int max_output;
    VALUE exc;

    ec = rb_econv_open_opts(src_encoding, dst_encoding, ecflags, ecopts);
    if (!ec)
        rb_exc_raise(rb_econv_open_exc(src_encoding, dst_encoding, ecflags));

    last_tc = ec->last_tc;
    max_output = last_tc ? last_tc->transcoder->max_output : 1;

    ret = econv_source_buffer_empty;
    ptr = *in_pos;
    while (ret != econv_finished) {
        unsigned char input_byte;
        const unsigned char *p = &input_byte;

        if (ret == econv_source_buffer_empty) {
            if (ptr < in_stop) {
                input_byte = *ptr;
                ret = rb_econv_convert(ec, &p, p+1, out_pos, out_stop, ECONV_PARTIAL_INPUT);
            }
            else {
                ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, 0);
            }
        }
        else {
            ret = rb_econv_convert(ec, NULL, NULL, out_pos, out_stop, ECONV_PARTIAL_INPUT);
        }
        if (&input_byte != p)
            ptr += p - &input_byte;
        switch (ret) {
          case econv_invalid_byte_sequence:
          case econv_incomplete_input:
          case econv_undefined_conversion:
            exc = make_econv_exception(ec);
            rb_econv_close(ec);
            rb_exc_raise(exc);
            break;

          case econv_destination_buffer_full:
            more_output_buffer(destination, resize_destination, max_output, &out_start, out_pos, &out_stop);
            break;

          case econv_source_buffer_empty:
            break;

          case econv_finished:
            break;
        }
    }
    rb_econv_close(ec);
    *in_pos = in_stop;
    return;
}
#endif


/*
 *  String-specific code
 */

static unsigned char *
str_transcoding_resize(VALUE destination, size_t len, size_t new_len)
{
    rb_str_resize(destination, new_len);
    return (unsigned char *)RSTRING_PTR(destination);
}

static int
econv_opts(VALUE opt, int ecflags)
{
    VALUE v;

    v = rb_hash_aref(opt, sym_invalid);
    if (NIL_P(v)) {
    }
    else if (v==sym_replace) {
        ecflags |= ECONV_INVALID_REPLACE;
    }
    else {
        rb_raise(rb_eArgError, "unknown value for invalid character option");
    }

    v = rb_hash_aref(opt, sym_undef);
    if (NIL_P(v)) {
    }
    else if (v==sym_replace) {
        ecflags |= ECONV_UNDEF_REPLACE;
    }
    else {
        rb_raise(rb_eArgError, "unknown value for undefined character option");
    }

    v = rb_hash_aref(opt, sym_replace);
    if (!NIL_P(v) && !(ecflags & ECONV_INVALID_REPLACE)) {
        ecflags |= ECONV_UNDEF_REPLACE;
    }

    v = rb_hash_aref(opt, sym_xml);
    if (!NIL_P(v)) {
        if (v==sym_text) {
            ecflags |= ECONV_XML_TEXT_DECORATOR|ECONV_UNDEF_HEX_CHARREF;
        }
        else if (v==sym_attr) {
            ecflags |= ECONV_XML_ATTR_CONTENT_DECORATOR|ECONV_XML_ATTR_QUOTE_DECORATOR|ECONV_UNDEF_HEX_CHARREF;
        }
        else if (RB_TYPE_P(v, T_SYMBOL)) {
            rb_raise(rb_eArgError, "unexpected value for xml option: %"PRIsVALUE, rb_sym2str(v));
        }
        else {
            rb_raise(rb_eArgError, "unexpected value for xml option");
        }
    }

#ifdef ENABLE_ECONV_NEWLINE_OPTION
    v = rb_hash_aref(opt, sym_newline);
    if (!NIL_P(v)) {
	ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
	if (v == sym_universal) {
	    ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
	}
	else if (v == sym_crlf) {
	    ecflags |= ECONV_CRLF_NEWLINE_DECORATOR;
	}
	else if (v == sym_cr) {
	    ecflags |= ECONV_CR_NEWLINE_DECORATOR;
	}
	else if (v == sym_lf) {
	    /* ecflags |= ECONV_LF_NEWLINE_DECORATOR; */
	}
	else if (SYMBOL_P(v)) {
	    rb_raise(rb_eArgError, "unexpected value for newline option: %"PRIsVALUE,
		     rb_sym2str(v));
	}
	else {
	    rb_raise(rb_eArgError, "unexpected value for newline option");
	}
    }
    else
#endif
    {
	int setflags = 0, newlineflag = 0;

	v = rb_hash_aref(opt, sym_universal_newline);
	if (RTEST(v))
	    setflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
	newlineflag |= !NIL_P(v);

	v = rb_hash_aref(opt, sym_crlf_newline);
	if (RTEST(v))
	    setflags |= ECONV_CRLF_NEWLINE_DECORATOR;
	newlineflag |= !NIL_P(v);

	v = rb_hash_aref(opt, sym_cr_newline);
	if (RTEST(v))
	    setflags |= ECONV_CR_NEWLINE_DECORATOR;
	newlineflag |= !NIL_P(v);

	if (newlineflag) {
	    ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
	    ecflags |= setflags;
	}
    }

    return ecflags;
}

int
rb_econv_prepare_options(VALUE opthash, VALUE *opts, int ecflags)
{
    VALUE newhash = Qnil;
    VALUE v;

    if (NIL_P(opthash)) {
        *opts = Qnil;
        return ecflags;
    }
    ecflags = econv_opts(opthash, ecflags);

    v = rb_hash_aref(opthash, sym_replace);
    if (!NIL_P(v)) {
	StringValue(v);
	if (rb_enc_str_coderange(v) == ENC_CODERANGE_BROKEN) {
	    VALUE dumped = rb_str_dump(v);
	    rb_raise(rb_eArgError, "replacement string is broken: %s as %s",
		     StringValueCStr(dumped),
		     rb_enc_name(rb_enc_get(v)));
	}
	v = rb_str_new_frozen(v);
	newhash = rb_hash_new();
	rb_hash_aset(newhash, sym_replace, v);
    }

    v = rb_hash_aref(opthash, sym_fallback);
    if (!NIL_P(v)) {
	VALUE h = rb_check_hash_type(v);
	if (NIL_P(h)
	    ? (rb_obj_is_proc(v) || rb_obj_is_method(v) || rb_respond_to(v, sym_aref))
	    : (v = h, 1)) {
	    if (NIL_P(newhash))
		newhash = rb_hash_new();
	    rb_hash_aset(newhash, sym_fallback, v);
	}
    }

    if (!NIL_P(newhash))
        rb_hash_freeze(newhash);
    *opts = newhash;

    return ecflags;
}

int
rb_econv_prepare_opts(VALUE opthash, VALUE *opts)
{
    return rb_econv_prepare_options(opthash, opts, 0);
}

rb_econv_t *
rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE opthash)
{
    rb_econv_t *ec;
    VALUE replacement;

    if (NIL_P(opthash)) {
        replacement = Qnil;
    }
    else {
        if (!RB_TYPE_P(opthash, T_HASH) || !OBJ_FROZEN(opthash))
            rb_bug("rb_econv_open_opts called with invalid opthash");
        replacement = rb_hash_aref(opthash, sym_replace);
    }

    ec = rb_econv_open(source_encoding, destination_encoding, ecflags);
    if (!ec)
        return ec;

    if (!NIL_P(replacement)) {
        int ret;
        rb_encoding *enc = rb_enc_get(replacement);

        ret = rb_econv_set_replacement(ec,
                (const unsigned char *)RSTRING_PTR(replacement),
                RSTRING_LEN(replacement),
                rb_enc_name(enc));
        if (ret == -1) {
            rb_econv_close(ec);
            return NULL;
        }
    }
    return ec;
}

static int
enc_arg(VALUE *arg, const char **name_p, rb_encoding **enc_p)
{
    rb_encoding *enc;
    const char *n;
    int encidx;
    VALUE encval;

    if (((encidx = rb_to_encoding_index(encval = *arg)) < 0) ||
	!(enc = rb_enc_from_index(encidx))) {
	enc = NULL;
	encidx = 0;
	n = StringValueCStr(*arg);
    }
    else {
	n = rb_enc_name(enc);
    }

    *name_p = n;
    *enc_p = enc;

    return encidx;
}

static int
str_transcode_enc_args(VALUE str, VALUE *arg1, VALUE *arg2,
        const char **sname_p, rb_encoding **senc_p,
        const char **dname_p, rb_encoding **denc_p)
{
    rb_encoding *senc, *denc;
    const char *sname, *dname;
    int sencidx, dencidx;

    dencidx = enc_arg(arg1, &dname, &denc);

    if (NIL_P(*arg2)) {
	sencidx = rb_enc_get_index(str);
	senc = rb_enc_from_index(sencidx);
	sname = rb_enc_name(senc);
    }
    else {
        sencidx = enc_arg(arg2, &sname, &senc);
    }

    *sname_p = sname;
    *senc_p = senc;
    *dname_p = dname;
    *denc_p = denc;
    return dencidx;
}

static int
str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts)
{
    VALUE dest;
    VALUE str = *self;
    VALUE arg1, arg2;
    long blen, slen;
    unsigned char *buf, *bp, *sp;
    const unsigned char *fromp;
    rb_encoding *senc, *denc;
    const char *sname, *dname;
    int dencidx;
    int explicitly_invalid_replace = TRUE;

    rb_check_arity(argc, 0, 2);

    if (argc == 0) {
	arg1 = rb_enc_default_internal();
	if (NIL_P(arg1)) {
	    if (!ecflags) return -1;
	    arg1 = rb_obj_encoding(str);
	}
	if (!(ecflags & ECONV_INVALID_MASK)) {
	    explicitly_invalid_replace = FALSE;
	}
	ecflags |= ECONV_INVALID_REPLACE | ECONV_UNDEF_REPLACE;
    }
    else {
	arg1 = argv[0];
    }
    arg2 = argc<=1 ? Qnil : argv[1];
    dencidx = str_transcode_enc_args(str, &arg1, &arg2, &sname, &senc, &dname, &denc);

    if ((ecflags & (ECONV_NEWLINE_DECORATOR_MASK|
                    ECONV_XML_TEXT_DECORATOR|
                    ECONV_XML_ATTR_CONTENT_DECORATOR|
                    ECONV_XML_ATTR_QUOTE_DECORATOR)) == 0) {
        if (senc && senc == denc) {
	    if ((ecflags & ECONV_INVALID_MASK) && explicitly_invalid_replace) {
		VALUE rep = Qnil;
		if (!NIL_P(ecopts)) {
		    rep = rb_hash_aref(ecopts, sym_replace);
		}
		dest = rb_enc_str_scrub(senc, str, rep);
		if (NIL_P(dest)) dest = str;
		*self = dest;
		return dencidx;
	    }
            return NIL_P(arg2) ? -1 : dencidx;
        }
        if (senc && denc && rb_enc_asciicompat(senc) && rb_enc_asciicompat(denc)) {
            if (rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT) {
                return dencidx;
            }
        }
        if (encoding_equal(sname, dname)) {
            return NIL_P(arg2) ? -1 : dencidx;
        }
    }
    else {
        if (encoding_equal(sname, dname)) {
            sname = "";
            dname = "";
        }
    }

    fromp = sp = (unsigned char *)RSTRING_PTR(str);
    slen = RSTRING_LEN(str);
    blen = slen + 30; /* len + margin */
    dest = rb_str_tmp_new(blen);
    bp = (unsigned char *)RSTRING_PTR(dest);

    transcode_loop(&fromp, &bp, (sp+slen), (bp+blen), dest, str_transcoding_resize, sname, dname, ecflags, ecopts);
    if (fromp != sp+slen) {
        rb_raise(rb_eArgError, "not fully converted, %"PRIdPTRDIFF" bytes left", sp+slen-fromp);
    }
    buf = (unsigned char *)RSTRING_PTR(dest);
    *bp = '\0';
    rb_str_set_len(dest, bp - buf);

    /* set encoding */
    if (!denc) {
	dencidx = rb_define_dummy_encoding(dname);
	RB_GC_GUARD(arg1);
	RB_GC_GUARD(arg2);
    }
    *self = dest;

    return dencidx;
}

static int
str_transcode(int argc, VALUE *argv, VALUE *self)
{
    VALUE opt;
    int ecflags = 0;
    VALUE ecopts = Qnil;

    argc = rb_scan_args(argc, argv, "02:", NULL, NULL, &opt);
    if (!NIL_P(opt)) {
	ecflags = rb_econv_prepare_opts(opt, &ecopts);
    }
    return str_transcode0(argc, argv, self, ecflags, ecopts);
}

static inline VALUE
str_encode_associate(VALUE str, int encidx)
{
    int cr = 0;

    rb_enc_associate_index(str, encidx);

    /* transcoded string never be broken. */
    if (rb_enc_asciicompat(rb_enc_from_index(encidx))) {
	rb_str_coderange_scan_restartable(RSTRING_PTR(str), RSTRING_END(str), 0, &cr);
    }
    else {
	cr = ENC_CODERANGE_VALID;
    }
    ENC_CODERANGE_SET(str, cr);
    return str;
}

/*
 *  call-seq:
 *     str.encode!(encoding [, options] )   -> str
 *     str.encode!(dst_encoding, src_encoding [, options] )   -> str
 *
 *  The first form transcodes the contents of <i>str</i> from
 *  str.encoding to +encoding+.
 *  The second form transcodes the contents of <i>str</i> from
 *  src_encoding to dst_encoding.
 *  The options Hash gives details for conversion. See String#encode
 *  for details.
 *  Returns the string even if no changes were made.
 */

static VALUE
str_encode_bang(int argc, VALUE *argv, VALUE str)
{
    VALUE newstr;
    int encidx;

    rb_check_frozen(str);

    newstr = str;
    encidx = str_transcode(argc, argv, &newstr);

    if (encidx < 0) return str;
    if (newstr == str) {
	rb_enc_associate_index(str, encidx);
	return str;
    }
    rb_str_shared_replace(str, newstr);
    return str_encode_associate(str, encidx);
}

static VALUE encoded_dup(VALUE newstr, VALUE str, int encidx);

/*
 *  call-seq:
 *     str.encode(encoding [, options] )   -> str
 *     str.encode(dst_encoding, src_encoding [, options] )   -> str
 *     str.encode([options])   -> str
 *
 *  The first form returns a copy of +str+ transcoded
 *  to encoding +encoding+.
 *  The second form returns a copy of +str+ transcoded
 *  from src_encoding to dst_encoding.
 *  The last form returns a copy of +str+ transcoded to
 *  <tt>Encoding.default_internal</tt>.
 *
 *  By default, the first and second form raise
 *  Encoding::UndefinedConversionError for characters that are
 *  undefined in the destination encoding, and
 *  Encoding::InvalidByteSequenceError for invalid byte sequences
 *  in the source encoding. The last form by default does not raise
 *  exceptions but uses replacement strings.
 *
 *  The +options+ Hash gives details for conversion and can have the following
 *  keys:
 *
 *  :invalid ::
 *    If the value is +:replace+, #encode replaces invalid byte sequences in
 *    +str+ with the replacement character.  The default is to raise the
 *    Encoding::InvalidByteSequenceError exception
 *  :undef ::
 *    If the value is +:replace+, #encode replaces characters which are
 *    undefined in the destination encoding with the replacement character.
 *    The default is to raise the Encoding::UndefinedConversionError.
 *  :replace ::
 *    Sets the replacement string to the given value. The default replacement
 *    string is "\uFFFD" for Unicode encoding forms, and "?" otherwise.
 *  :fallback ::
 *    Sets the replacement string by the given object for undefined
 *    character.  The object should be a Hash, a Proc, a Method, or an
 *    object which has [] method.
 *    Its key is an undefined character encoded in the source encoding
 *    of current transcoder. Its value can be any encoding until it
 *    can be converted into the destination encoding of the transcoder.
 *  :xml ::
 *    The value must be +:text+ or +:attr+.
 *    If the value is +:text+ #encode replaces undefined characters with their
 *    (upper-case hexadecimal) numeric character references. '&', '<', and '>'
 *    are converted to "&amp;", "&lt;", and "&gt;", respectively.
 *    If the value is +:attr+, #encode also quotes the replacement result
 *    (using '"'), and replaces '"' with "&quot;".
 *  :cr_newline ::
 *    Replaces LF ("\n") with CR ("\r") if value is true.
 *  :crlf_newline ::
 *    Replaces LF ("\n") with CRLF ("\r\n") if value is true.
 *  :universal_newline ::
 *    Replaces CRLF ("\r\n") and CR ("\r") with LF ("\n") if value is true.
 */

static VALUE
str_encode(int argc, VALUE *argv, VALUE str)
{
    VALUE newstr = str;
    int encidx = str_transcode(argc, argv, &newstr);
    return encoded_dup(newstr, str, encidx);
}

VALUE
rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
{
    int argc = 1;
    VALUE *argv = &to;
    VALUE newstr = str;
    int encidx = str_transcode0(argc, argv, &newstr, ecflags, ecopts);
    return encoded_dup(newstr, str, encidx);
}

static VALUE
encoded_dup(VALUE newstr, VALUE str, int encidx)
{
    if (encidx < 0) return rb_str_dup(str);
    if (newstr == str) {
	newstr = rb_str_dup(str);
	rb_enc_associate_index(newstr, encidx);
	return newstr;
    }
    else {
	RBASIC_SET_CLASS(newstr, rb_obj_class(str));
    }
    return str_encode_associate(newstr, encidx);
}

static void
econv_free(void *ptr)
{
    rb_econv_t *ec = ptr;
    rb_econv_close(ec);
}

static size_t
econv_memsize(const void *ptr)
{
    return sizeof(rb_econv_t);
}

static const rb_data_type_t econv_data_type = {
    "econv",
    {NULL, econv_free, econv_memsize,},
    0, 0, RUBY_TYPED_FREE_IMMEDIATELY
};

static VALUE
econv_s_allocate(VALUE klass)
{
    return TypedData_Wrap_Struct(klass, &econv_data_type, NULL);
}

static rb_encoding *
make_dummy_encoding(const char *name)
{
    rb_encoding *enc;
    int idx;
    idx = rb_define_dummy_encoding(name);
    enc = rb_enc_from_index(idx);
    return enc;
}

static rb_encoding *
make_encoding(const char *name)
{
    rb_encoding *enc;
    enc = rb_enc_find(name);
    if (!enc)
        enc = make_dummy_encoding(name);
    return enc;
}

static VALUE
make_encobj(const char *name)
{
    return rb_enc_from_encoding(make_encoding(name));
}

/*
 * call-seq:
 *   Encoding::Converter.asciicompat_encoding(string) -> encoding or nil
 *   Encoding::Converter.asciicompat_encoding(encoding) -> encoding or nil
 *
 * Returns the corresponding ASCII compatible encoding.
 *
 * Returns nil if the argument is an ASCII compatible encoding.
 *
 * "corresponding ASCII compatible encoding" is an ASCII compatible encoding which
 * can represents exactly the same characters as the given ASCII incompatible encoding.
 * So, no conversion undefined error occurs when converting between the two encodings.
 *
 *   Encoding::Converter.asciicompat_encoding("ISO-2022-JP") #=> #<Encoding:stateless-ISO-2022-JP>
 *   Encoding::Converter.asciicompat_encoding("UTF-16BE") #=> #<Encoding:UTF-8>
 *   Encoding::Converter.asciicompat_encoding("UTF-8") #=> nil
 *
 */
static VALUE
econv_s_asciicompat_encoding(VALUE klass, VALUE arg)
{
    const char *arg_name, *result_name;
    rb_encoding *arg_enc, *result_enc;

    enc_arg(&arg, &arg_name, &arg_enc);

    result_name = rb_econv_asciicompat_encoding(arg_name);

    if (result_name == NULL)
        return Qnil;

    result_enc = make_encoding(result_name);

    return rb_enc_from_encoding(result_enc);
}

static void
econv_args(int argc, VALUE *argv,
    VALUE *snamev_p, VALUE *dnamev_p,
    const char **sname_p, const char **dname_p,
    rb_encoding **senc_p, rb_encoding **denc_p,
    int *ecflags_p,
    VALUE *ecopts_p)
{
    VALUE opt, flags_v, ecopts;
    int sidx, didx;
    const char *sname, *dname;
    rb_encoding *senc, *denc;
    int ecflags;

    argc = rb_scan_args(argc, argv, "21:", snamev_p, dnamev_p, &flags_v, &opt);

    if (!NIL_P(flags_v)) {
	if (!NIL_P(opt)) {
	    rb_error_arity(argc + 1, 2, 3);
	}
        ecflags = NUM2INT(rb_to_int(flags_v));
        ecopts = Qnil;
    }
    else if (!NIL_P(opt)) {
        ecflags = rb_econv_prepare_opts(opt, &ecopts);
    }
    else {
        ecflags = 0;
        ecopts = Qnil;
    }

    senc = NULL;
    sidx = rb_to_encoding_index(*snamev_p);
    if (0 <= sidx) {
        senc = rb_enc_from_index(sidx);
    }
    else {
        StringValue(*snamev_p);
    }

    denc = NULL;
    didx = rb_to_encoding_index(*dnamev_p);
    if (0 <= didx) {
        denc = rb_enc_from_index(didx);
    }
    else {
        StringValue(*dnamev_p);
    }

    sname = senc ? rb_enc_name(senc) : StringValueCStr(*snamev_p);
    dname = denc ? rb_enc_name(denc) : StringValueCStr(*dnamev_p);

    *sname_p = sname;
    *dname_p = dname;
    *senc_p = senc;
    *denc_p = denc;
    *ecflags_p = ecflags;
    *ecopts_p = ecopts;
}

static int
decorate_convpath(VALUE convpath, int ecflags)
{
    int num_decorators;
    const char *decorators[MAX_ECFLAGS_DECORATORS];
    int i;
    int n, len;

    num_decorators = decorator_names(ecflags, decorators);
    if (num_decorators == -1)
        return -1;

    len = n = RARRAY_LENINT(convpath);
    if (n != 0) {
        VALUE pair = RARRAY_AREF(convpath, n-1);
	if (RB_TYPE_P(pair, T_ARRAY)) {
	    const char *sname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 0)));
	    const char *dname = rb_enc_name(rb_to_encoding(RARRAY_AREF(pair, 1)));
	    transcoder_entry_t *entry = get_transcoder_entry(sname, dname);
	    const rb_transcoder *tr = load_transcoder_entry(entry);
	    if (!tr)
		return -1;
	    if (!DECORATOR_P(tr->src_encoding, tr->dst_encoding) &&
		    tr->asciicompat_type == asciicompat_encoder) {
		n--;
		rb_ary_store(convpath, len + num_decorators - 1, pair);
	    }
	}
	else {
	    rb_ary_store(convpath, len + num_decorators - 1, pair);
	}
    }

    for (i = 0; i < num_decorators; i++)
        rb_ary_store(convpath, n + i, rb_str_new_cstr(decorators[i]));

    return 0;
}

static void
search_convpath_i(const char *sname, const char *dname, int depth, void *arg)
{
    VALUE *ary_p = arg;
    VALUE v;

    if (*ary_p == Qnil) {
        *ary_p = rb_ary_new();
    }

    if (DECORATOR_P(sname, dname)) {
        v = rb_str_new_cstr(dname);
    }
    else {
        v = rb_assoc_new(make_encobj(sname), make_encobj(dname));
    }
    rb_ary_store(*ary_p, depth, v);
}

/*
 * call-seq:
 *   Encoding::Converter.search_convpath(source_encoding, destination_encoding)         -> ary
 *   Encoding::Converter.search_convpath(source_encoding, destination_encoding, opt)    -> ary
 *
 *  Returns a conversion path.
 *
 *   p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP")
 *   #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
 *   #    [#<Encoding:UTF-8>, #<Encoding:EUC-JP>]]
 *
 *   p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", universal_newline: true)
 *   or
 *   p Encoding::Converter.search_convpath("ISO-8859-1", "EUC-JP", newline: :universal)
 *   #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
 *   #    [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
 *   #    "universal_newline"]
 *
 *   p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", universal_newline: true)
 *   or
 *   p Encoding::Converter.search_convpath("ISO-8859-1", "UTF-32BE", newline: :universal)
 *   #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
 *   #    "universal_newline",
 *   #    [#<Encoding:UTF-8>, #<Encoding:UTF-32BE>]]
 */
static VALUE
econv_s_search_convpath(int argc, VALUE *argv, VALUE klass)
{
    VALUE snamev, dnamev;
    const char *sname, *dname;
    rb_encoding *senc, *denc;
    int ecflags;
    VALUE ecopts;
    VALUE convpath;

    econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);

    convpath = Qnil;
    transcode_search_path(sname, dname, search_convpath_i, &convpath);

    if (NIL_P(convpath))
        rb_exc_raise(rb_econv_open_exc(sname, dname, ecflags));

    if (decorate_convpath(convpath, ecflags) == -1) {
	VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
	RB_GC_GUARD(snamev);
	RB_GC_GUARD(dnamev);
	rb_exc_raise(exc);
    }

    return convpath;
}

/*
 * Check the existence of a conversion path.
 * Returns the number of converters in the conversion path.
 * result: >=0:success -1:failure
 */
int
rb_econv_has_convpath_p(const char* from_encoding, const char* to_encoding)
{
    VALUE convpath = Qnil;
    transcode_search_path(from_encoding, to_encoding, search_convpath_i,
			  &convpath);
    return RTEST(convpath);
}

struct rb_econv_init_by_convpath_t {
    rb_econv_t *ec;
    int index;
    int ret;
};

static void
rb_econv_init_by_convpath_i(const char *sname, const char *dname, int depth, void *arg)
{
    struct rb_econv_init_by_convpath_t *a = (struct rb_econv_init_by_convpath_t *)arg;
    int ret;

    if (a->ret == -1)
        return;

    ret = rb_econv_add_converter(a->ec, sname, dname, a->index);

    a->ret = ret;
    return;
}

static rb_econv_t *
rb_econv_init_by_convpath(VALUE self, VALUE convpath,
    const char **sname_p, const char **dname_p,
    rb_encoding **senc_p, rb_encoding**denc_p)
{
    rb_econv_t *ec;
    long i;
    int ret, first=1;
    VALUE elt;
    rb_encoding *senc = 0, *denc = 0;
    const char *sname, *dname;

    ec = rb_econv_alloc(RARRAY_LENINT(convpath));
    DATA_PTR(self) = ec;

    for (i = 0; i < RARRAY_LEN(convpath); i++) {
        VALUE snamev, dnamev;
        VALUE pair;
        elt = rb_ary_entry(convpath, i);
        if (!NIL_P(pair = rb_check_array_type(elt))) {
            if (RARRAY_LEN(pair) != 2)
                rb_raise(rb_eArgError, "not a 2-element array in convpath");
            snamev = rb_ary_entry(pair, 0);
            enc_arg(&snamev, &sname, &senc);
            dnamev = rb_ary_entry(pair, 1);
            enc_arg(&dnamev, &dname, &denc);
        }
        else {
            sname = "";
            dname = StringValueCStr(elt);
        }
        if (DECORATOR_P(sname, dname)) {
            ret = rb_econv_add_converter(ec, sname, dname, ec->num_trans);
	    if (ret == -1) {
		VALUE msg = rb_sprintf("decoration failed: %s", dname);
		RB_GC_GUARD(snamev);
		RB_GC_GUARD(dnamev);
		rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
	    }
        }
        else {
            int j = ec->num_trans;
            struct rb_econv_init_by_convpath_t arg;
            arg.ec = ec;
            arg.index = ec->num_trans;
            arg.ret = 0;
            ret = transcode_search_path(sname, dname, rb_econv_init_by_convpath_i, &arg);
	    if (ret == -1 || arg.ret == -1) {
		VALUE msg = rb_sprintf("adding conversion failed: %s to %s", sname, dname);
		RB_GC_GUARD(snamev);
		RB_GC_GUARD(dnamev);
                rb_exc_raise(rb_exc_new_str(rb_eArgError, msg));
	    }
            if (first) {
                first = 0;
                *senc_p = senc;
                *sname_p = ec->elems[j].tc->transcoder->src_encoding;
            }
            *denc_p = denc;
            *dname_p = ec->elems[ec->num_trans-1].tc->transcoder->dst_encoding;
        }
    }

    if (first) {
	*senc_p = NULL;
	*denc_p = NULL;
	*sname_p = "";
	*dname_p = "";
    }

    ec->source_encoding_name = *sname_p;
    ec->destination_encoding_name = *dname_p;

    return ec;
}

/*
 * call-seq:
 *   Encoding::Converter.new(source_encoding, destination_encoding)
 *   Encoding::Converter.new(source_encoding, destination_encoding, opt)
 *   Encoding::Converter.new(convpath)
 *
 * possible options elements:
 *   hash form:
 *     :invalid => nil            # raise error on invalid byte sequence (default)
 *     :invalid => :replace       # replace invalid byte sequence
 *     :undef => nil              # raise error on undefined conversion (default)
 *     :undef => :replace         # replace undefined conversion
 *     :replace => string         # replacement string ("?" or "\uFFFD" if not specified)
 *     :newline => :universal     # decorator for converting CRLF and CR to LF
 *     :newline => :crlf          # decorator for converting LF to CRLF
 *     :newline => :cr            # decorator for converting LF to CR
 *     :universal_newline => true # decorator for converting CRLF and CR to LF
 *     :crlf_newline => true      # decorator for converting LF to CRLF
 *     :cr_newline => true        # decorator for converting LF to CR
 *     :xml => :text              # escape as XML CharData.
 *     :xml => :attr              # escape as XML AttValue
 *   integer form:
 *     Encoding::Converter::INVALID_REPLACE
 *     Encoding::Converter::UNDEF_REPLACE
 *     Encoding::Converter::UNDEF_HEX_CHARREF
 *     Encoding::Converter::UNIVERSAL_NEWLINE_DECORATOR
 *     Encoding::Converter::CRLF_NEWLINE_DECORATOR
 *     Encoding::Converter::CR_NEWLINE_DECORATOR
 *     Encoding::Converter::XML_TEXT_DECORATOR
 *     Encoding::Converter::XML_ATTR_CONTENT_DECORATOR
 *     Encoding::Converter::XML_ATTR_QUOTE_DECORATOR
 *
 * Encoding::Converter.new creates an instance of Encoding::Converter.
 *
 * Source_encoding and destination_encoding should be a string or
 * Encoding object.
 *
 * opt should be nil, a hash or an integer.
 *
 * convpath should be an array.
 * convpath may contain
 * - two-element arrays which contain encodings or encoding names, or
 * - strings representing decorator names.
 *
 * Encoding::Converter.new optionally takes an option.
 * The option should be a hash or an integer.
 * The option hash can contain :invalid => nil, etc.
 * The option integer should be logical-or of constants such as
 * Encoding::Converter::INVALID_REPLACE, etc.
 *
 * [:invalid => nil]
 *   Raise error on invalid byte sequence.  This is a default behavior.
 * [:invalid => :replace]
 *   Replace invalid byte sequence by replacement string.
 * [:undef => nil]
 *   Raise an error if a character in source_encoding is not defined in destination_encoding.
 *   This is a default behavior.
 * [:undef => :replace]
 *   Replace undefined character in destination_encoding with replacement string.
 * [:replace => string]
 *   Specify the replacement string.
 *   If not specified, "\uFFFD" is used for Unicode encodings and "?" for others.
 * [:universal_newline => true]
 *   Convert CRLF and CR to LF.
 * [:crlf_newline => true]
 *   Convert LF to CRLF.
 * [:cr_newline => true]
 *   Convert LF to CR.
 * [:xml => :text]
 *   Escape as XML CharData.
 *   This form can be used as an HTML 4.0 #PCDATA.
 *   - '&' -> '&amp;'
 *   - '<' -> '&lt;'
 *   - '>' -> '&gt;'
 *   - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
 * [:xml => :attr]
 *   Escape as XML AttValue.
 *   The converted result is quoted as "...".
 *   This form can be used as an HTML 4.0 attribute value.
 *   - '&' -> '&amp;'
 *   - '<' -> '&lt;'
 *   - '>' -> '&gt;'
 *   - '"' -> '&quot;'
 *   - undefined characters in destination_encoding -> hexadecimal CharRef such as &#xHH;
 *
 * Examples:
 *   # UTF-16BE to UTF-8
 *   ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
 *
 *   # Usually, decorators such as newline conversion are inserted last.
 *   ec = Encoding::Converter.new("UTF-16BE", "UTF-8", :universal_newline => true)
 *   p ec.convpath #=> [[#<Encoding:UTF-16BE>, #<Encoding:UTF-8>],
 *                 #    "universal_newline"]
 *
 *   # But, if the last encoding is ASCII incompatible,
 *   # decorators are inserted before the last conversion.
 *   ec = Encoding::Converter.new("UTF-8", "UTF-16BE", :crlf_newline => true)
 *   p ec.convpath #=> ["crlf_newline",
 *                 #    [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
 *
 *   # Conversion path can be specified directly.
 *   ec = Encoding::Converter.new(["universal_newline", ["EUC-JP", "UTF-8"], ["UTF-8", "UTF-16BE"]])
 *   p ec.convpath #=> ["universal_newline",
 *                 #    [#<Encoding:EUC-JP>, #<Encoding:UTF-8>],
 *                 #    [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]
 */
static VALUE
econv_init(int argc, VALUE *argv, VALUE self)
{
    VALUE ecopts;
    VALUE snamev, dnamev;
    const char *sname, *dname;
    rb_encoding *senc, *denc;
    rb_econv_t *ec;
    int ecflags;
    VALUE convpath;

    if (rb_check_typeddata(self, &econv_data_type)) {
        rb_raise(rb_eTypeError, "already initialized");
    }

    if (argc == 1 && !NIL_P(convpath = rb_check_array_type(argv[0]))) {
        ec = rb_econv_init_by_convpath(self, convpath, &sname, &dname, &senc, &denc);
        ecflags = 0;
        ecopts = Qnil;
    }
    else {
        econv_args(argc, argv, &snamev, &dnamev, &sname, &dname, &senc, &denc, &ecflags, &ecopts);
        ec = rb_econv_open_opts(sname, dname, ecflags, ecopts);
    }

    if (!ec) {
	VALUE exc = rb_econv_open_exc(sname, dname, ecflags);
	RB_GC_GUARD(snamev);
	RB_GC_GUARD(dnamev);
	rb_exc_raise(exc);
    }

    if (!DECORATOR_P(sname, dname)) {
        if (!senc)
            senc = make_dummy_encoding(sname);
        if (!denc)
            denc = make_dummy_encoding(dname);
	RB_GC_GUARD(snamev);
	RB_GC_GUARD(dnamev);
    }

    ec->source_encoding = senc;
    ec->destination_encoding = denc;

    DATA_PTR(self) = ec;

    return self;
}

/*
 * call-seq:
 *   ec.inspect         -> string
 *
 * Returns a printable version of <i>ec</i>
 *
 *   ec = Encoding::Converter.new("iso-8859-1", "utf-8")
 *   puts ec.inspect    #=> #<Encoding::Converter: ISO-8859-1 to UTF-8>
 *
 */
static VALUE
econv_inspect(VALUE self)
{
    const char *cname = rb_obj_classname(self);
    rb_econv_t *ec;

    TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
    if (!ec)
        return rb_sprintf("#<%s: uninitialized>", cname);
    else {
        const char *sname = ec->source_encoding_name;
        const char *dname = ec->destination_encoding_name;
        VALUE str;
        str = rb_sprintf("#<%s: ", cname);
        econv_description(sname, dname, ec->flags, str);
        rb_str_cat2(str, ">");
        return str;
    }
}

static rb_econv_t *
check_econv(VALUE self)
{
    rb_econv_t *ec;

    TypedData_Get_Struct(self, rb_econv_t, &econv_data_type, ec);
    if (!ec) {
        rb_raise(rb_eTypeError, "uninitialized encoding converter");
    }
    return ec;
}

/*
 * call-seq:
 *   ec.source_encoding -> encoding
 *
 * Returns the source encoding as an Encoding object.
 */
static VALUE
econv_source_encoding(VALUE self)
{
    rb_econv_t *ec = check_econv(self);
    if (!ec->source_encoding)
        return Qnil;
    return rb_enc_from_encoding(ec->source_encoding);
}

/*
 * call-seq:
 *   ec.destination_encoding -> encoding
 *
 * Returns the destination encoding as an Encoding object.
 */
static VALUE
econv_destination_encoding(VALUE self)
{
    rb_econv_t *ec = check_econv(self);
    if (!ec->destination_encoding)
        return Qnil;
    return rb_enc_from_encoding(ec->destination_encoding);
}

/*
 * call-seq:
 *   ec.convpath        -> ary
 *
 * Returns the conversion path of ec.
 *
 * The result is an array of conversions.
 *
 *   ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP", crlf_newline: true)
 *   p ec.convpath
 *   #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
 *   #    [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
 *   #    "crlf_newline"]
 *
 * Each element of the array is a pair of encodings or a string.
 * A pair means an encoding conversion.
 * A string means a decorator.
 *
 * In the above example, [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] means
 * a converter from ISO-8859-1 to UTF-8.
 * "crlf_newline" means newline converter from LF to CRLF.
 */
static VALUE
econv_convpath(VALUE self)
{
    rb_econv_t *ec = check_econv(self);
    VALUE result;
    int i;

    result = rb_ary_new();
    for (i = 0; i < ec->num_trans; i++) {
        const rb_transcoder *tr = ec->elems[i].tc->transcoder;
        VALUE v;
        if (DECORATOR_P(tr->src_encoding, tr->dst_encoding))
            v = rb_str_new_cstr(tr->dst_encoding);
        else
            v = rb_assoc_new(make_encobj(tr->src_encoding), make_encobj(tr->dst_encoding));
        rb_ary_push(result, v);
    }
    return result;
}

/*
 * call-seq:
 *   ec == other        -> true or false
 */
static VALUE
econv_equal(VALUE self, VALUE other)
{
    rb_econv_t *ec1 = check_econv(self);
    rb_econv_t *ec2;
    int i;

    if (!rb_typeddata_is_kind_of(other, &econv_data_type)) {
	return Qnil;
    }
    ec2 = DATA_PTR(other);
    if (!ec2) return Qfalse;
    if (ec1->source_encoding_name != ec2->source_encoding_name &&
	strcmp(ec1->source_encoding_name, ec2->source_encoding_name))
	return Qfalse;
    if (ec1->destination_encoding_name != ec2->destination_encoding_name &&
	strcmp(ec1->destination_encoding_name, ec2->destination_encoding_name))
	return Qfalse;
    if (ec1->flags != ec2->flags) return Qfalse;
    if (ec1->replacement_enc != ec2->replacement_enc &&
	strcmp(ec1->replacement_enc, ec2->replacement_enc))
	return Qfalse;
    if (ec1->replacement_len != ec2->replacement_len) return Qfalse;
    if (ec1->replacement_str != ec2->replacement_str &&
	memcmp(ec1->replacement_str, ec2->replacement_str, ec2->replacement_len))
	return Qfalse;

    if (ec1->num_trans != ec2->num_trans) return Qfalse;
    for (i = 0; i < ec1->num_trans; i++) {
        if (ec1->elems[i].tc->transcoder != ec2->elems[i].tc->transcoder)
	    return Qfalse;
    }
    return Qtrue;
}

static VALUE
econv_result_to_symbol(rb_econv_result_t res)
{
    switch (res) {
      case econv_invalid_byte_sequence: return sym_invalid_byte_sequence;
      case econv_incomplete_input: return sym_incomplete_input;
      case econv_undefined_conversion: return sym_undefined_conversion;
      case econv_destination_buffer_full: return sym_destination_buffer_full;
      case econv_source_buffer_empty: return sym_source_buffer_empty;
      case econv_finished: return sym_finished;
      case econv_after_output: return sym_after_output;
      default: return INT2NUM(res); /* should not be reached */
    }
}

/*
 * call-seq:
 *   ec.primitive_convert(source_buffer, destination_buffer) -> symbol
 *   ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset) -> symbol
 *   ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize) -> symbol
 *   ec.primitive_convert(source_buffer, destination_buffer, destination_byteoffset, destination_bytesize, opt) -> symbol
 *
 * possible opt elements:
 *   hash form:
 *     :partial_input => true           # source buffer may be part of larger source
 *     :after_output => true            # stop conversion after output before input
 *   integer form:
 *     Encoding::Converter::PARTIAL_INPUT
 *     Encoding::Converter::AFTER_OUTPUT
 *
 * possible results:
 *    :invalid_byte_sequence
 *    :incomplete_input
 *    :undefined_conversion
 *    :after_output
 *    :destination_buffer_full
 *    :source_buffer_empty
 *    :finished
 *
 * primitive_convert converts source_buffer into destination_buffer.
 *
 * source_buffer should be a string or nil.
 * nil means an empty string.
 *
 * destination_buffer should be a string.
 *
 * destination_byteoffset should be an integer or nil.
 * nil means the end of destination_buffer.
 * If it is omitted, nil is assumed.
 *
 * destination_bytesize should be an integer or nil.
 * nil means unlimited.
 * If it is omitted, nil is assumed.
 *
 * opt should be nil, a hash or an integer.
 * nil means no flags.
 * If it is omitted, nil is assumed.
 *
 * primitive_convert converts the content of source_buffer from beginning
 * and store the result into destination_buffer.
 *
 * destination_byteoffset and destination_bytesize specify the region which
 * the converted result is stored.
 * destination_byteoffset specifies the start position in destination_buffer in bytes.
 * If destination_byteoffset is nil,
 * destination_buffer.bytesize is used for appending the result.
 * destination_bytesize specifies maximum number of bytes.
 * If destination_bytesize is nil,
 * destination size is unlimited.
 * After conversion, destination_buffer is resized to
 * destination_byteoffset + actually produced number of bytes.
 * Also destination_buffer's encoding is set to destination_encoding.
 *
 * primitive_convert drops the converted part of source_buffer.
 * the dropped part is converted in destination_buffer or
 * buffered in Encoding::Converter object.
 *
 * primitive_convert stops conversion when one of following condition met.
 * - invalid byte sequence found in source buffer (:invalid_byte_sequence)
 *   +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
 * - unexpected end of source buffer (:incomplete_input)
 *   this occur only when :partial_input is not specified.
 *   +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
 * - character not representable in output encoding (:undefined_conversion)
 *   +primitive_errinfo+ and +last_error+ methods returns the detail of the error.
 * - after some output is generated, before input is done (:after_output)
 *   this occur only when :after_output is specified.
 * - destination buffer is full (:destination_buffer_full)
 *   this occur only when destination_bytesize is non-nil.
 * - source buffer is empty (:source_buffer_empty)
 *   this occur only when :partial_input is specified.
 * - conversion is finished (:finished)
 *
 * example:
 *   ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
 *   ret = ec.primitive_convert(src="pi", dst="", nil, 100)
 *   p [ret, src, dst] #=> [:finished, "", "\x00p\x00i"]
 *
 *   ec = Encoding::Converter.new("UTF-8", "UTF-16BE")
 *   ret = ec.primitive_convert(src="pi", dst="", nil, 1)
 *   p [ret, src, dst] #=> [:destination_buffer_full, "i", "\x00"]
 *   ret = ec.primitive_convert(src, dst="", nil, 1)
 *   p [ret, src, dst] #=> [:destination_buffer_full, "", "p"]
 *   ret = ec.primitive_convert(src, dst="", nil, 1)
 *   p [ret, src, dst] #=> [:destination_buffer_full, "", "\x00"]
 *   ret = ec.primitive_convert(src, dst="", nil, 1)
 *   p [ret, src, dst] #=> [:finished, "", "i"]
 *
 */
static VALUE
econv_primitive_convert(int argc, VALUE *argv, VALUE self)
{
    VALUE input, output, output_byteoffset_v, output_bytesize_v, opt, flags_v;
    rb_econv_t *ec = check_econv(self);
    rb_econv_result_t res;
    const unsigned char *ip, *is;
    unsigned char *op, *os;
    long output_byteoffset, output_bytesize;
    unsigned long output_byteend;
    int flags;

    argc = rb_scan_args(argc, argv, "23:", &input, &output, &output_byteoffset_v, &output_bytesize_v, &flags_v, &opt);

    if (NIL_P(output_byteoffset_v))
        output_byteoffset = 0; /* dummy */
    else
        output_byteoffset = NUM2LONG(output_byteoffset_v);

    if (NIL_P(output_bytesize_v))
        output_bytesize = 0; /* dummy */
    else
        output_bytesize = NUM2LONG(output_bytesize_v);

    if (!NIL_P(flags_v)) {
	if (!NIL_P(opt)) {
	    rb_error_arity(argc + 1, 2, 5);
	}
	flags = NUM2INT(rb_to_int(flags_v));
    }
    else if (!NIL_P(opt)) {
        VALUE v;
        flags = 0;
        v = rb_hash_aref(opt, sym_partial_input);
        if (RTEST(v))
            flags |= ECONV_PARTIAL_INPUT;
        v = rb_hash_aref(opt, sym_after_output);
        if (RTEST(v))
            flags |= ECONV_AFTER_OUTPUT;
    }
    else {
        flags = 0;
    }

    StringValue(output);
    if (!NIL_P(input))
        StringValue(input);
    rb_str_modify(output);

    if (NIL_P(output_bytesize_v)) {
        output_bytesize = RSTRING_EMBED_LEN_MAX;
        if (!NIL_P(input) && output_bytesize < RSTRING_LEN(input))
            output_bytesize = RSTRING_LEN(input);
    }

  retry:

    if (NIL_P(output_byteoffset_v))
        output_byteoffset = RSTRING_LEN(output);

    if (output_byteoffset < 0)
        rb_raise(rb_eArgError, "negative output_byteoffset");

    if (RSTRING_LEN(output) < output_byteoffset)
        rb_raise(rb_eArgError, "output_byteoffset too big");

    if (output_bytesize < 0)
        rb_raise(rb_eArgError, "negative output_bytesize");

    output_byteend = (unsigned long)output_byteoffset +
                     (unsigned long)output_bytesize;

    if (output_byteend < (unsigned long)output_byteoffset ||
        LONG_MAX < output_byteend)
        rb_raise(rb_eArgError, "output_byteoffset+output_bytesize too big");

    if (rb_str_capacity(output) < output_byteend)
        rb_str_resize(output, output_byteend);

    if (NIL_P(input)) {
        ip = is = NULL;
    }
    else {
        ip = (const unsigned char *)RSTRING_PTR(input);
        is = ip + RSTRING_LEN(input);
    }

    op = (unsigned char *)RSTRING_PTR(output) + output_byteoffset;
    os = op + output_bytesize;

    res = rb_econv_convert(ec, &ip, is, &op, os, flags);
    rb_str_set_len(output, op-(unsigned char *)RSTRING_PTR(output));
    if (!NIL_P(input)) {
        OBJ_INFECT_RAW(output, input);
        rb_str_drop_bytes(input, ip - (unsigned char *)RSTRING_PTR(input));
    }

    if (NIL_P(output_bytesize_v) && res == econv_destination_buffer_full) {
        if (LONG_MAX / 2 < output_bytesize)
            rb_raise(rb_eArgError, "too long conversion result");
        output_bytesize *= 2;
        output_byteoffset_v = Qnil;
        goto retry;
    }

    if (ec->destination_encoding) {
        rb_enc_associate(output, ec->destination_encoding);
    }

    return econv_result_to_symbol(res);
}

/*
 * call-seq:
 *   ec.convert(source_string) -> destination_string
 *
 * Convert source_string and return destination_string.
 *
 * source_string is assumed as a part of source.
 * i.e.  :partial_input=>true is specified internally.
 * finish method should be used last.
 *
 *   ec = Encoding::Converter.new("utf-8", "euc-jp")
 *   puts ec.convert("\u3042").dump     #=> "\xA4\xA2"
 *   puts ec.finish.dump                #=> ""
 *
 *   ec = Encoding::Converter.new("euc-jp", "utf-8")
 *   puts ec.convert("\xA4").dump       #=> ""
 *   puts ec.convert("\xA2").dump       #=> "\xE3\x81\x82"
 *   puts ec.finish.dump                #=> ""
 *
 *   ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
 *   puts ec.convert("\xE3").dump       #=> "".force_encoding("ISO-2022-JP")
 *   puts ec.convert("\x81").dump       #=> "".force_encoding("ISO-2022-JP")
 *   puts ec.convert("\x82").dump       #=> "\e$B$\"".force_encoding("ISO-2022-JP")
 *   puts ec.finish.dump                #=> "\e(B".force_encoding("ISO-2022-JP")
 *
 * If a conversion error occur,
 * Encoding::UndefinedConversionError or
 * Encoding::InvalidByteSequenceError is raised.
 * Encoding::Converter#convert doesn't supply methods to recover or restart
 * from these exceptions.
 * When you want to handle these conversion errors,
 * use Encoding::Converter#primitive_convert.
 *
 */
static VALUE
econv_convert(VALUE self, VALUE source_string)
{
    VALUE ret, dst;
    VALUE av[5];
    int ac;
    rb_econv_t *ec = check_econv(self);

    StringValue(source_string);

    dst = rb_str_new(NULL, 0);

    av[0] = rb_str_dup(source_string);
    av[1] = dst;
    av[2] = Qnil;
    av[3] = Qnil;
    av[4] = INT2NUM(ECONV_PARTIAL_INPUT);
    ac = 5;

    ret = econv_primitive_convert(ac, av, self);

    if (ret == sym_invalid_byte_sequence ||
        ret == sym_undefined_conversion ||
        ret == sym_incomplete_input) {
        VALUE exc = make_econv_exception(ec);
        rb_exc_raise(exc);
    }

    if (ret == sym_finished) {
        rb_raise(rb_eArgError, "converter already finished");
    }

    if (ret != sym_source_buffer_empty) {
        rb_bug("unexpected result of econv_primitive_convert");
    }

    return dst;
}

/*
 * call-seq:
 *   ec.finish -> string
 *
 * Finishes the converter.
 * It returns the last part of the converted string.
 *
 *   ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
 *   p ec.convert("\u3042")     #=> "\e$B$\""
 *   p ec.finish                #=> "\e(B"
 */
static VALUE
econv_finish(VALUE self)
{
    VALUE ret, dst;
    VALUE av[5];
    int ac;
    rb_econv_t *ec = check_econv(self);

    dst = rb_str_new(NULL, 0);

    av[0] = Qnil;
    av[1] = dst;
    av[2] = Qnil;
    av[3] = Qnil;
    av[4] = INT2FIX(0);
    ac = 5;

    ret = econv_primitive_convert(ac, av, self);

    if (ret == sym_invalid_byte_sequence ||
        ret == sym_undefined_conversion ||
        ret == sym_incomplete_input) {
        VALUE exc = make_econv_exception(ec);
        rb_exc_raise(exc);
    }

    if (ret != sym_finished) {
        rb_bug("unexpected result of econv_primitive_convert");
    }

    return dst;
}

/*
 * call-seq:
 *   ec.primitive_errinfo -> array
 *
 * primitive_errinfo returns important information regarding the last error
 * as a 5-element array:
 *
 *   [result, enc1, enc2, error_bytes, readagain_bytes]
 *
 * result is the last result of primitive_convert.
 *
 * Other elements are only meaningful when result is
 * :invalid_byte_sequence, :incomplete_input or :undefined_conversion.
 *
 * enc1 and enc2 indicate a conversion step as a pair of strings.
 * For example, a converter from EUC-JP to ISO-8859-1 converts
 * a string as follows: EUC-JP -> UTF-8 -> ISO-8859-1.
 * So [enc1, enc2] is either ["EUC-JP", "UTF-8"] or ["UTF-8", "ISO-8859-1"].
 *
 * error_bytes and readagain_bytes indicate the byte sequences which caused the error.
 * error_bytes is discarded portion.
 * readagain_bytes is buffered portion which is read again on next conversion.
 *
 * Example:
 *
 *   # \xff is invalid as EUC-JP.
 *   ec = Encoding::Converter.new("EUC-JP", "Shift_JIS")
 *   ec.primitive_convert(src="\xff", dst="", nil, 10)
 *   p ec.primitive_errinfo
 *   #=> [:invalid_byte_sequence, "EUC-JP", "UTF-8", "\xFF", ""]
 *
 *   # HIRAGANA LETTER A (\xa4\xa2 in EUC-JP) is not representable in ISO-8859-1.
 *   # Since this error is occur in UTF-8 to ISO-8859-1 conversion,
 *   # error_bytes is HIRAGANA LETTER A in UTF-8 (\xE3\x81\x82).
 *   ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
 *   ec.primitive_convert(src="\xa4\xa2", dst="", nil, 10)
 *   p ec.primitive_errinfo
 *   #=> [:undefined_conversion, "UTF-8", "ISO-8859-1", "\xE3\x81\x82", ""]
 *
 *   # partial character is invalid
 *   ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
 *   ec.primitive_convert(src="\xa4", dst="", nil, 10)
 *   p ec.primitive_errinfo
 *   #=> [:incomplete_input, "EUC-JP", "UTF-8", "\xA4", ""]
 *
 *   # Encoding::Converter::PARTIAL_INPUT prevents invalid errors by
 *   # partial characters.
 *   ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
 *   ec.primitive_convert(src="\xa4", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT)
 *   p ec.primitive_errinfo
 *   #=> [:source_buffer_empty, nil, nil, nil, nil]
 *
 *   # \xd8\x00\x00@ is invalid as UTF-16BE because
 *   # no low surrogate after high surrogate (\xd8\x00).
 *   # It is detected by 3rd byte (\00) which is part of next character.
 *   # So the high surrogate (\xd8\x00) is discarded and
 *   # the 3rd byte is read again later.
 *   # Since the byte is buffered in ec, it is dropped from src.
 *   ec = Encoding::Converter.new("UTF-16BE", "UTF-8")
 *   ec.primitive_convert(src="\xd8\x00\x00@", dst="", nil, 10)
 *   p ec.primitive_errinfo
 *   #=> [:invalid_byte_sequence, "UTF-16BE", "UTF-8", "\xD8\x00", "\x00"]
 *   p src
 *   #=> "@"
 *
 *   # Similar to UTF-16BE, \x00\xd8@\x00 is invalid as UTF-16LE.
 *   # The problem is detected by 4th byte.
 *   ec = Encoding::Converter.new("UTF-16LE", "UTF-8")
 *   ec.primitive_convert(src="\x00\xd8@\x00", dst="", nil, 10)
 *   p ec.primitive_errinfo
 *   #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "@\x00"]
 *   p src
 *   #=> ""
 *
 */
static VALUE
econv_primitive_errinfo(VALUE self)
{
    rb_econv_t *ec = check_econv(self);

    VALUE ary;

    ary = rb_ary_new2(5);

    rb_ary_store(ary, 0, econv_result_to_symbol(ec->last_error.result));
    rb_ary_store(ary, 4, Qnil);

    if (ec->last_error.source_encoding)
        rb_ary_store(ary, 1, rb_str_new2(ec->last_error.source_encoding));

    if (ec->last_error.destination_encoding)
        rb_ary_store(ary, 2, rb_str_new2(ec->last_error.destination_encoding));

    if (ec->last_error.error_bytes_start) {
        rb_ary_store(ary, 3, rb_str_new((const char *)ec->last_error.error_bytes_start, ec->last_error.error_bytes_len));
        rb_ary_store(ary, 4, rb_str_new((const char *)ec->last_error.error_bytes_start + ec->last_error.error_bytes_len, ec->last_error.readagain_len));
    }

    return ary;
}

/*
 * call-seq:
 *   ec.insert_output(string) -> nil
 *
 * Inserts string into the encoding converter.
 * The string will be converted to the destination encoding and
 * output on later conversions.
 *
 * If the destination encoding is stateful,
 * string is converted according to the state and the state is updated.
 *
 * This method should be used only when a conversion error occurs.
 *
 *  ec = Encoding::Converter.new("utf-8", "iso-8859-1")
 *  src = "HIRAGANA LETTER A is \u{3042}."
 *  dst = ""
 *  p ec.primitive_convert(src, dst)    #=> :undefined_conversion
 *  puts "[#{dst.dump}, #{src.dump}]"   #=> ["HIRAGANA LETTER A is ", "."]
 *  ec.insert_output("<err>")
 *  p ec.primitive_convert(src, dst)    #=> :finished
 *  puts "[#{dst.dump}, #{src.dump}]"   #=> ["HIRAGANA LETTER A is <err>.", ""]
 *
 *  ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
 *  src = "\u{306F 3041 3068 2661 3002}" # U+2661 is not representable in iso-2022-jp
 *  dst = ""
 *  p ec.primitive_convert(src, dst)    #=> :undefined_conversion
 *  puts "[#{dst.dump}, #{src.dump}]"   #=> ["\e$B$O$!$H".force_encoding("ISO-2022-JP"), "\xE3\x80\x82"]
 *  ec.insert_output "?"                # state change required to output "?".
 *  p ec.primitive_convert(src, dst)    #=> :finished
 *  puts "[#{dst.dump}, #{src.dump}]"   #=> ["\e$B$O$!$H\e(B?\e$B!#\e(B".force_encoding("ISO-2022-JP"), ""]
 *
 */
static VALUE
econv_insert_output(VALUE self, VALUE string)
{
    const char *insert_enc;

    int ret;

    rb_econv_t *ec = check_econv(self);

    StringValue(string);
    insert_enc = rb_econv_encoding_to_insert_output(ec);
    string = rb_str_encode(string, rb_enc_from_encoding(rb_enc_find(insert_enc)), 0, Qnil);

    ret = rb_econv_insert_output(ec, (const unsigned char *)RSTRING_PTR(string), RSTRING_LEN(string), insert_enc);
    if (ret == -1) {
	rb_raise(rb_eArgError, "too big string");
    }

    return Qnil;
}

/*
 * call-seq
 *   ec.putback                    -> string
 *   ec.putback(max_numbytes)      -> string
 *
 * Put back the bytes which will be converted.
 *
 * The bytes are caused by invalid_byte_sequence error.
 * When invalid_byte_sequence error, some bytes are discarded and
 * some bytes are buffered to be converted later.
 * The latter bytes can be put back.
 * It can be observed by
 * Encoding::InvalidByteSequenceError#readagain_bytes and
 * Encoding::Converter#primitive_errinfo.
 *
 *   ec = Encoding::Converter.new("utf-16le", "iso-8859-1")
 *   src = "\x00\xd8\x61\x00"
 *   dst = ""
 *   p ec.primitive_convert(src, dst)   #=> :invalid_byte_sequence
 *   p ec.primitive_errinfo     #=> [:invalid_byte_sequence, "UTF-16LE", "UTF-8", "\x00\xD8", "a\x00"]
 *   p ec.putback               #=> "a\x00"
 *   p ec.putback               #=> ""          # no more bytes to put back
 *
 */
static VALUE
econv_putback(int argc, VALUE *argv, VALUE self)
{
    rb_econv_t *ec = check_econv(self);
    int n;
    int putbackable;
    VALUE str, max;

    rb_scan_args(argc, argv, "01", &max);

    if (NIL_P(max))
        n = rb_econv_putbackable(ec);
    else {
        n = NUM2INT(max);
        putbackable = rb_econv_putbackable(ec);
        if (putbackable < n)
            n = putbackable;
    }

    str = rb_str_new(NULL, n);
    rb_econv_putback(ec, (unsigned char *)RSTRING_PTR(str), n);

    if (ec->source_encoding) {
        rb_enc_associate(str, ec->source_encoding);
    }

    return str;
}

/*
 * call-seq:
 *   ec.last_error -> exception or nil
 *
 * Returns an exception object for the last conversion.
 * Returns nil if the last conversion did not produce an error.
 *
 * "error" means that
 * Encoding::InvalidByteSequenceError and Encoding::UndefinedConversionError for
 * Encoding::Converter#convert and
 * :invalid_byte_sequence, :incomplete_input and :undefined_conversion for
 * Encoding::Converter#primitive_convert.
 *
 *  ec = Encoding::Converter.new("utf-8", "iso-8859-1")
 *  p ec.primitive_convert(src="\xf1abcd", dst="")       #=> :invalid_byte_sequence
 *  p ec.last_error      #=> #<Encoding::InvalidByteSequenceError: "\xF1" followed by "a" on UTF-8>
 *  p ec.primitive_convert(src, dst, nil, 1)             #=> :destination_buffer_full
 *  p ec.last_error      #=> nil
 *
 */
static VALUE
econv_last_error(VALUE self)
{
    rb_econv_t *ec = check_econv(self);
    VALUE exc;

    exc = make_econv_exception(ec);
    if (NIL_P(exc))
        return Qnil;
    return exc;
}

/*
 * call-seq:
 *   ec.replacement -> string
 *
 * Returns the replacement string.
 *
 *  ec = Encoding::Converter.new("euc-jp", "us-ascii")
 *  p ec.replacement    #=> "?"
 *
 *  ec = Encoding::Converter.new("euc-jp", "utf-8")
 *  p ec.replacement    #=> "\uFFFD"
 */
static VALUE
econv_get_replacement(VALUE self)
{
    rb_econv_t *ec = check_econv(self);
    int ret;
    rb_encoding *enc;

    ret = make_replacement(ec);
    if (ret == -1) {
        rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
    }

    enc = rb_enc_find(ec->replacement_enc);
    return rb_enc_str_new((const char *)ec->replacement_str, (long)ec->replacement_len, enc);
}

/*
 * call-seq:
 *   ec.replacement = string
 *
 * Sets the replacement string.
 *
 *  ec = Encoding::Converter.new("utf-8", "us-ascii", :undef => :replace)
 *  ec.replacement = "<undef>"
 *  p ec.convert("a \u3042 b")      #=> "a <undef> b"
 */
static VALUE
econv_set_replacement(VALUE self, VALUE arg)
{
    rb_econv_t *ec = check_econv(self);
    VALUE string = arg;
    int ret;
    rb_encoding *enc;

    StringValue(string);
    enc = rb_enc_get(string);

    ret = rb_econv_set_replacement(ec,
            (const unsigned char *)RSTRING_PTR(string),
            RSTRING_LEN(string),
            rb_enc_name(enc));

    if (ret == -1) {
        /* xxx: rb_eInvalidByteSequenceError? */
        rb_raise(rb_eUndefinedConversionError, "replacement character setup failed");
    }

    return arg;
}

VALUE
rb_econv_make_exception(rb_econv_t *ec)
{
    return make_econv_exception(ec);
}

void
rb_econv_check_error(rb_econv_t *ec)
{
    VALUE exc;

    exc = make_econv_exception(ec);
    if (NIL_P(exc))
        return;
    rb_exc_raise(exc);
}

/*
 * call-seq:
 *   ecerr.source_encoding_name         -> string
 *
 * Returns the source encoding name as a string.
 */
static VALUE
ecerr_source_encoding_name(VALUE self)
{
    return rb_attr_get(self, rb_intern("source_encoding_name"));
}

/*
 * call-seq:
 *   ecerr.source_encoding              -> encoding
 *
 * Returns the source encoding as an encoding object.
 *
 * Note that the result may not be equal to the source encoding of
 * the encoding converter if the conversion has multiple steps.
 *
 *  ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP
 *  begin
 *    ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP.
 *  rescue Encoding::UndefinedConversionError
 *    p $!.source_encoding              #=> #<Encoding:UTF-8>
 *    p $!.destination_encoding         #=> #<Encoding:EUC-JP>
 *    p $!.source_encoding_name         #=> "UTF-8"
 *    p $!.destination_encoding_name    #=> "EUC-JP"
 *  end
 *
 */
static VALUE
ecerr_source_encoding(VALUE self)
{
    return rb_attr_get(self, rb_intern("source_encoding"));
}

/*
 * call-seq:
 *   ecerr.destination_encoding_name         -> string
 *
 * Returns the destination encoding name as a string.
 */
static VALUE
ecerr_destination_encoding_name(VALUE self)
{
    return rb_attr_get(self, rb_intern("destination_encoding_name"));
}

/*
 * call-seq:
 *   ecerr.destination_encoding         -> string
 *
 * Returns the destination encoding as an encoding object.
 */
static VALUE
ecerr_destination_encoding(VALUE self)
{
    return rb_attr_get(self, rb_intern("destination_encoding"));
}

/*
 * call-seq:
 *   ecerr.error_char         -> string
 *
 * Returns the one-character string which cause Encoding::UndefinedConversionError.
 *
 *  ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP")
 *  begin
 *    ec.convert("\xa0")
 *  rescue Encoding::UndefinedConversionError
 *    puts $!.error_char.dump   #=> "\xC2\xA0"
 *    p $!.error_char.encoding  #=> #<Encoding:UTF-8>
 *  end
 *
 */
static VALUE
ecerr_error_char(VALUE self)
{
    return rb_attr_get(self, rb_intern("error_char"));
}

/*
 * call-seq:
 *   ecerr.error_bytes         -> string
 *
 * Returns the discarded bytes when Encoding::InvalidByteSequenceError occurs.
 *
 *  ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
 *  begin
 *    ec.convert("abc\xA1\xFFdef")
 *  rescue Encoding::InvalidByteSequenceError
 *    p $!      #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP>
 *    puts $!.error_bytes.dump          #=> "\xA1"
 *    puts $!.readagain_bytes.dump      #=> "\xFF"
 *  end
 */
static VALUE
ecerr_error_bytes(VALUE self)
{
    return rb_attr_get(self, rb_intern("error_bytes"));
}

/*
 * call-seq:
 *   ecerr.readagain_bytes         -> string
 *
 * Returns the bytes to be read again when Encoding::InvalidByteSequenceError occurs.
 */
static VALUE
ecerr_readagain_bytes(VALUE self)
{
    return rb_attr_get(self, rb_intern("readagain_bytes"));
}

/*
 * call-seq:
 *   ecerr.incomplete_input?         -> true or false
 *
 * Returns true if the invalid byte sequence error is caused by
 * premature end of string.
 *
 *  ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1")
 *
 *  begin
 *    ec.convert("abc\xA1z")
 *  rescue Encoding::InvalidByteSequenceError
 *    p $!      #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "z" on EUC-JP>
 *    p $!.incomplete_input?    #=> false
 *  end
 *
 *  begin
 *    ec.convert("abc\xA1")
 *    ec.finish
 *  rescue Encoding::InvalidByteSequenceError
 *    p $!      #=> #<Encoding::InvalidByteSequenceError: incomplete "\xA1" on EUC-JP>
 *    p $!.incomplete_input?    #=> true
 *  end
 */
static VALUE
ecerr_incomplete_input(VALUE self)
{
    return rb_attr_get(self, rb_intern("incomplete_input"));
}

/*
 *  Document-class: Encoding::UndefinedConversionError
 *
 *  Raised by Encoding and String methods when a transcoding operation
 *  fails.
 */

/*
 *  Document-class: Encoding::InvalidByteSequenceError
 *
 *  Raised by Encoding and String methods when the string being
 *  transcoded contains a byte invalid for the either the source or
 *  target encoding.
 */

/*
 *  Document-class: Encoding::ConverterNotFoundError
 *
 *  Raised by transcoding methods when a named encoding does not
 *  correspond with a known converter.
 */

#undef rb_intern
void
Init_transcode(void)
{
    transcoder_table = st_init_strcasetable();

    sym_invalid = ID2SYM(rb_intern("invalid"));
    sym_undef = ID2SYM(rb_intern("undef"));
    sym_replace = ID2SYM(rb_intern("replace"));
    sym_fallback = ID2SYM(rb_intern("fallback"));
    sym_aref = ID2SYM(rb_intern("[]"));
    sym_xml = ID2SYM(rb_intern("xml"));
    sym_text = ID2SYM(rb_intern("text"));
    sym_attr = ID2SYM(rb_intern("attr"));

    sym_invalid_byte_sequence = ID2SYM(rb_intern("invalid_byte_sequence"));
    sym_undefined_conversion = ID2SYM(rb_intern("undefined_conversion"));
    sym_destination_buffer_full = ID2SYM(rb_intern("destination_buffer_full"));
    sym_source_buffer_empty = ID2SYM(rb_intern("source_buffer_empty"));
    sym_finished = ID2SYM(rb_intern("finished"));
    sym_after_output = ID2SYM(rb_intern("after_output"));
    sym_incomplete_input = ID2SYM(rb_intern("incomplete_input"));
    sym_universal_newline = ID2SYM(rb_intern("universal_newline"));
    sym_crlf_newline = ID2SYM(rb_intern("crlf_newline"));
    sym_cr_newline = ID2SYM(rb_intern("cr_newline"));
    sym_partial_input = ID2SYM(rb_intern("partial_input"));

#ifdef ENABLE_ECONV_NEWLINE_OPTION
    sym_newline = ID2SYM(rb_intern("newline"));
    sym_universal = ID2SYM(rb_intern("universal"));
    sym_crlf = ID2SYM(rb_intern("crlf"));
    sym_cr = ID2SYM(rb_intern("cr"));
    sym_lf = ID2SYM(rb_intern("lf"));
#endif

    InitVM(transcode);
}

void
InitVM_transcode(void)
{
    rb_eUndefinedConversionError = rb_define_class_under(rb_cEncoding, "UndefinedConversionError", rb_eEncodingError);
    rb_eInvalidByteSequenceError = rb_define_class_under(rb_cEncoding, "InvalidByteSequenceError", rb_eEncodingError);
    rb_eConverterNotFoundError = rb_define_class_under(rb_cEncoding, "ConverterNotFoundError", rb_eEncodingError);

    rb_define_method(rb_cString, "encode", str_encode, -1);
    rb_define_method(rb_cString, "encode!", str_encode_bang, -1);

    rb_cEncodingConverter = rb_define_class_under(rb_cEncoding, "Converter", rb_cData);
    rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate);
    rb_define_singleton_method(rb_cEncodingConverter, "asciicompat_encoding", econv_s_asciicompat_encoding, 1);
    rb_define_singleton_method(rb_cEncodingConverter, "search_convpath", econv_s_search_convpath, -1);
    rb_define_method(rb_cEncodingConverter, "initialize", econv_init, -1);
    rb_define_method(rb_cEncodingConverter, "inspect", econv_inspect, 0);
    rb_define_method(rb_cEncodingConverter, "convpath", econv_convpath, 0);
    rb_define_method(rb_cEncodingConverter, "source_encoding", econv_source_encoding, 0);
    rb_define_method(rb_cEncodingConverter, "destination_encoding", econv_destination_encoding, 0);
    rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
    rb_define_method(rb_cEncodingConverter, "convert", econv_convert, 1);
    rb_define_method(rb_cEncodingConverter, "finish", econv_finish, 0);
    rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
    rb_define_method(rb_cEncodingConverter, "insert_output", econv_insert_output, 1);
    rb_define_method(rb_cEncodingConverter, "putback", econv_putback, -1);
    rb_define_method(rb_cEncodingConverter, "last_error", econv_last_error, 0);
    rb_define_method(rb_cEncodingConverter, "replacement", econv_get_replacement, 0);
    rb_define_method(rb_cEncodingConverter, "replacement=", econv_set_replacement, 1);
    rb_define_method(rb_cEncodingConverter, "==", econv_equal, 1);

    /* Document-const: INVALID_MASK
     *
     * Mask for invalid byte sequences
     */
    rb_define_const(rb_cEncodingConverter, "INVALID_MASK", INT2FIX(ECONV_INVALID_MASK));

    /* Document-const: INVALID_REPLACE
     *
     * Replace invalid byte sequences
     */
    rb_define_const(rb_cEncodingConverter, "INVALID_REPLACE", INT2FIX(ECONV_INVALID_REPLACE));

    /* Document-const: UNDEF_MASK
     *
     * Mask for a valid character in the source encoding but no related
     * character(s) in destination encoding.
     */
    rb_define_const(rb_cEncodingConverter, "UNDEF_MASK", INT2FIX(ECONV_UNDEF_MASK));

    /* Document-const: UNDEF_REPLACE
     *
     * Replace byte sequences that are undefined in the destination encoding.
     */
    rb_define_const(rb_cEncodingConverter, "UNDEF_REPLACE", INT2FIX(ECONV_UNDEF_REPLACE));

    /* Document-const: UNDEF_HEX_CHARREF
     *
     * Replace byte sequences that are undefined in the destination encoding
     * with an XML hexadecimal character reference.  This is valid for XML
     * conversion.
     */
    rb_define_const(rb_cEncodingConverter, "UNDEF_HEX_CHARREF", INT2FIX(ECONV_UNDEF_HEX_CHARREF));

    /* Document-const: PARTIAL_INPUT
     *
     * Indicates the source may be part of a larger string.  See
     * primitive_convert for an example.
     */
    rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(ECONV_PARTIAL_INPUT));

    /* Document-const: AFTER_OUTPUT
     *
     * Stop converting after some output is complete but before all of the
     * input was consumed.  See primitive_convert for an example.
     */
    rb_define_const(rb_cEncodingConverter, "AFTER_OUTPUT", INT2FIX(ECONV_AFTER_OUTPUT));

    /* Document-const: UNIVERSAL_NEWLINE_DECORATOR
     *
     * Decorator for converting CRLF and CR to LF
     */
    rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE_DECORATOR", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECORATOR));

    /* Document-const: CRLF_NEWLINE_DECORATOR
     *
     * Decorator for converting LF to CRLF
     */
    rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE_DECORATOR", INT2FIX(ECONV_CRLF_NEWLINE_DECORATOR));

    /* Document-const: CR_NEWLINE_DECORATOR
     *
     * Decorator for converting LF to CR
     */
    rb_define_const(rb_cEncodingConverter, "CR_NEWLINE_DECORATOR", INT2FIX(ECONV_CR_NEWLINE_DECORATOR));

    /* Document-const: XML_TEXT_DECORATOR
     *
     * Escape as XML CharData
     */
    rb_define_const(rb_cEncodingConverter, "XML_TEXT_DECORATOR", INT2FIX(ECONV_XML_TEXT_DECORATOR));

    /* Document-const: XML_ATTR_CONTENT_DECORATOR
     *
     * Escape as XML AttValue
     */
    rb_define_const(rb_cEncodingConverter, "XML_ATTR_CONTENT_DECORATOR", INT2FIX(ECONV_XML_ATTR_CONTENT_DECORATOR));

    /* Document-const: XML_ATTR_QUOTE_DECORATOR
     *
     * Escape as XML AttValue
     */
    rb_define_const(rb_cEncodingConverter, "XML_ATTR_QUOTE_DECORATOR", INT2FIX(ECONV_XML_ATTR_QUOTE_DECORATOR));

    rb_define_method(rb_eUndefinedConversionError, "source_encoding_name", ecerr_source_encoding_name, 0);
    rb_define_method(rb_eUndefinedConversionError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
    rb_define_method(rb_eUndefinedConversionError, "source_encoding", ecerr_source_encoding, 0);
    rb_define_method(rb_eUndefinedConversionError, "destination_encoding", ecerr_destination_encoding, 0);
    rb_define_method(rb_eUndefinedConversionError, "error_char", ecerr_error_char, 0);

    rb_define_method(rb_eInvalidByteSequenceError, "source_encoding_name", ecerr_source_encoding_name, 0);
    rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding_name", ecerr_destination_encoding_name, 0);
    rb_define_method(rb_eInvalidByteSequenceError, "source_encoding", ecerr_source_encoding, 0);
    rb_define_method(rb_eInvalidByteSequenceError, "destination_encoding", ecerr_destination_encoding, 0);
    rb_define_method(rb_eInvalidByteSequenceError, "error_bytes", ecerr_error_bytes, 0);
    rb_define_method(rb_eInvalidByteSequenceError, "readagain_bytes", ecerr_readagain_bytes, 0);
    rb_define_method(rb_eInvalidByteSequenceError, "incomplete_input?", ecerr_incomplete_input, 0);

    Init_newline();
}