1 # iconv_open-utf.m4 serial 1
2 dnl Copyright (C) 2007-2018 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.
7 # A placeholder to ensure that this m4 file gets included by aclocal.
8 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF], [])
10 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF_SUPPORT],
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],
22 LIBS="$LIBS $LIBICONV"
33 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
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]";
40 cd = iconv_open ("UTF-16BE", "UTF-8");
41 if (cd == (iconv_t)(-1))
52 inbytesleft = sizeof (input) - 1;
54 outbytesleft = sizeof (buf);
56 (ICONV_CONST char **) &inptr, &inbytesleft,
57 &outptr, &outbytesleft);
58 if (!(res == 0 && inbytesleft == 0))
60 else if (!(outptr == buf + (sizeof (expected) - 1)))
62 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
64 else if (!(iconv_close (cd) == 0))
68 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
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";
75 cd = iconv_open ("UTF-16LE", "UTF-8");
76 if (cd == (iconv_t)(-1))
87 inbytesleft = sizeof (input) - 1;
89 outbytesleft = sizeof (buf);
91 (ICONV_CONST char **) &inptr, &inbytesleft,
92 &outptr, &outbytesleft);
93 if (!(res == 0 && inbytesleft == 0))
95 else if (!(outptr == buf + (sizeof (expected) - 1)))
97 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
99 else if (!(iconv_close (cd) == 0))
103 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
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]";
110 cd = iconv_open ("UTF-32BE", "UTF-8");
111 if (cd == (iconv_t)(-1))
122 inbytesleft = sizeof (input) - 1;
124 outbytesleft = sizeof (buf);
126 (ICONV_CONST char **) &inptr, &inbytesleft,
127 &outptr, &outbytesleft);
128 if (!(res == 0 && inbytesleft == 0))
130 else if (!(outptr == buf + (sizeof (expected) - 1)))
132 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
134 else if (!(iconv_close (cd) == 0))
138 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
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";
145 cd = iconv_open ("UTF-32LE", "UTF-8");
146 if (cd == (iconv_t)(-1))
157 inbytesleft = sizeof (input) - 1;
159 outbytesleft = sizeof (buf);
161 (ICONV_CONST char **) &inptr, &inbytesleft,
162 &outptr, &outbytesleft);
163 if (!(res == 0 && inbytesleft == 0))
165 else if (!(outptr == buf + (sizeof (expected) - 1)))
167 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
169 else if (!(iconv_close (cd) == 0))
173 /* Test conversion from UTF-16BE to UTF-8 with no errors.
174 This test fails on NetBSD 3.0. */
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]";
181 cd = iconv_open ("UTF-8", "UTF-16BE");
182 if (cd == (iconv_t)(-1))
193 inbytesleft = sizeof (input) - 1;
195 outbytesleft = sizeof (buf);
197 (ICONV_CONST char **) &inptr, &inbytesleft,
198 &outptr, &outbytesleft);
199 if (!(res == 0 && inbytesleft == 0))
201 else if (!(outptr == buf + (sizeof (expected) - 1)))
203 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
205 else if (!(iconv_close (cd) == 0))
211 [gl_cv_func_iconv_supports_utf=yes],
212 [gl_cv_func_iconv_supports_utf=no],
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"
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
223 solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf=yes ;;