Merge branch 'upstream' into stable
[gnulib.git] / m4 / iconv_open.m4
1 # iconv_open.m4 serial 8
2 dnl Copyright (C) 2007-2010 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__
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     if test -n "$am_cv_proto_iconv_arg1"; then
56       ICONV_CONST="const"
57     else
58       ICONV_CONST=
59     fi
60     AC_SUBST([ICONV_CONST])
61     AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
62       [gl_cv_func_iconv_supports_utf],
63       [
64         save_LIBS="$LIBS"
65         LIBS="$LIBS $LIBICONV"
66         AC_RUN_IFELSE(
67           [AC_LANG_SOURCE([[
68 #include <iconv.h>
69 #include <errno.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #define ASSERT(expr) if (!(expr)) return 1;
74 int main ()
75 {
76   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
77   {
78     static const char input[] =
79       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
80     static const char expected[] =
81       "\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]";
82     iconv_t cd;
83     char buf[100];
84     const char *inptr;
85     size_t inbytesleft;
86     char *outptr;
87     size_t outbytesleft;
88     size_t res;
89     cd = iconv_open ("UTF-16BE", "UTF-8");
90     ASSERT (cd != (iconv_t)(-1));
91     inptr = input;
92     inbytesleft = sizeof (input) - 1;
93     outptr = buf;
94     outbytesleft = sizeof (buf);
95     res = iconv (cd,
96                  (ICONV_CONST char **) &inptr, &inbytesleft,
97                  &outptr, &outbytesleft);
98     ASSERT (res == 0 && inbytesleft == 0);
99     ASSERT (outptr == buf + (sizeof (expected) - 1));
100     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
101     ASSERT (iconv_close (cd) == 0);
102   }
103   /* Test conversion from UTF-8 to UTF-16LE 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       "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";
109     iconv_t cd;
110     char buf[100];
111     const char *inptr;
112     size_t inbytesleft;
113     char *outptr;
114     size_t outbytesleft;
115     size_t res;
116     cd = iconv_open ("UTF-16LE", "UTF-8");
117     ASSERT (cd != (iconv_t)(-1));
118     inptr = input;
119     inbytesleft = sizeof (input) - 1;
120     outptr = buf;
121     outbytesleft = sizeof (buf);
122     res = iconv (cd,
123                  (ICONV_CONST char **) &inptr, &inbytesleft,
124                  &outptr, &outbytesleft);
125     ASSERT (res == 0 && inbytesleft == 0);
126     ASSERT (outptr == buf + (sizeof (expected) - 1));
127     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
128     ASSERT (iconv_close (cd) == 0);
129   }
130   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
131   {
132     static const char input[] =
133       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
134     static const char expected[] =
135       "\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]";
136     iconv_t cd;
137     char buf[100];
138     const char *inptr;
139     size_t inbytesleft;
140     char *outptr;
141     size_t outbytesleft;
142     size_t res;
143     cd = iconv_open ("UTF-32BE", "UTF-8");
144     ASSERT (cd != (iconv_t)(-1));
145     inptr = input;
146     inbytesleft = sizeof (input) - 1;
147     outptr = buf;
148     outbytesleft = sizeof (buf);
149     res = iconv (cd,
150                  (ICONV_CONST char **) &inptr, &inbytesleft,
151                  &outptr, &outbytesleft);
152     ASSERT (res == 0 && inbytesleft == 0);
153     ASSERT (outptr == buf + (sizeof (expected) - 1));
154     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
155     ASSERT (iconv_close (cd) == 0);
156   }
157   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
158   {
159     static const char input[] =
160       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
161     static const char expected[] =
162       "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";
163     iconv_t cd;
164     char buf[100];
165     const char *inptr;
166     size_t inbytesleft;
167     char *outptr;
168     size_t outbytesleft;
169     size_t res;
170     cd = iconv_open ("UTF-32LE", "UTF-8");
171     ASSERT (cd != (iconv_t)(-1));
172     inptr = input;
173     inbytesleft = sizeof (input) - 1;
174     outptr = buf;
175     outbytesleft = sizeof (buf);
176     res = iconv (cd,
177                  (ICONV_CONST char **) &inptr, &inbytesleft,
178                  &outptr, &outbytesleft);
179     ASSERT (res == 0 && inbytesleft == 0);
180     ASSERT (outptr == buf + (sizeof (expected) - 1));
181     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
182     ASSERT (iconv_close (cd) == 0);
183   }
184   /* Test conversion from UTF-16BE to UTF-8 with no errors.
185      This test fails on NetBSD 3.0.  */
186   {
187     static const char input[] =
188       "\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]";
189     static const char expected[] =
190       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
191     iconv_t cd;
192     char buf[100];
193     const char *inptr;
194     size_t inbytesleft;
195     char *outptr;
196     size_t outbytesleft;
197     size_t res;
198     cd = iconv_open ("UTF-8", "UTF-16BE");
199     ASSERT (cd != (iconv_t)(-1));
200     inptr = input;
201     inbytesleft = sizeof (input) - 1;
202     outptr = buf;
203     outbytesleft = sizeof (buf);
204     res = iconv (cd,
205                  (ICONV_CONST char **) &inptr, &inbytesleft,
206                  &outptr, &outbytesleft);
207     ASSERT (res == 0 && inbytesleft == 0);
208     ASSERT (outptr == buf + (sizeof (expected) - 1));
209     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
210     ASSERT (iconv_close (cd) == 0);
211   }
212   return 0;
213 }]])],
214           [gl_cv_func_iconv_supports_utf=yes],
215           [gl_cv_func_iconv_supports_utf=no],
216           [
217            dnl We know that GNU libiconv, GNU libc, and Solaris >= 9 do.
218            dnl OSF/1 5.1 has these encodings, but inserts a BOM in the "to"
219            dnl direction.
220            gl_cv_func_iconv_supports_utf=no
221            if test $gl_func_iconv_gnu = yes; then
222              gl_cv_func_iconv_supports_utf=yes
223            else
224 changequote(,)dnl
225              case "$host_os" in
226                solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf=yes ;;
227              esac
228 changequote([,])dnl
229            fi
230           ])
231         LIBS="$save_LIBS"
232       ])
233     if test $gl_cv_func_iconv_supports_utf = no; then
234       REPLACE_ICONV_UTF=1
235       AC_DEFINE([REPLACE_ICONV_UTF], [1],
236         [Define if the iconv() functions are enhanced to handle the UTF-{16,32}{BE,LE} encodings.])
237       REPLACE_ICONV=1
238       gl_REPLACE_ICONV_OPEN
239       AC_LIBOBJ([iconv])
240       AC_LIBOBJ([iconv_close])
241     fi
242   fi
243 ])