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 <newlib.h>
31 #include "check.h"
32 
33 #if defined(_ICONV_FROM_ENCODING_UTF_8) || \
34     defined(_ICONV_FROM_ENCODING_ISO_8859_5) || \
35     defined(_ICONV_FROM_ENCODING_KOI8_R)
36 
37 #ifdef _ICONV_FROM_ENCODING_ISO_8859_5
38 char iso_8859_5[] =
39 {
40     0xbe,0xdf,0xd5,0xe0,0xd0,0xe2,0xde,0xe0,0xeb,0x20,
41     0xd2,0x20,0xde,0xd4,0xdd,0xde,0xd9,0x20,0xe1,0xe2,
42     0xe0,0xde,0xda,0xd5,0x20,0xd8,0xdc,0xd5,0xee,0xe2,
43     0x20,0xde,0xd4,0xd8,0xdd,0xd0,0xda,0xde,0xd2,0xeb,
44     0xd9,0x20,0xdf,0xe0,0xd8,0xde,0xe0,0xd8,0xe2,0xd5,
45     0xe2,0x0a,0xc1,0xe2,0xe0,0xde,0xda,0xd8,0x20,0xe3,
46     0xdf,0xde,0xe0,0xef,0xd4,0xde,0xe7,0xd5,0xdd,0xeb,
47     0x20,0xdf,0xde,0x20,0xe3,0xd1,0xeb,0xd2,0xd0,0xdd,
48     0xd8,0xee,0x20,0xdf,0xe0,0xd8,0xde,0xe0,0xd8,0xe2,
49     0xd5,0xe2,0xde,0xd2,0x2e,0x0a,0x0a,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,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
54     0x2d,0x2d,0x2d,0x2d,0x20,0xb2,0xeb,0xdf,0xde,0xdb,
55     0xdd,0xd5,0xdd,0xd8,0xd5,0x20,0x2d,0x2d,0x2d,0x2d,
56     0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x28,0x29,0x20,0x20,
57     0x5b,0x5d,0x20,0x20,0x2d,0x3e,0x20,0x20,0x2e,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,0x20,0x20,0x20,0x20,0x20,0x20,
61     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x21,
62     0x20,0x20,0x20,0x7e,0x20,0x20,0x20,0x2b,0x2b,0x20,
63     0x20,0x2d,0x2d,0x20,0x20,0x2b,0x20,0x20,0x2d,0x20,
64     0x20,0x2a,0x20,0x20,0x26,0x20,0x20,0x28,0x74,0x79,
65     0x70,0x65,0x29,0x20,0x20,0x73,0x69,0x7a,0x65,0x6f,
66     0x66,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
67     0x3e,0x0a,0x2a,0x20,0x20,0x2f,0x20,0x20,0x25,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     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
72     0x2d,0x2d,0x2d,0x3e,0x0a,0x2b,0x20,0x20,0x2d,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
77     0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x3c,0x3c,
78     0x20,0x20,0x3e,0x3e,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,0x20,0x20,0x20,0x20,
82     0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,
83     0x0a,0x3c,0x20,0x20,0x3c,0x3d,0x20,0x20,0x3e,0x20,
84     0x20,0x3e,0x3d,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,0x20,
87     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
88     0x2d,0x2d,0x3e,0x0a,0x3d,0x3d,0x20,0x20,0x21,0x3d,
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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
93     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,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,0x20,0x20,0x20,0x20,0x20,
98     0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,
99     0x5e,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,0x20,0x20,
103     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,
104     0x2d,0x3e,0x0a,0x7c,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
109     0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,0x26,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,0x20,0x20,0x20,0x20,0x20,0x20,
114     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x7c,
115     0x7c,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,0x20,0x20,0x20,
119     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
120     0x3e,0x0a,0x3f,0x3a,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     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
125     0x3c,0x2d,0x2d,0x2d,0x20,0x20,0x20,0x0a,0x3d,0x20,
126     0x20,0x2b,0x3d,0x20,0x20,0x2d,0x3d,0x20,0x20,0x2f,
127     0x3d,0x20,0x20,0x25,0x3d,0x20,0x20,0x26,0x3d,0x20,
128     0x20,0x5e,0x3d,0x20,0x20,0x7c,0x3d,0x20,0x20,0x3c,
129     0x3c,0x3d,0x20,0x20,0x3e,0x3e,0x3d,0x20,0x20,0x20,
130     0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x2d,0x2d,0x2d,
131     0x0a,0x2c,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,0x20,
135     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
136     0x2d,0x2d,0x3e,0x0a
137 };
138 #endif /* #ifdef _ICONV_FROM_ENCODING_ISO_8859_5 */
139 
140 #ifdef _ICONV_FROM_ENCODING_KOI8_R
141 char koi8_r[] =
142 {
143     0xef,0xd0,0xc5,0xd2,0xc1,0xd4,0xcf,0xd2,0xd9,0x20,
144     0xd7,0x20,0xcf,0xc4,0xce,0xcf,0xca,0x20,0xd3,0xd4,
145     0xd2,0xcf,0xcb,0xc5,0x20,0xc9,0xcd,0xc5,0xc0,0xd4,
146     0x20,0xcf,0xc4,0xc9,0xce,0xc1,0xcb,0xcf,0xd7,0xd9,
147     0xca,0x20,0xd0,0xd2,0xc9,0xcf,0xd2,0xc9,0xd4,0xc5,
148     0xd4,0x0a,0xf3,0xd4,0xd2,0xcf,0xcb,0xc9,0x20,0xd5,
149     0xd0,0xcf,0xd2,0xd1,0xc4,0xcf,0xde,0xc5,0xce,0xd9,
150     0x20,0xd0,0xcf,0x20,0xd5,0xc2,0xd9,0xd7,0xc1,0xce,
151     0xc9,0xc0,0x20,0xd0,0xd2,0xc9,0xcf,0xd2,0xc9,0xd4,
152     0xc5,0xd4,0xcf,0xd7,0x2e,0x0a,0x0a,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,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,
157     0x2d,0x2d,0x2d,0x2d,0x20,0xf7,0xd9,0xd0,0xcf,0xcc,
158     0xce,0xc5,0xce,0xc9,0xc5,0x20,0x2d,0x2d,0x2d,0x2d,
159     0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x28,0x29,0x20,0x20,
160     0x5b,0x5d,0x20,0x20,0x2d,0x3e,0x20,0x20,0x2e,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,0x20,0x20,0x20,0x20,0x20,0x20,
164     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x21,
165     0x20,0x20,0x20,0x7e,0x20,0x20,0x20,0x2b,0x2b,0x20,
166     0x20,0x2d,0x2d,0x20,0x20,0x2b,0x20,0x20,0x2d,0x20,
167     0x20,0x2a,0x20,0x20,0x26,0x20,0x20,0x28,0x74,0x79,
168     0x70,0x65,0x29,0x20,0x20,0x73,0x69,0x7a,0x65,0x6f,
169     0x66,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
170     0x3e,0x0a,0x2a,0x20,0x20,0x2f,0x20,0x20,0x25,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     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
175     0x2d,0x2d,0x2d,0x3e,0x0a,0x2b,0x20,0x20,0x2d,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
180     0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x3c,0x3c,
181     0x20,0x20,0x3e,0x3e,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,0x20,0x20,0x20,0x20,
185     0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,
186     0x0a,0x3c,0x20,0x20,0x3c,0x3d,0x20,0x20,0x3e,0x20,
187     0x20,0x3e,0x3d,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,0x20,
190     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
191     0x2d,0x2d,0x3e,0x0a,0x3d,0x3d,0x20,0x20,0x21,0x3d,
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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
196     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,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,0x20,0x20,0x20,0x20,0x20,
201     0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,
202     0x5e,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,0x20,0x20,
206     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,
207     0x2d,0x3e,0x0a,0x7c,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
212     0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,0x26,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,0x20,0x20,0x20,0x20,0x20,0x20,
217     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x7c,
218     0x7c,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,0x20,0x20,0x20,
222     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
223     0x3e,0x0a,0x3f,0x3a,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     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
228     0x3c,0x2d,0x2d,0x2d,0x20,0x20,0x20,0x0a,0x3d,0x20,
229     0x20,0x2b,0x3d,0x20,0x20,0x2d,0x3d,0x20,0x20,0x2f,
230     0x3d,0x20,0x20,0x25,0x3d,0x20,0x20,0x26,0x3d,0x20,
231     0x20,0x5e,0x3d,0x20,0x20,0x7c,0x3d,0x20,0x20,0x3c,
232     0x3c,0x3d,0x20,0x20,0x3e,0x3e,0x3d,0x20,0x20,0x20,
233     0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x2d,0x2d,0x2d,
234     0x0a,0x2c,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,0x20,
238     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
239     0x2d,0x2d,0x3e,0x0a
240 };
241 #endif /* #ifdef _ICONV_FROM_ENCODING_KOI8_R */
242 
243 #ifdef _ICONV_FROM_ENCODING_UTF_8
244 char utf8[] =
245 {
246     0xd0,0x9e,0xd0,0xbf,0xd0,0xb5,0xd1,0x80,0xd0,0xb0,
247     0xd1,0x82,0xd0,0xbe,0xd1,0x80,0xd1,0x8b,0x20,0xd0,
248     0xb2,0x20,0xd0,0xbe,0xd0,0xb4,0xd0,0xbd,0xd0,0xbe,
249     0xd0,0xb9,0x20,0xd1,0x81,0xd1,0x82,0xd1,0x80,0xd0,
250     0xbe,0xd0,0xba,0xd0,0xb5,0x20,0xd0,0xb8,0xd0,0xbc,
251     0xd0,0xb5,0xd1,0x8e,0xd1,0x82,0x20,0xd0,0xbe,0xd0,
252     0xb4,0xd0,0xb8,0xd0,0xbd,0xd0,0xb0,0xd0,0xba,0xd0,
253     0xbe,0xd0,0xb2,0xd1,0x8b,0xd0,0xb9,0x20,0xd0,0xbf,
254     0xd1,0x80,0xd0,0xb8,0xd0,0xbe,0xd1,0x80,0xd0,0xb8,
255     0xd1,0x82,0xd0,0xb5,0xd1,0x82,0x0a,0xd0,0xa1,0xd1,
256     0x82,0xd1,0x80,0xd0,0xbe,0xd0,0xba,0xd0,0xb8,0x20,
257     0xd1,0x83,0xd0,0xbf,0xd0,0xbe,0xd1,0x80,0xd1,0x8f,
258     0xd0,0xb4,0xd0,0xbe,0xd1,0x87,0xd0,0xb5,0xd0,0xbd,
259     0xd1,0x8b,0x20,0xd0,0xbf,0xd0,0xbe,0x20,0xd1,0x83,
260     0xd0,0xb1,0xd1,0x8b,0xd0,0xb2,0xd0,0xb0,0xd0,0xbd,
261     0xd0,0xb8,0xd1,0x8e,0x20,0xd0,0xbf,0xd1,0x80,0xd0,
262     0xb8,0xd0,0xbe,0xd1,0x80,0xd0,0xb8,0xd1,0x82,0xd0,
263     0xb5,0xd1,0x82,0xd0,0xbe,0xd0,0xb2,0x2e,0x0a,0x0a,
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,0x2d,0x2d,0x2d,
268     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x20,0xd0,0x92,
269     0xd1,0x8b,0xd0,0xbf,0xd0,0xbe,0xd0,0xbb,0xd0,0xbd,
270     0xd0,0xb5,0xd0,0xbd,0xd0,0xb8,0xd0,0xb5,0x20,0x2d,
271     0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x2d,0x0a,0x28,
272     0x29,0x20,0x20,0x5b,0x5d,0x20,0x20,0x2d,0x3e,0x20,
273     0x20,0x2e,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,0x20,0x20,0x20,
276     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
277     0x3e,0x0a,0x21,0x20,0x20,0x20,0x7e,0x20,0x20,0x20,
278     0x2b,0x2b,0x20,0x20,0x2d,0x2d,0x20,0x20,0x2b,0x20,
279     0x20,0x2d,0x20,0x20,0x2a,0x20,0x20,0x26,0x20,0x20,
280     0x28,0x74,0x79,0x70,0x65,0x29,0x20,0x20,0x73,0x69,
281     0x7a,0x65,0x6f,0x66,0x20,0x20,0x20,0x20,0x20,0x20,
282     0x2d,0x2d,0x2d,0x3e,0x0a,0x2a,0x20,0x20,0x2f,0x20,
283     0x20,0x25,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
287     0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x2b,0x20,
288     0x20,0x2d,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,0x20,0x20,0x20,0x20,
292     0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,
293     0x0a,0x3c,0x3c,0x20,0x20,0x3e,0x3e,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,0x20,
297     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,
298     0x2d,0x2d,0x3e,0x0a,0x3c,0x20,0x20,0x3c,0x3d,0x20,
299     0x20,0x3e,0x20,0x20,0x3e,0x3d,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
303     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x3d,0x3d,0x20,
304     0x20,0x21,0x3d,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,0x20,0x20,0x20,0x20,0x20,
308     0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,
309     0x26,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,0x20,0x20,
313     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,
314     0x2d,0x3e,0x0a,0x5e,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
319     0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x7c,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,0x20,0x20,0x20,0x20,0x20,0x20,
324     0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a,0x26,
325     0x26,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,0x20,0x20,0x20,
329     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x2d,0x2d,0x2d,
330     0x3e,0x0a,0x7c,0x7c,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     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
335     0x2d,0x2d,0x2d,0x3e,0x0a,0x3f,0x3a,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
340     0x20,0x20,0x20,0x3c,0x2d,0x2d,0x2d,0x20,0x20,0x20,
341     0x0a,0x3d,0x20,0x20,0x2b,0x3d,0x20,0x20,0x2d,0x3d,
342     0x20,0x20,0x2f,0x3d,0x20,0x20,0x25,0x3d,0x20,0x20,
343     0x26,0x3d,0x20,0x20,0x5e,0x3d,0x20,0x20,0x7c,0x3d,
344     0x20,0x20,0x3c,0x3c,0x3d,0x20,0x20,0x3e,0x3e,0x3d,
345     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,
346     0x2d,0x2d,0x2d,0x0a,0x2c,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,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
351     0x20,0x20,0x2d,0x2d,0x2d,0x3e,0x0a
352 };
353 #endif
354 
355 struct iconv_data
356 {
357     size_t len;
358     char *name;
359     char *data;
360 };
361 
362 #define CONVERSIONS 3
363 
364 struct iconv_data data[] =
365 {
366 #ifdef _ICONV_FROM_ENCODING_ISO_8859_5
367     {sizeof(iso_8859_5), "ISO-8859-5", (char *)iso_8859_5},
368 #endif
369 #ifdef _ICONV_FROM_ENCODING_KOI8_R
370     {sizeof(koi8_r), "KOI8-R", (char *)koi8_r},
371 #endif
372 #ifdef _ICONV_FROM_ENCODING_UTF_8
373     {sizeof(utf8), "UTF-8", (char *)utf8},
374 #endif
375     {0, NULL, NULL}
376 };
377 
378 #define OUTBUF_LEN 1500
379 char ob[OUTBUF_LEN];
380 
381 iconv_t descs[CONVERSIONS*CONVERSIONS];
382 
383 #define ERROR 0
384 
385 #ifndef TEST_NLSPATH
386 #define TEST_NLSPATH "./"
387 #endif
388 
main(void)389 int main(void)
390 {
391     int i, j, d = 0;
392     unsigned k;
393     size_t n;
394     char *outbuf, *inbuf;
395     int conversions = sizeof(data)/sizeof(struct iconv_data) - 1;
396 
397     CHECK(setenv("NLSPATH", TEST_NLSPATH, 0) != -1);
398 
399     puts("RU iconv test");
400 
401     for (i = 0; i < conversions; i++)
402     {
403         for (j = 0; j < conversions; j++)
404 	{
405 	    descs[d] = iconv_open(data[j].name, data[i].name);
406 	    if (descs[d++] == (iconv_t)-1)
407 	    {
408 	        printf("iconv_open(%s, %s)\n", data[i].name, data[j].name);
409 	        perror("");
410                 CHECK(ERROR);
411 	    }
412 	}
413     }
414 
415     d = 0;
416     for (i = 0; i < conversions; i++)
417     {
418         for (j = 0; j < conversions; j++)
419 	{
420 	    size_t inbytes = data[i].len;
421 	    size_t outbytes = OUTBUF_LEN;
422 	    inbuf = data[i].data;
423 	    outbuf = (char *)ob;
424 
425 	    if (iconv(descs[d], NULL, NULL, (char **)&outbuf, &outbytes)                                                                       == (size_t)-1)
426             {
427                 perror("Can't reset shift state");
428                 CHECK(ERROR);
429             }
430 
431             n = iconv(descs[d++], (const char **)&(inbuf), &inbytes,
432 	                          (char **)&outbuf, &outbytes);
433             if (n == (size_t)-1)
434             {
435 	        printf("Conversion from %s to %s FAILED - iconv() "
436                        "returned -1\n", data[i].name, data[j].name);
437 		perror("");
438                 CHECK(ERROR);
439             }
440 
441 	    if (data[j].len != OUTBUF_LEN - outbytes)
442 	    {
443                 printf("Conversion from %s to %s FAILED",
444                        data[i].name, data[j].name);
445 	        printf(" - bad output buffer length (%ld instead of %lu)\n",
446 		       OUTBUF_LEN - (long) outbytes, (unsigned long) data[j].len);
447                 CHECK(ERROR);
448 	    }
449 
450 	    for (k = 0; k < data[j].len; k++)
451 	    {
452 	        if (ob[k] != data[j].data[k])
453 		{
454                     printf("Conversion from %s to %s FAILED",
455                            data[i].name, data[j].name);
456    	            printf("Error: byte %d is wrong\n", k);
457 		    printf("outbuf value: %#x, inbuf value %#x, "
458 		           "right value: %#x\n",
459           	           (int)ob[k], (int)(data[i].data[k]),
460 		           (int)(data[j].data[k]));
461                     CHECK(ERROR);
462 		}
463 	    }
464 
465 	    printf("iconv from %s to %s was successfully done\n",
466                    data[i].name, data[j].name);
467 
468 	}
469     }
470 
471     d = 0;
472     for (i = 0; i < conversions; i++)
473         for (j = 0; j < conversions; j++)
474             CHECK(iconv_close(descs[d++]) != -1);
475 
476     exit(0);
477 }
478 
479 #else /* #if defined(_ICONV_FROM_ENCODING_UTF_8) || ... */
main(int argc,char ** argv)480 int main(int argc, char **argv)
481 {
482     puts("None of ISO-8859-5, KOI8-R and UTF-8 converters linked, SKIP test");
483     exit(0);
484 }
485 #endif /* #if defined(_ICONV_FROM_ENCODING_UTF_8) || ... */
486