1ad30f8e7SGabor Kovesdan /*-
2ad30f8e7SGabor Kovesdan * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
3ad30f8e7SGabor Kovesdan * All rights reserved.
4ad30f8e7SGabor Kovesdan *
5ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
6ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
7ad30f8e7SGabor Kovesdan * are met:
8ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
9ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
10ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
11ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
12ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
13ad30f8e7SGabor Kovesdan *
14ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
25ad30f8e7SGabor Kovesdan */
26ad30f8e7SGabor Kovesdan
27814bd1edSKyle Evans #include <sys/param.h>
28ad30f8e7SGabor Kovesdan #include <sys/endian.h>
29ad30f8e7SGabor Kovesdan
30ad30f8e7SGabor Kovesdan #include <err.h>
31ad30f8e7SGabor Kovesdan #include <errno.h>
32ad30f8e7SGabor Kovesdan #include <iconv.h>
33ad30f8e7SGabor Kovesdan #include <stdbool.h>
34ad30f8e7SGabor Kovesdan #include <stdio.h>
35ad30f8e7SGabor Kovesdan #include <stdlib.h>
36ad30f8e7SGabor Kovesdan
37ad30f8e7SGabor Kovesdan /*
38ad30f8e7SGabor Kovesdan * iconv_open must return (iconv_t)-1 on non-existing encoding
39ad30f8e7SGabor Kovesdan * and set errno to EINVAL.
40ad30f8e7SGabor Kovesdan */
41ad30f8e7SGabor Kovesdan static int
open_1(void)42ad30f8e7SGabor Kovesdan open_1(void)
43ad30f8e7SGabor Kovesdan {
44ad30f8e7SGabor Kovesdan iconv_t cd;
45ad30f8e7SGabor Kovesdan
46ad30f8e7SGabor Kovesdan errno = 0;
47ad30f8e7SGabor Kovesdan cd = iconv_open("nonexisting", "foobar");
48ad30f8e7SGabor Kovesdan
49ad30f8e7SGabor Kovesdan if ((cd == (iconv_t)-1) && (errno == EINVAL))
50ad30f8e7SGabor Kovesdan return (0);
51ad30f8e7SGabor Kovesdan else {
52ad30f8e7SGabor Kovesdan iconv_close(cd);
53ad30f8e7SGabor Kovesdan return (1);
54ad30f8e7SGabor Kovesdan }
55ad30f8e7SGabor Kovesdan }
56ad30f8e7SGabor Kovesdan
57ad30f8e7SGabor Kovesdan /*
58ad30f8e7SGabor Kovesdan * iconv_open must return (iconv_t)-1 if too much files are open
59ad30f8e7SGabor Kovesdan * and set errno to ENFILE.
60ad30f8e7SGabor Kovesdan */
61ad30f8e7SGabor Kovesdan #define MAX_LIMIT 1025
62ad30f8e7SGabor Kovesdan static int
open_2(void)63ad30f8e7SGabor Kovesdan open_2(void)
64ad30f8e7SGabor Kovesdan {
65ad30f8e7SGabor Kovesdan iconv_t cd[MAX_LIMIT];
66814bd1edSKyle Evans size_t i;
67814bd1edSKyle Evans int ret;
68ad30f8e7SGabor Kovesdan
69ad30f8e7SGabor Kovesdan errno = 0;
70814bd1edSKyle Evans ret = 1;
71ad30f8e7SGabor Kovesdan for (i = 0; i < MAX_LIMIT; i++) {
72ad30f8e7SGabor Kovesdan cd[i] = iconv_open("ASCII", "UTF8");
73814bd1edSKyle Evans if (cd[i] == (iconv_t)-1) {
74814bd1edSKyle Evans if (errno == ENFILE || errno == EMFILE)
75814bd1edSKyle Evans ret = 0;
76814bd1edSKyle Evans cd[i] = NULL;
77ad30f8e7SGabor Kovesdan break;
78ad30f8e7SGabor Kovesdan }
79814bd1edSKyle Evans }
80ad30f8e7SGabor Kovesdan
81814bd1edSKyle Evans for (i = MIN(i, nitems(cd) - 1); i > 0; i--)
82ad30f8e7SGabor Kovesdan iconv_close(cd[i]);
83ad30f8e7SGabor Kovesdan return (ret);
84ad30f8e7SGabor Kovesdan }
85ad30f8e7SGabor Kovesdan
86ad30f8e7SGabor Kovesdan /*
87ad30f8e7SGabor Kovesdan * iconv_close must return (iconv_t)-1 if conversion descriptor is
88ad30f8e7SGabor Kovesdan * invalid and set errno to EBADF.
89ad30f8e7SGabor Kovesdan */
90ad30f8e7SGabor Kovesdan static int
close_1(void)91ad30f8e7SGabor Kovesdan close_1(void)
92ad30f8e7SGabor Kovesdan {
93ad30f8e7SGabor Kovesdan iconv_t cd = (iconv_t)-1;
94ad30f8e7SGabor Kovesdan
95ad30f8e7SGabor Kovesdan return ((iconv_close(cd) == -1) && (errno = EBADF) ? 0 : 1);
96ad30f8e7SGabor Kovesdan }
97ad30f8e7SGabor Kovesdan
98ad30f8e7SGabor Kovesdan static int
conv_ebadf(void)99ad30f8e7SGabor Kovesdan conv_ebadf(void)
100ad30f8e7SGabor Kovesdan {
101ad30f8e7SGabor Kovesdan iconv_t cd = (iconv_t)-1;
102ad30f8e7SGabor Kovesdan
103ad30f8e7SGabor Kovesdan errno = 0;
104ad30f8e7SGabor Kovesdan return ((iconv(cd, NULL, 0, NULL, 0) == (size_t)-1 && errno == EBADF) ? 0 : 1);
105ad30f8e7SGabor Kovesdan }
106ad30f8e7SGabor Kovesdan
107ad30f8e7SGabor Kovesdan static int
conv_ret(void)108ad30f8e7SGabor Kovesdan conv_ret(void)
109ad30f8e7SGabor Kovesdan {
110ad30f8e7SGabor Kovesdan iconv_t cd;
111ad30f8e7SGabor Kovesdan size_t inbytesleft, outbytesleft;
112efe014e6SEdward Tomasz Napierala char *inptr;
113ad30f8e7SGabor Kovesdan char *outptr;
114ad30f8e7SGabor Kovesdan uint32_t outbuf[4];
115ad30f8e7SGabor Kovesdan uint32_t inbuf[2] = { 0x00000151, 0x00000171 };
116ad30f8e7SGabor Kovesdan
117ad30f8e7SGabor Kovesdan if ((cd = iconv_open("ASCII", "UTF-32LE")) == (iconv_t)-1)
118ad30f8e7SGabor Kovesdan return (1);
119ad30f8e7SGabor Kovesdan
120efe014e6SEdward Tomasz Napierala inptr = (char *)inbuf;
121ad30f8e7SGabor Kovesdan outptr = (char *)outbuf;
122ad30f8e7SGabor Kovesdan inbytesleft = 8;
123ad30f8e7SGabor Kovesdan outbytesleft = 16;
124ad30f8e7SGabor Kovesdan
125ad30f8e7SGabor Kovesdan return (iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft) == 2 ? 0 : 1);
126ad30f8e7SGabor Kovesdan }
127ad30f8e7SGabor Kovesdan
128ad30f8e7SGabor Kovesdan static int
conv_2big(void)129ad30f8e7SGabor Kovesdan conv_2big(void)
130ad30f8e7SGabor Kovesdan {
131ad30f8e7SGabor Kovesdan iconv_t cd;
132ad30f8e7SGabor Kovesdan size_t inbytesleft, outbytesleft;
133efe014e6SEdward Tomasz Napierala char *inptr;
134ad30f8e7SGabor Kovesdan char *outptr;
135ad30f8e7SGabor Kovesdan uint32_t inbuf[4];
136ad30f8e7SGabor Kovesdan uint32_t outbuf[2];
137ad30f8e7SGabor Kovesdan int ret;
138ad30f8e7SGabor Kovesdan
139ad30f8e7SGabor Kovesdan if ((cd = iconv_open("ASCII", "ASCII")) == (iconv_t)-1)
140ad30f8e7SGabor Kovesdan return (1);
141ad30f8e7SGabor Kovesdan
142efe014e6SEdward Tomasz Napierala inptr = (char *)inbuf;
143ad30f8e7SGabor Kovesdan outptr = (char *)outbuf;
144ad30f8e7SGabor Kovesdan inbytesleft = 16;
145ad30f8e7SGabor Kovesdan outbytesleft = 8;
146ad30f8e7SGabor Kovesdan
147ad30f8e7SGabor Kovesdan errno = 0;
148ad30f8e7SGabor Kovesdan ret = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
149ad30f8e7SGabor Kovesdan
150ad30f8e7SGabor Kovesdan #ifdef VERBOSE
151ad30f8e7SGabor Kovesdan printf("inptr - inbuf = %d\n", (const uint8_t *)inptr - (uint8_t *)inbuf);
152ad30f8e7SGabor Kovesdan printf("inbytesleft = %d\n", inbytesleft);
153ad30f8e7SGabor Kovesdan printf("outbytesleft = %d\n", outbytesleft);
154ad30f8e7SGabor Kovesdan printf("outptr - outbuf = %d\n", (uint8_t *)outptr - (uint8_t *)outbuf);
155ad30f8e7SGabor Kovesdan printf("errno = %d\n", errno);
156ad30f8e7SGabor Kovesdan printf("ret = %d\n", (int)ret);
157ad30f8e7SGabor Kovesdan #endif
158ad30f8e7SGabor Kovesdan
159ad30f8e7SGabor Kovesdan if (((const uint8_t *)inptr - (uint8_t *)inbuf == 8) && (inbytesleft == 8) &&
160ad30f8e7SGabor Kovesdan (outbytesleft == 0) && ((uint8_t *)outptr - (uint8_t *)outbuf == 8) &&
161ad30f8e7SGabor Kovesdan (errno == E2BIG) && ((size_t)ret == (size_t)-1))
162ad30f8e7SGabor Kovesdan return (0);
163ad30f8e7SGabor Kovesdan else
164ad30f8e7SGabor Kovesdan return (1);
165ad30f8e7SGabor Kovesdan }
166ad30f8e7SGabor Kovesdan
167ad30f8e7SGabor Kovesdan static int
conv_einval(void)168ad30f8e7SGabor Kovesdan conv_einval(void)
169ad30f8e7SGabor Kovesdan {
170ad30f8e7SGabor Kovesdan iconv_t cd;
171ad30f8e7SGabor Kovesdan size_t inbytesleft, outbytesleft;
172efe014e6SEdward Tomasz Napierala char *inptr;
173ad30f8e7SGabor Kovesdan char *outptr;
174ad30f8e7SGabor Kovesdan uint32_t outbuf[4];
175ad30f8e7SGabor Kovesdan uint16_t inbuf[1] = { 0xEA42 };
176ad30f8e7SGabor Kovesdan int ret;
177ad30f8e7SGabor Kovesdan
178ad30f8e7SGabor Kovesdan if ((cd = iconv_open("UTF-32", "BIG5")) == (iconv_t)-1)
179ad30f8e7SGabor Kovesdan return (1);
180ad30f8e7SGabor Kovesdan
181efe014e6SEdward Tomasz Napierala inptr = (char *)inbuf;
182ad30f8e7SGabor Kovesdan outptr = (char *)outbuf;
183ad30f8e7SGabor Kovesdan inbytesleft = 2;
184ad30f8e7SGabor Kovesdan outbytesleft = 16;
185ad30f8e7SGabor Kovesdan
186ad30f8e7SGabor Kovesdan errno = 0;
187ad30f8e7SGabor Kovesdan ret = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
188ad30f8e7SGabor Kovesdan
189ad30f8e7SGabor Kovesdan #ifdef VERBOSE
190ad30f8e7SGabor Kovesdan printf("inptr - inbuf = %d\n", (const uint8_t *)inptr - (uint8_t *)inbuf);
191ad30f8e7SGabor Kovesdan printf("inbytesleft = %d\n", inbytesleft);
192ad30f8e7SGabor Kovesdan printf("outbytesleft = %d\n", outbytesleft);
193ad30f8e7SGabor Kovesdan printf("outptr - outbuf = %d\n", (uint8_t *)outptr - (uint8_t *)outbuf);
194ad30f8e7SGabor Kovesdan printf("errno = %d\n", errno);
195ad30f8e7SGabor Kovesdan printf("ret = %d\n", (int)ret);
196ad30f8e7SGabor Kovesdan #endif
197ad30f8e7SGabor Kovesdan
198ad30f8e7SGabor Kovesdan if (((const uint8_t *)inptr - (uint8_t *)inbuf == 1) && (inbytesleft == 1) &&
199ad30f8e7SGabor Kovesdan (outbytesleft == 8) && ((uint8_t *)outptr - (uint8_t *)outbuf == 8) &&
200ad30f8e7SGabor Kovesdan (errno == EINVAL) && ((size_t)ret == (size_t)-1))
201ad30f8e7SGabor Kovesdan return (0);
202ad30f8e7SGabor Kovesdan else
203ad30f8e7SGabor Kovesdan return (1);
204ad30f8e7SGabor Kovesdan }
205ad30f8e7SGabor Kovesdan
206ad30f8e7SGabor Kovesdan static int
conv_eilseq(void)207ad30f8e7SGabor Kovesdan conv_eilseq(void)
208ad30f8e7SGabor Kovesdan {
209ad30f8e7SGabor Kovesdan iconv_t cd;
210ad30f8e7SGabor Kovesdan size_t inbytesleft, outbytesleft;
211efe014e6SEdward Tomasz Napierala char *inptr;
212ad30f8e7SGabor Kovesdan char *outptr;
213ad30f8e7SGabor Kovesdan uint32_t outbuf[4];
214ad30f8e7SGabor Kovesdan uint16_t inbuf[1] = { 0x8AC0 };
215ad30f8e7SGabor Kovesdan int ret;
216ad30f8e7SGabor Kovesdan
217ad30f8e7SGabor Kovesdan if ((cd = iconv_open("Latin2", "UTF-16LE")) == (iconv_t)-1)
218ad30f8e7SGabor Kovesdan return (1);
219ad30f8e7SGabor Kovesdan
220efe014e6SEdward Tomasz Napierala inptr = (char *)inbuf;
221ad30f8e7SGabor Kovesdan outptr = (char *)outbuf;
222ad30f8e7SGabor Kovesdan inbytesleft = 4;
223ad30f8e7SGabor Kovesdan outbytesleft = 16;
224ad30f8e7SGabor Kovesdan
225ad30f8e7SGabor Kovesdan errno = 0;
226ad30f8e7SGabor Kovesdan ret = iconv(cd, &inptr, &inbytesleft, &outptr, &outbytesleft);
227ad30f8e7SGabor Kovesdan
228ad30f8e7SGabor Kovesdan #ifdef VERBOSE
229ad30f8e7SGabor Kovesdan printf("inptr - inbuf = %d\n", (const uint8_t *)inptr - (uint8_t *)inbuf);
230ad30f8e7SGabor Kovesdan printf("inbytesleft = %d\n", inbytesleft);
231ad30f8e7SGabor Kovesdan printf("outbytesleft = %d\n", outbytesleft);
232ad30f8e7SGabor Kovesdan printf("outptr - outbuf = %d\n", (uint8_t *)outptr - (uint8_t *)outbuf);
233ad30f8e7SGabor Kovesdan printf("errno = %d\n", errno);
234ad30f8e7SGabor Kovesdan printf("ret = %d\n", (int)ret);
235ad30f8e7SGabor Kovesdan #endif
236ad30f8e7SGabor Kovesdan
237ad30f8e7SGabor Kovesdan if (((const uint8_t *)inptr - (uint8_t *)inbuf == 0) && (inbytesleft == 4) &&
238ad30f8e7SGabor Kovesdan (outbytesleft == 16) && ((uint8_t *)outptr - (uint8_t *)outbuf == 0) &&
239ad30f8e7SGabor Kovesdan (errno == EILSEQ) && ((size_t)ret == (size_t)-1))
240ad30f8e7SGabor Kovesdan return (0);
241ad30f8e7SGabor Kovesdan else
242ad30f8e7SGabor Kovesdan return (1);
243ad30f8e7SGabor Kovesdan }
244ad30f8e7SGabor Kovesdan
245ad30f8e7SGabor Kovesdan static void
test(int (tester)(void),const char * label)246ad30f8e7SGabor Kovesdan test(int (tester) (void), const char * label)
247ad30f8e7SGabor Kovesdan {
248ad30f8e7SGabor Kovesdan int ret;
249ad30f8e7SGabor Kovesdan
250ad30f8e7SGabor Kovesdan if ((ret = tester()))
251ad30f8e7SGabor Kovesdan printf("%s failed (%d)\n", label, ret);
252ad30f8e7SGabor Kovesdan else
253ad30f8e7SGabor Kovesdan printf("%s succeeded\n", label);
254ad30f8e7SGabor Kovesdan }
255ad30f8e7SGabor Kovesdan
256ad30f8e7SGabor Kovesdan int
main(void)257ad30f8e7SGabor Kovesdan main(void)
258ad30f8e7SGabor Kovesdan {
259ad30f8e7SGabor Kovesdan
260ad30f8e7SGabor Kovesdan test(open_1, "open_1");
261ad30f8e7SGabor Kovesdan test(open_2, "open_2");
262ad30f8e7SGabor Kovesdan test(close_1, "close_1");
263ad30f8e7SGabor Kovesdan test(conv_ret, "conv_ret");
264ad30f8e7SGabor Kovesdan test(conv_ebadf, "conv_ebadf");
265ad30f8e7SGabor Kovesdan test(conv_2big, "conv_2big");
266ad30f8e7SGabor Kovesdan test(conv_einval, "conv_einval");
267ad30f8e7SGabor Kovesdan test(conv_eilseq, "conv_eilseq");
268ad30f8e7SGabor Kovesdan }
269