3 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 # A placeholder to ensure that this m4 file gets included by aclocal.
9 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF], [])
11 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF_SUPPORT],
13 dnl This macro relies on am_cv_func_iconv and gl_func_iconv_gnu from
14 dnl gl_FUNC_ICONV_OPEN, but is called from within gl_FUNC_ICONV_OPEN.
15 dnl *Not* AC_REQUIRE([gl_FUNC_ICONV_OPEN]).
16 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17 AC_REQUIRE([gl_ICONV_H_DEFAULTS])
18 if test "$am_cv_func_iconv" = yes; then
19 AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
20 [gl_cv_func_iconv_supports_utf],
23 LIBS="$LIBS $LIBICONV"
34 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
36 static const char input[] =
37 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
38 static const char expected[] =
39 "\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]";
41 cd = iconv_open ("UTF-16BE", "UTF-8");
42 if (cd == (iconv_t)(-1))
53 inbytesleft = sizeof (input) - 1;
55 outbytesleft = sizeof (buf);
57 (ICONV_CONST char **) &inptr, &inbytesleft,
58 &outptr, &outbytesleft);
59 if (!(res == 0 && inbytesleft == 0))
61 else if (!(outptr == buf + (sizeof (expected) - 1)))
63 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
65 else if (!(iconv_close (cd) == 0))
69 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
71 static const char input[] =
72 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
73 static const char expected[] =
74 "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";
76 cd = iconv_open ("UTF-16LE", "UTF-8");
77 if (cd == (iconv_t)(-1))
88 inbytesleft = sizeof (input) - 1;
90 outbytesleft = sizeof (buf);
92 (ICONV_CONST char **) &inptr, &inbytesleft,
93 &outptr, &outbytesleft);
94 if (!(res == 0 && inbytesleft == 0))
96 else if (!(outptr == buf + (sizeof (expected) - 1)))
98 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
100 else if (!(iconv_close (cd) == 0))
104 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
106 static const char input[] =
107 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
108 static const char expected[] =
109 "\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]";
111 cd = iconv_open ("UTF-32BE", "UTF-8");
112 if (cd == (iconv_t)(-1))
123 inbytesleft = sizeof (input) - 1;
125 outbytesleft = sizeof (buf);
127 (ICONV_CONST char **) &inptr, &inbytesleft,
128 &outptr, &outbytesleft);
129 if (!(res == 0 && inbytesleft == 0))
131 else if (!(outptr == buf + (sizeof (expected) - 1)))
133 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
135 else if (!(iconv_close (cd) == 0))
139 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
141 static const char input[] =
142 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
143 static const char expected[] =
144 "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";
146 cd = iconv_open ("UTF-32LE", "UTF-8");
147 if (cd == (iconv_t)(-1))
158 inbytesleft = sizeof (input) - 1;
160 outbytesleft = sizeof (buf);
162 (ICONV_CONST char **) &inptr, &inbytesleft,
163 &outptr, &outbytesleft);
164 if (!(res == 0 && inbytesleft == 0))
166 else if (!(outptr == buf + (sizeof (expected) - 1)))
168 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
170 else if (!(iconv_close (cd) == 0))
174 /* Test conversion from UTF-16BE to UTF-8 with no errors.
175 This test fails on NetBSD 3.0. */
177 static const char input[] =
178 "\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]";
179 static const char expected[] =
180 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
182 cd = iconv_open ("UTF-8", "UTF-16BE");
183 if (cd == (iconv_t)(-1))
194 inbytesleft = sizeof (input) - 1;
196 outbytesleft = sizeof (buf);
198 (ICONV_CONST char **) &inptr, &inbytesleft,
199 &outptr, &outbytesleft);
200 if (!(res == 0 && inbytesleft == 0))
202 else if (!(outptr == buf + (sizeof (expected) - 1)))
204 else if (!(memcmp (buf, expected, sizeof (expected) - 1) == 0))
206 else if (!(iconv_close (cd) == 0))
212 [gl_cv_func_iconv_supports_utf=yes],
213 [gl_cv_func_iconv_supports_utf=no],
215 dnl We know that GNU libiconv, GNU libc, musl libc, and Solaris >= 9 do.
216 dnl OSF/1 5.1 has these encodings, but inserts a BOM in the "to"
218 gl_cv_func_iconv_supports_utf="$gl_cross_guess_normal"
219 if test $gl_func_iconv_gnu = yes; then
220 gl_cv_func_iconv_supports_utf="guessing yes"
224 *-musl* | midipix*) gl_cv_func_iconv_supports_utf="guessing yes" ;;
225 solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf="guessing yes" ;;