1 /*
2  * Copyright (c) 2003, Artem B. Bityuckiy, SoftMine Corporation.
3  * Rights transferred to Franklin Electronic Publishers.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <iconv.h>
29 #include <errno.h>
30 #include "check.h"
31 
32 #if defined(_ICONV_FROM_ENCODING_UTF_8) || \
33     defined(_ICONV_FROM_ENCODING_ISO_8859_5) || \
34     defined(_ICONV_FROM_ENCODING_KOI8_R)
35 
36 #ifdef _ICONV_FROM_ENCODING_ISO_8859_5
37 static const char iso_8859_5[] =
38 {
39     0xbe,0xdf,0xd5,0xe0,0xd0,0xe2,0xde,0xe0,0xeb,0x20,
40     0xd2,0x20,0xde,0xd4,0xdd,0xde,0xd9,0x20,0xe1,0xe2,
41     0xe0,0xde,0xda,0xd5,0x20,0xd8,0xdc,0xd5,0xee,0xe2,
42     0x20,0xde,0xd4,0xd8,0xdd,0xd0,0xda,0xde,0xd2,0xeb,
43     0xd9,0x20,0xdf,0xe0,0xd8,0xde,0xe0,0xd8,0xe2,0xd5,
44     0xe2,0x0a,0xc1,0xe2,0xe0,0xde,0xda,0xd8,0x20,0xe3,
45     0xdf,0xde,0xe0,0xef,0xd4,0xde,0xe7,0xd5,0xdd,0xeb,
46     0x20,0xdf,0xde,0x20,0xe3,0xd1,0xeb,0xd2,0xd0,0xdd,
47     0xd8,0xee,0x20,0xdf,0xe0,0xd8,0xde,0xe0,0xd8,0xe2,
48     0xd5,0xe2,0xde,0xd2,0x2e,0x0a,0x0a,0x2d,0x2d,0x2d,
49     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
50     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
51     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
52     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
53     0x2d,0x2d,0x2d,0x2d,0x20,0xb2,0xeb,0xdf,0xde,0xdb,
54     0xdd,0xd5,0xdd,0xd8,0xd5,0x20,0x2d,0x2d,0x2d,0x2d,
55     0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x28,0x29,0x20,0x20,
56     0x5b,0x5d,0x20,0x20,0x2d,0x3e,0x20,0x20,0x2e,0x20,
57     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
58     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
59     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
60     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x21,
61     0x20,0x20,0x20,0x7e,0x20,0x20,0x20,0x2b,0x2b,0x20,
62     0x20,0x2d,0x2d,0x20,0x20,0x2b,0x20,0x20,0x2d,0x20,
63     0x20,0x2a,0x20,0x20,0x26,0x20,0x20,0x28,0x74,0x79,
64     0x70,0x65,0x29,0x20,0x20,0x73,0x69,0x7a,0x65,0x6f,
65     0x66,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
66     0x3e,0x0a,0x2a,0x20,0x20,0x2f,0x20,0x20,0x25,0x20,
67     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
68     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
69     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
70     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
71     0x2d,0x2d,0x2d,0x3e,0x0a,0x2b,0x20,0x20,0x2d,0x20,
72     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
73     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
74     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
75     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
76     0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x3c,0x3c,
77     0x20,0x20,0x3e,0x3e,0x20,0x20,0x20,0x20,0x20,0x20,
78     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
79     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
80     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
81     0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,
82     0x0a,0x3c,0x20,0x20,0x3c,0x3d,0x20,0x20,0x3e,0x20,
83     0x20,0x3e,0x3d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
84     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
85     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
86     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
87     0x2d,0x2d,0x3e,0x0a,0x3d,0x3d,0x20,0x20,0x21,0x3d,
88     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
89     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
90     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
91     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
92     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,0x20,0x20,
93     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
94     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
95     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
96     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
97     0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,
98     0x5e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
99     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
100     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
101     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
102     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,
103     0x2d,0x3e,0x0a,0x7c,0x20,0x20,0x20,0x20,0x20,0x20,
104     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
105     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
106     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
107     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
108     0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,0x26,0x20,0x20,
109     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
110     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
111     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
112     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
113     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x7c,
114     0x7c,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
115     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
116     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
117     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
118     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
119     0x3e,0x0a,0x3f,0x3a,0x20,0x20,0x20,0x20,0x20,0x20,
120     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
121     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
122     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
123     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
124     0x3c,0x2d,0x2d,0x2d,0x20,0x20,0x20,0x0a,0x3d,0x20,
125     0x20,0x2b,0x3d,0x20,0x20,0x2d,0x3d,0x20,0x20,0x2f,
126     0x3d,0x20,0x20,0x25,0x3d,0x20,0x20,0x26,0x3d,0x20,
127     0x20,0x5e,0x3d,0x20,0x20,0x7c,0x3d,0x20,0x20,0x3c,
128     0x3c,0x3d,0x20,0x20,0x3e,0x3e,0x3d,0x20,0x20,0x20,
129     0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x2d,0x2d,0x2d,
130     0x0a,0x2c,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
131     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
132     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
133     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
134     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
135     0x2d,0x2d,0x3e,0x0a
136 };
137 #endif /* #ifdef _ICONV_FROM_ENCODING_ISO_8859_5 */
138 
139 #ifdef _ICONV_FROM_ENCODING_KOI8_R
140 static const char koi8_r[] =
141 {
142     0xef,0xd0,0xc5,0xd2,0xc1,0xd4,0xcf,0xd2,0xd9,0x20,
143     0xd7,0x20,0xcf,0xc4,0xce,0xcf,0xca,0x20,0xd3,0xd4,
144     0xd2,0xcf,0xcb,0xc5,0x20,0xc9,0xcd,0xc5,0xc0,0xd4,
145     0x20,0xcf,0xc4,0xc9,0xce,0xc1,0xcb,0xcf,0xd7,0xd9,
146     0xca,0x20,0xd0,0xd2,0xc9,0xcf,0xd2,0xc9,0xd4,0xc5,
147     0xd4,0x0a,0xf3,0xd4,0xd2,0xcf,0xcb,0xc9,0x20,0xd5,
148     0xd0,0xcf,0xd2,0xd1,0xc4,0xcf,0xde,0xc5,0xce,0xd9,
149     0x20,0xd0,0xcf,0x20,0xd5,0xc2,0xd9,0xd7,0xc1,0xce,
150     0xc9,0xc0,0x20,0xd0,0xd2,0xc9,0xcf,0xd2,0xc9,0xd4,
151     0xc5,0xd4,0xcf,0xd7,0x2e,0x0a,0x0a,0x2d,0x2d,0x2d,
152     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
153     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
154     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
155     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
156     0x2d,0x2d,0x2d,0x2d,0x20,0xf7,0xd9,0xd0,0xcf,0xcc,
157     0xce,0xc5,0xce,0xc9,0xc5,0x20,0x2d,0x2d,0x2d,0x2d,
158     0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x28,0x29,0x20,0x20,
159     0x5b,0x5d,0x20,0x20,0x2d,0x3e,0x20,0x20,0x2e,0x20,
160     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
161     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
162     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
163     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x21,
164     0x20,0x20,0x20,0x7e,0x20,0x20,0x20,0x2b,0x2b,0x20,
165     0x20,0x2d,0x2d,0x20,0x20,0x2b,0x20,0x20,0x2d,0x20,
166     0x20,0x2a,0x20,0x20,0x26,0x20,0x20,0x28,0x74,0x79,
167     0x70,0x65,0x29,0x20,0x20,0x73,0x69,0x7a,0x65,0x6f,
168     0x66,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
169     0x3e,0x0a,0x2a,0x20,0x20,0x2f,0x20,0x20,0x25,0x20,
170     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
171     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
172     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
173     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
174     0x2d,0x2d,0x2d,0x3e,0x0a,0x2b,0x20,0x20,0x2d,0x20,
175     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
176     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
177     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
178     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
179     0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x3c,0x3c,
180     0x20,0x20,0x3e,0x3e,0x20,0x20,0x20,0x20,0x20,0x20,
181     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
182     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
183     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
184     0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,
185     0x0a,0x3c,0x20,0x20,0x3c,0x3d,0x20,0x20,0x3e,0x20,
186     0x20,0x3e,0x3d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
187     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
188     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
189     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
190     0x2d,0x2d,0x3e,0x0a,0x3d,0x3d,0x20,0x20,0x21,0x3d,
191     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
192     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
193     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
194     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
195     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,0x20,0x20,
196     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
197     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
198     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
199     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
200     0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,
201     0x5e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
202     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
203     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
204     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
205     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,
206     0x2d,0x3e,0x0a,0x7c,0x20,0x20,0x20,0x20,0x20,0x20,
207     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
208     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
209     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
210     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
211     0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,0x26,0x20,0x20,
212     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
213     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
214     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
215     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
216     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x7c,
217     0x7c,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
218     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
219     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
220     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
221     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
222     0x3e,0x0a,0x3f,0x3a,0x20,0x20,0x20,0x20,0x20,0x20,
223     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
224     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
225     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
226     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
227     0x3c,0x2d,0x2d,0x2d,0x20,0x20,0x20,0x0a,0x3d,0x20,
228     0x20,0x2b,0x3d,0x20,0x20,0x2d,0x3d,0x20,0x20,0x2f,
229     0x3d,0x20,0x20,0x25,0x3d,0x20,0x20,0x26,0x3d,0x20,
230     0x20,0x5e,0x3d,0x20,0x20,0x7c,0x3d,0x20,0x20,0x3c,
231     0x3c,0x3d,0x20,0x20,0x3e,0x3e,0x3d,0x20,0x20,0x20,
232     0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x2d,0x2d,0x2d,
233     0x0a,0x2c,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
234     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
235     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
236     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
237     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
238     0x2d,0x2d,0x3e,0x0a
239 };
240 #endif /* #ifdef _ICONV_FROM_ENCODING_KOI8_R */
241 
242 #ifdef _ICONV_FROM_ENCODING_UTF_8
243 static const char utf8[] =
244 {
245     0xd0,0x9e,0xd0,0xbf,0xd0,0xb5,0xd1,0x80,0xd0,0xb0,
246     0xd1,0x82,0xd0,0xbe,0xd1,0x80,0xd1,0x8b,0x20,0xd0,
247     0xb2,0x20,0xd0,0xbe,0xd0,0xb4,0xd0,0xbd,0xd0,0xbe,
248     0xd0,0xb9,0x20,0xd1,0x81,0xd1,0x82,0xd1,0x80,0xd0,
249     0xbe,0xd0,0xba,0xd0,0xb5,0x20,0xd0,0xb8,0xd0,0xbc,
250     0xd0,0xb5,0xd1,0x8e,0xd1,0x82,0x20,0xd0,0xbe,0xd0,
251     0xb4,0xd0,0xb8,0xd0,0xbd,0xd0,0xb0,0xd0,0xba,0xd0,
252     0xbe,0xd0,0xb2,0xd1,0x8b,0xd0,0xb9,0x20,0xd0,0xbf,
253     0xd1,0x80,0xd0,0xb8,0xd0,0xbe,0xd1,0x80,0xd0,0xb8,
254     0xd1,0x82,0xd0,0xb5,0xd1,0x82,0x0a,0xd0,0xa1,0xd1,
255     0x82,0xd1,0x80,0xd0,0xbe,0xd0,0xba,0xd0,0xb8,0x20,
256     0xd1,0x83,0xd0,0xbf,0xd0,0xbe,0xd1,0x80,0xd1,0x8f,
257     0xd0,0xb4,0xd0,0xbe,0xd1,0x87,0xd0,0xb5,0xd0,0xbd,
258     0xd1,0x8b,0x20,0xd0,0xbf,0xd0,0xbe,0x20,0xd1,0x83,
259     0xd0,0xb1,0xd1,0x8b,0xd0,0xb2,0xd0,0xb0,0xd0,0xbd,
260     0xd0,0xb8,0xd1,0x8e,0x20,0xd0,0xbf,0xd1,0x80,0xd0,
261     0xb8,0xd0,0xbe,0xd1,0x80,0xd0,0xb8,0xd1,0x82,0xd0,
262     0xb5,0xd1,0x82,0xd0,0xbe,0xd0,0xb2,0x2e,0x0a,0x0a,
263     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
264     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
265     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
266     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
267     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x20,0xd0,0x92,
268     0xd1,0x8b,0xd0,0xbf,0xd0,0xbe,0xd0,0xbb,0xd0,0xbd,
269     0xd0,0xb5,0xd0,0xbd,0xd0,0xb8,0xd0,0xb5,0x20,0x2d,
270     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x28,
271     0x29,0x20,0x20,0x5b,0x5d,0x20,0x20,0x2d,0x3e,0x20,
272     0x20,0x2e,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
273     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
274     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
275     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
276     0x3e,0x0a,0x21,0x20,0x20,0x20,0x7e,0x20,0x20,0x20,
277     0x2b,0x2b,0x20,0x20,0x2d,0x2d,0x20,0x20,0x2b,0x20,
278     0x20,0x2d,0x20,0x20,0x2a,0x20,0x20,0x26,0x20,0x20,
279     0x28,0x74,0x79,0x70,0x65,0x29,0x20,0x20,0x73,0x69,
280     0x7a,0x65,0x6f,0x66,0x20,0x20,0x20,0x20,0x20,0x20,
281     0x2d,0x2d,0x2d,0x3e,0x0a,0x2a,0x20,0x20,0x2f,0x20,
282     0x20,0x25,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
283     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
284     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
285     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
286     0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x2b,0x20,
287     0x20,0x2d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
288     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
289     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
290     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
291     0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,
292     0x0a,0x3c,0x3c,0x20,0x20,0x3e,0x3e,0x20,0x20,0x20,
293     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
294     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
295     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
296     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
297     0x2d,0x2d,0x3e,0x0a,0x3c,0x20,0x20,0x3c,0x3d,0x20,
298     0x20,0x3e,0x20,0x20,0x3e,0x3d,0x20,0x20,0x20,0x20,
299     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
300     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
301     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
302     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x3d,0x3d,0x20,
303     0x20,0x21,0x3d,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
304     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
305     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
306     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
307     0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,
308     0x26,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
309     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
310     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
311     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
312     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,
313     0x2d,0x3e,0x0a,0x5e,0x20,0x20,0x20,0x20,0x20,0x20,
314     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
315     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
316     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
317     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
318     0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x7c,0x20,0x20,0x20,
319     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
320     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
321     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
322     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
323     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,
324     0x26,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
325     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
326     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
327     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
328     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
329     0x3e,0x0a,0x7c,0x7c,0x20,0x20,0x20,0x20,0x20,0x20,
330     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
331     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
332     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
333     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
334     0x2d,0x2d,0x2d,0x3e,0x0a,0x3f,0x3a,0x20,0x20,0x20,
335     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
336     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
337     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
338     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
339     0x20,0x20,0x20,0x3c,0x2d,0x2d,0x2d,0x20,0x20,0x20,
340     0x0a,0x3d,0x20,0x20,0x2b,0x3d,0x20,0x20,0x2d,0x3d,
341     0x20,0x20,0x2f,0x3d,0x20,0x20,0x25,0x3d,0x20,0x20,
342     0x26,0x3d,0x20,0x20,0x5e,0x3d,0x20,0x20,0x7c,0x3d,
343     0x20,0x20,0x3c,0x3c,0x3d,0x20,0x20,0x3e,0x3e,0x3d,
344     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,
345     0x2d,0x2d,0x2d,0x0a,0x2c,0x20,0x20,0x20,0x20,0x20,
346     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
347     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
348     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
349     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
350     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a
351 };
352 #endif
353 
354 struct iconv_data
355 {
356     size_t len;
357     char *name;
358     char *data;
359 };
360 
361 #define CONVERSIONS 3
362 
363 static const struct iconv_data data[] =
364 {
365 #ifdef _ICONV_FROM_ENCODING_ISO_8859_5
366     {sizeof(iso_8859_5), "ISO-8859-5", (char *)iso_8859_5},
367 #endif
368 #ifdef _ICONV_FROM_ENCODING_KOI8_R
369     {sizeof(koi8_r), "KOI8-R", (char *)koi8_r},
370 #endif
371 #ifdef _ICONV_FROM_ENCODING_UTF_8
372     {sizeof(utf8), "UTF-8", (char *)utf8},
373 #endif
374     {0, NULL, NULL}
375 };
376 
377 #define OUTBUF_LEN 1500
378 char ob[OUTBUF_LEN];
379 
380 iconv_t descs[CONVERSIONS*CONVERSIONS];
381 
382 #define ERROR 0
383 
384 #ifndef TEST_NLSPATH
385 #define TEST_NLSPATH "./"
386 #endif
387 
main(void)388 int main(void)
389 {
390     int i, j, d = 0;
391     unsigned k;
392     size_t n;
393     char *outbuf, *inbuf;
394     int conversions = sizeof(data)/sizeof(struct iconv_data) - 1;
395 
396     CHECK(setenv("NLSPATH", TEST_NLSPATH, 0) != -1);
397 
398     puts("RU iconv test");
399 
400     for (i = 0; i < conversions; i++)
401     {
402         for (j = 0; j < conversions; j++)
403 	{
404 	    descs[d] = iconv_open(data[j].name, data[i].name);
405 	    if (descs[d++] == (iconv_t)-1)
406 	    {
407 	        printf("iconv_open(%s, %s)\n", data[i].name, data[j].name);
408 	        perror("");
409                 CHECK(ERROR);
410 	    }
411 	}
412     }
413 
414     d = 0;
415     for (i = 0; i < conversions; i++)
416     {
417         for (j = 0; j < conversions; j++)
418 	{
419 	    size_t inbytes = data[i].len;
420 	    size_t outbytes = OUTBUF_LEN;
421 	    inbuf = data[i].data;
422 	    outbuf = (char *)ob;
423 
424 	    if (iconv(descs[d], NULL, NULL, (char **)&outbuf, &outbytes)                                                                       == (size_t)-1)
425             {
426                 perror("Can't reset shift state");
427                 CHECK(ERROR);
428             }
429 
430             n = iconv(descs[d++], &inbuf, &inbytes,
431 	                          &outbuf, &outbytes);
432             if (n == (size_t)-1)
433             {
434 	        printf("Conversion from %s to %s FAILED - iconv() "
435                        "returned -1\n", data[i].name, data[j].name);
436 		perror("");
437                 CHECK(ERROR);
438             }
439 
440 	    if (data[j].len != OUTBUF_LEN - outbytes)
441 	    {
442                 printf("Conversion from %s to %s FAILED",
443                        data[i].name, data[j].name);
444 	        printf(" - bad output buffer length (%ld instead of %lu)\n",
445 		       OUTBUF_LEN - (long) outbytes, (unsigned long) data[j].len);
446                 CHECK(ERROR);
447 	    }
448 
449 	    for (k = 0; k < data[j].len; k++)
450 	    {
451 	        if (ob[k] != data[j].data[k])
452 		{
453                     printf("Conversion from %s to %s FAILED",
454                            data[i].name, data[j].name);
455    	            printf("Error: byte %d is wrong\n", k);
456 		    printf("outbuf value: %#x, inbuf value %#x, "
457 		           "right value: %#x\n",
458           	           (int)ob[k], (int)(data[i].data[k]),
459 		           (int)(data[j].data[k]));
460                     CHECK(ERROR);
461 		}
462 	    }
463 
464 	    printf("iconv from %s to %s was successfully done\n",
465                    data[i].name, data[j].name);
466 
467 	}
468     }
469 
470     d = 0;
471     for (i = 0; i < conversions; i++)
472         for (j = 0; j < conversions; j++)
473             CHECK(iconv_close(descs[d++]) != -1);
474 
475     exit(0);
476 }
477 
478 #else /* #if defined(_ICONV_FROM_ENCODING_UTF_8) || ... */
main(void)479 int main(void)
480 {
481     puts("None of ISO-8859-5, KOI8-R and UTF-8 converters linked, SKIP test");
482     exit(0);
483 }
484 #endif /* #if defined(_ICONV_FROM_ENCODING_UTF_8) || ... */
485