maint: update copyright
[gnulib.git] / m4 / iconv_open-utf.m4
1 # iconv_open-utf.m4 serial 1
2 dnl Copyright (C) 2007-2014 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 # A placeholder to ensure that this m4 file gets included by aclocal.
8 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF], [])
9
10 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF_SUPPORT],
11 [
12   dnl This macro relies on am_cv_func_iconv and gl_func_iconv_gnu from
13   dnl gl_FUNC_ICONV_OPEN, but is called from within gl_FUNC_ICONV_OPEN.
14   dnl *Not* AC_REQUIRE([gl_FUNC_ICONV_OPEN]).
15   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
16   AC_REQUIRE([gl_ICONV_H_DEFAULTS])
17   if test "$am_cv_func_iconv" = yes; then
18     AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
19       [gl_cv_func_iconv_supports_utf],
20       [
21         save_LIBS="$LIBS"
22         LIBS="$LIBS $LIBICONV"
23         AC_RUN_IFELSE(
24           [AC_LANG_SOURCE([[
25 #include <iconv.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 int main ()
31 {
32   int result = 0;
33   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
34   {
35     static const char input[] =
36       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
37     static const char expected[] =
38       "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
39     iconv_t cd;
40     cd = iconv_open ("UTF-16BE", "UTF-8");
41     if (cd == (iconv_t)(-1))
42       result |= 1;
43     else
44       {
45         char buf[100];
46         const char *inptr;
47         size_t inbytesleft;
48         char *outptr;
49         size_t outbytesleft;
50         size_t res;
51         inptr = input;
52         inbytesleft = sizeof (input) - 1;
53         outptr = buf;
54         outbytesleft = sizeof (buf);
55         res = iconv (cd,
56                      (ICONV_CONST char **) &inptr, &inbytesleft,
57                      &outptr, &outbytesleft);
58         if (!(res == 0 && inbytesleft == 0))
59           result |= 1;
60         else if (!(outptr == buf + (sizeof (expected) - 1)))
61           result |= 1;
62         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
63           result |= 1;
64         else if (!(iconv_close (cd) == 0))
65           result |= 1;
66       }
67   }
68   /* Test conversion from UTF-8 to UTF-16LE with no errors.  */
69   {
70     static const char input[] =
71       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
72     static const char expected[] =
73       "J\000a\000p\000a\000n\000e\000s\000e\000 \000(\000\345\145\054\147\236\212)\000 \000[\000\065\330\015\335\065\330\036\335\065\330\055\335]\000";
74     iconv_t cd;
75     cd = iconv_open ("UTF-16LE", "UTF-8");
76     if (cd == (iconv_t)(-1))
77       result |= 2;
78     else
79       {
80         char buf[100];
81         const char *inptr;
82         size_t inbytesleft;
83         char *outptr;
84         size_t outbytesleft;
85         size_t res;
86         inptr = input;
87         inbytesleft = sizeof (input) - 1;
88         outptr = buf;
89         outbytesleft = sizeof (buf);
90         res = iconv (cd,
91                      (ICONV_CONST char **) &inptr, &inbytesleft,
92                      &outptr, &outbytesleft);
93         if (!(res == 0 && inbytesleft == 0))
94           result |= 2;
95         else if (!(outptr == buf + (sizeof (expected) - 1)))
96           result |= 2;
97         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
98           result |= 2;
99         else if (!(iconv_close (cd) == 0))
100           result |= 2;
101       }
102   }
103   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
104   {
105     static const char input[] =
106       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
107     static const char expected[] =
108       "\000\000\000J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\145\345\000\000\147\054\000\000\212\236\000\000\000)\000\000\000 \000\000\000[\000\001\325\015\000\001\325\036\000\001\325\055\000\000\000]";
109     iconv_t cd;
110     cd = iconv_open ("UTF-32BE", "UTF-8");
111     if (cd == (iconv_t)(-1))
112       result |= 4;
113     else
114       {
115         char buf[100];
116         const char *inptr;
117         size_t inbytesleft;
118         char *outptr;
119         size_t outbytesleft;
120         size_t res;
121         inptr = input;
122         inbytesleft = sizeof (input) - 1;
123         outptr = buf;
124         outbytesleft = sizeof (buf);
125         res = iconv (cd,
126                      (ICONV_CONST char **) &inptr, &inbytesleft,
127                      &outptr, &outbytesleft);
128         if (!(res == 0 && inbytesleft == 0))
129           result |= 4;
130         else if (!(outptr == buf + (sizeof (expected) - 1)))
131           result |= 4;
132         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
133           result |= 4;
134         else if (!(iconv_close (cd) == 0))
135           result |= 4;
136       }
137   }
138   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
139   {
140     static const char input[] =
141       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
142     static const char expected[] =
143       "J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\000\345\145\000\000\054\147\000\000\236\212\000\000)\000\000\000 \000\000\000[\000\000\000\015\325\001\000\036\325\001\000\055\325\001\000]\000\000\000";
144     iconv_t cd;
145     cd = iconv_open ("UTF-32LE", "UTF-8");
146     if (cd == (iconv_t)(-1))
147       result |= 8;
148     else
149       {
150         char buf[100];
151         const char *inptr;
152         size_t inbytesleft;
153         char *outptr;
154         size_t outbytesleft;
155         size_t res;
156         inptr = input;
157         inbytesleft = sizeof (input) - 1;
158         outptr = buf;
159         outbytesleft = sizeof (buf);
160         res = iconv (cd,
161                      (ICONV_CONST char **) &inptr, &inbytesleft,
162                      &outptr, &outbytesleft);
163         if (!(res == 0 && inbytesleft == 0))
164           result |= 8;
165         else if (!(outptr == buf + (sizeof (expected) - 1)))
166           result |= 8;
167         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
168           result |= 8;
169         else if (!(iconv_close (cd) == 0))
170           result |= 8;
171       }
172   }
173   /* Test conversion from UTF-16BE to UTF-8 with no errors.
174      This test fails on NetBSD 3.0.  */
175   {
176     static const char input[] =
177       "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
178     static const char expected[] =
179       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
180     iconv_t cd;
181     cd = iconv_open ("UTF-8", "UTF-16BE");
182     if (cd == (iconv_t)(-1))
183       result |= 16;
184     else
185       {
186         char buf[100];
187         const char *inptr;
188         size_t inbytesleft;
189         char *outptr;
190         size_t outbytesleft;
191         size_t res;
192         inptr = input;
193         inbytesleft = sizeof (input) - 1;
194         outptr = buf;
195         outbytesleft = sizeof (buf);
196         res = iconv (cd,
197                      (ICONV_CONST char **) &inptr, &inbytesleft,
198                      &outptr, &outbytesleft);
199         if (!(res == 0 && inbytesleft == 0))
200           result |= 16;
201         else if (!(outptr == buf + (sizeof (expected) - 1)))
202           result |= 16;
203         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
204           result |= 16;
205         else if (!(iconv_close (cd) == 0))
206           result |= 16;
207       }
208   }
209   return result;
210 }]])],
211           [gl_cv_func_iconv_supports_utf=yes],
212           [gl_cv_func_iconv_supports_utf=no],
213           [
214            dnl We know that GNU libiconv, GNU libc, and Solaris >= 9 do.
215            dnl OSF/1 5.1 has these encodings, but inserts a BOM in the "to"
216            dnl direction.
217            gl_cv_func_iconv_supports_utf=no
218            if test $gl_func_iconv_gnu = yes; then
219              gl_cv_func_iconv_supports_utf=yes
220            else
221 changequote(,)dnl
222              case "$host_os" in
223                solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf=yes ;;
224              esac
225 changequote([,])dnl
226            fi
227           ])
228         LIBS="$save_LIBS"
229       ])
230   fi
231 ])