maint: update almost all copyright ranges to include 2011
[gnulib.git] / m4 / iconv_open.m4
1 # iconv_open.m4 serial 12
2 dnl Copyright (C) 2007-2011 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 AC_DEFUN([gl_FUNC_ICONV_OPEN],
8 [
9   AC_REQUIRE([AM_ICONV])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_REQUIRE([gl_ICONV_H_DEFAULTS])
12   if test "$am_cv_func_iconv" = yes; then
13     dnl Provide the <iconv.h> override, for the sake of the C++ aliases.
14     gl_REPLACE_ICONV_H
15     dnl Test whether iconv_open accepts standardized encoding names.
16     dnl We know that GNU libiconv and GNU libc do.
17     AC_EGREP_CPP([gnu_iconv], [
18       #include <iconv.h>
19       #if defined _LIBICONV_VERSION || (defined __GLIBC__ && !defined __UCLIBC__)
20        gnu_iconv
21       #endif
22       ], [gl_func_iconv_gnu=yes], [gl_func_iconv_gnu=no])
23     if test $gl_func_iconv_gnu = no; then
24       iconv_flavor=
25       case "$host_os" in
26         aix*)     iconv_flavor=ICONV_FLAVOR_AIX ;;
27         irix*)    iconv_flavor=ICONV_FLAVOR_IRIX ;;
28         hpux*)    iconv_flavor=ICONV_FLAVOR_HPUX ;;
29         osf*)     iconv_flavor=ICONV_FLAVOR_OSF ;;
30         solaris*) iconv_flavor=ICONV_FLAVOR_SOLARIS ;;
31       esac
32       if test -n "$iconv_flavor"; then
33         AC_DEFINE_UNQUOTED([ICONV_FLAVOR], [$iconv_flavor],
34           [Define to a symbolic name denoting the flavor of iconv_open()
35            implementation.])
36         gl_REPLACE_ICONV_OPEN
37       fi
38     fi
39   fi
40 ])
41
42 AC_DEFUN([gl_REPLACE_ICONV_OPEN],
43 [
44   gl_REPLACE_ICONV_H
45   REPLACE_ICONV_OPEN=1
46   AC_LIBOBJ([iconv_open])
47 ])
48
49 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF],
50 [
51   AC_REQUIRE([gl_FUNC_ICONV_OPEN])
52   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
53   AC_REQUIRE([gl_ICONV_H_DEFAULTS])
54   if test "$am_cv_func_iconv" = yes; then
55     AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
56       [gl_cv_func_iconv_supports_utf],
57       [
58         save_LIBS="$LIBS"
59         LIBS="$LIBS $LIBICONV"
60         AC_RUN_IFELSE(
61           [AC_LANG_SOURCE([[
62 #include <iconv.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 int main ()
68 {
69   int result = 0;
70   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
71   {
72     static const char input[] =
73       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
74     static const char expected[] =
75       "\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]";
76     iconv_t cd;
77     cd = iconv_open ("UTF-16BE", "UTF-8");
78     if (cd == (iconv_t)(-1))
79       result |= 1;
80     else
81       {
82         char buf[100];
83         const char *inptr;
84         size_t inbytesleft;
85         char *outptr;
86         size_t outbytesleft;
87         size_t res;
88         inptr = input;
89         inbytesleft = sizeof (input) - 1;
90         outptr = buf;
91         outbytesleft = sizeof (buf);
92         res = iconv (cd,
93                      (ICONV_CONST char **) &inptr, &inbytesleft,
94                      &outptr, &outbytesleft);
95         if (!(res == 0 && inbytesleft == 0))
96           result |= 1;
97         else if (!(outptr == buf + (sizeof (expected) - 1)))
98           result |= 1;
99         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
100           result |= 1;
101         else if (!(iconv_close (cd) == 0))
102           result |= 1;
103       }
104   }
105   /* Test conversion from UTF-8 to UTF-16LE with no errors.  */
106   {
107     static const char input[] =
108       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
109     static const char expected[] =
110       "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";
111     iconv_t cd;
112     cd = iconv_open ("UTF-16LE", "UTF-8");
113     if (cd == (iconv_t)(-1))
114       result |= 2;
115     else
116       {
117         char buf[100];
118         const char *inptr;
119         size_t inbytesleft;
120         char *outptr;
121         size_t outbytesleft;
122         size_t res;
123         inptr = input;
124         inbytesleft = sizeof (input) - 1;
125         outptr = buf;
126         outbytesleft = sizeof (buf);
127         res = iconv (cd,
128                      (ICONV_CONST char **) &inptr, &inbytesleft,
129                      &outptr, &outbytesleft);
130         if (!(res == 0 && inbytesleft == 0))
131           result |= 2;
132         else if (!(outptr == buf + (sizeof (expected) - 1)))
133           result |= 2;
134         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
135           result |= 2;
136         else if (!(iconv_close (cd) == 0))
137           result |= 2;
138       }
139   }
140   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
141   {
142     static const char input[] =
143       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
144     static const char expected[] =
145       "\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]";
146     iconv_t cd;
147     cd = iconv_open ("UTF-32BE", "UTF-8");
148     if (cd == (iconv_t)(-1))
149       result |= 4;
150     else
151       {
152         char buf[100];
153         const char *inptr;
154         size_t inbytesleft;
155         char *outptr;
156         size_t outbytesleft;
157         size_t res;
158         inptr = input;
159         inbytesleft = sizeof (input) - 1;
160         outptr = buf;
161         outbytesleft = sizeof (buf);
162         res = iconv (cd,
163                      (ICONV_CONST char **) &inptr, &inbytesleft,
164                      &outptr, &outbytesleft);
165         if (!(res == 0 && inbytesleft == 0))
166           result |= 4;
167         else if (!(outptr == buf + (sizeof (expected) - 1)))
168           result |= 4;
169         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
170           result |= 4;
171         else if (!(iconv_close (cd) == 0))
172           result |= 4;
173       }
174   }
175   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
176   {
177     static const char input[] =
178       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
179     static const char expected[] =
180       "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";
181     iconv_t cd;
182     cd = iconv_open ("UTF-32LE", "UTF-8");
183     if (cd == (iconv_t)(-1))
184       result |= 8;
185     else
186       {
187         char buf[100];
188         const char *inptr;
189         size_t inbytesleft;
190         char *outptr;
191         size_t outbytesleft;
192         size_t res;
193         inptr = input;
194         inbytesleft = sizeof (input) - 1;
195         outptr = buf;
196         outbytesleft = sizeof (buf);
197         res = iconv (cd,
198                      (ICONV_CONST char **) &inptr, &inbytesleft,
199                      &outptr, &outbytesleft);
200         if (!(res == 0 && inbytesleft == 0))
201           result |= 8;
202         else if (!(outptr == buf + (sizeof (expected) - 1)))
203           result |= 8;
204         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
205           result |= 8;
206         else if (!(iconv_close (cd) == 0))
207           result |= 8;
208       }
209   }
210   /* Test conversion from UTF-16BE to UTF-8 with no errors.
211      This test fails on NetBSD 3.0.  */
212   {
213     static const char input[] =
214       "\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]";
215     static const char expected[] =
216       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
217     iconv_t cd;
218     cd = iconv_open ("UTF-8", "UTF-16BE");
219     if (cd == (iconv_t)(-1))
220       result |= 16;
221     else
222       {
223         char buf[100];
224         const char *inptr;
225         size_t inbytesleft;
226         char *outptr;
227         size_t outbytesleft;
228         size_t res;
229         inptr = input;
230         inbytesleft = sizeof (input) - 1;
231         outptr = buf;
232         outbytesleft = sizeof (buf);
233         res = iconv (cd,
234                      (ICONV_CONST char **) &inptr, &inbytesleft,
235                      &outptr, &outbytesleft);
236         if (!(res == 0 && inbytesleft == 0))
237           result |= 16;
238         else if (!(outptr == buf + (sizeof (expected) - 1)))
239           result |= 16;
240         else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
241           result |= 16;
242         else if (!(iconv_close (cd) == 0))
243           result |= 16;
244       }
245   }
246   return result;
247 }]])],
248           [gl_cv_func_iconv_supports_utf=yes],
249           [gl_cv_func_iconv_supports_utf=no],
250           [
251            dnl We know that GNU libiconv, GNU libc, and Solaris >= 9 do.
252            dnl OSF/1 5.1 has these encodings, but inserts a BOM in the "to"
253            dnl direction.
254            gl_cv_func_iconv_supports_utf=no
255            if test $gl_func_iconv_gnu = yes; then
256              gl_cv_func_iconv_supports_utf=yes
257            else
258 changequote(,)dnl
259              case "$host_os" in
260                solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf=yes ;;
261              esac
262 changequote([,])dnl
263            fi
264           ])
265         LIBS="$save_LIBS"
266       ])
267     if test $gl_cv_func_iconv_supports_utf = no; then
268       REPLACE_ICONV_UTF=1
269       AC_DEFINE([REPLACE_ICONV_UTF], [1],
270         [Define if the iconv() functions are enhanced to handle the UTF-{16,32}{BE,LE} encodings.])
271       REPLACE_ICONV=1
272       gl_REPLACE_ICONV_OPEN
273       AC_LIBOBJ([iconv])
274       AC_LIBOBJ([iconv_close])
275     fi
276   fi
277 ])