iconv.m4 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # iconv.m4 serial 24
  2. dnl Copyright (C) 2000-2002, 2007-2014, 2016-2021 Free Software Foundation,
  3. dnl 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.
  7. dnl From Bruno Haible.
  8. AC_PREREQ([2.64])
  9. dnl Note: AM_ICONV is documented in the GNU gettext manual
  10. dnl <https://www.gnu.org/software/gettext/manual/html_node/AM_005fICONV.html>.
  11. dnl Don't make changes that are incompatible with that documentation!
  12. AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
  13. [
  14. dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
  15. AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
  16. AC_REQUIRE([AC_LIB_RPATH])
  17. dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
  18. dnl accordingly.
  19. AC_LIB_LINKFLAGS_BODY([iconv])
  20. ])
  21. AC_DEFUN([AM_ICONV_LINK],
  22. [
  23. dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
  24. dnl those with the standalone portable GNU libiconv installed).
  25. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  26. dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
  27. dnl accordingly.
  28. AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
  29. dnl Add $INCICONV to CPPFLAGS before performing the following checks,
  30. dnl because if the user has installed libiconv and not disabled its use
  31. dnl via --without-libiconv-prefix, he wants to use it. The first
  32. dnl AC_LINK_IFELSE will then fail, the second AC_LINK_IFELSE will succeed.
  33. am_save_CPPFLAGS="$CPPFLAGS"
  34. AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
  35. AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
  36. am_cv_func_iconv="no, consider installing GNU libiconv"
  37. am_cv_lib_iconv=no
  38. AC_LINK_IFELSE(
  39. [AC_LANG_PROGRAM(
  40. [[
  41. #include <stdlib.h>
  42. #include <iconv.h>
  43. ]],
  44. [[iconv_t cd = iconv_open("","");
  45. iconv(cd,NULL,NULL,NULL,NULL);
  46. iconv_close(cd);]])],
  47. [am_cv_func_iconv=yes])
  48. if test "$am_cv_func_iconv" != yes; then
  49. am_save_LIBS="$LIBS"
  50. LIBS="$LIBS $LIBICONV"
  51. AC_LINK_IFELSE(
  52. [AC_LANG_PROGRAM(
  53. [[
  54. #include <stdlib.h>
  55. #include <iconv.h>
  56. ]],
  57. [[iconv_t cd = iconv_open("","");
  58. iconv(cd,NULL,NULL,NULL,NULL);
  59. iconv_close(cd);]])],
  60. [am_cv_lib_iconv=yes]
  61. [am_cv_func_iconv=yes])
  62. LIBS="$am_save_LIBS"
  63. fi
  64. ])
  65. if test "$am_cv_func_iconv" = yes; then
  66. AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
  67. dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
  68. dnl Solaris 10.
  69. am_save_LIBS="$LIBS"
  70. if test $am_cv_lib_iconv = yes; then
  71. LIBS="$LIBS $LIBICONV"
  72. fi
  73. am_cv_func_iconv_works=no
  74. for ac_iconv_const in '' 'const'; do
  75. AC_RUN_IFELSE(
  76. [AC_LANG_PROGRAM(
  77. [[
  78. #include <iconv.h>
  79. #include <string.h>
  80. #ifndef ICONV_CONST
  81. # define ICONV_CONST $ac_iconv_const
  82. #endif
  83. ]],
  84. [[int result = 0;
  85. /* Test against AIX 5.1...7.2 bug: Failures are not distinguishable from
  86. successful returns. This is even documented in
  87. <https://www.ibm.com/support/knowledgecenter/ssw_aix_72/i_bostechref/iconv.html> */
  88. {
  89. iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
  90. if (cd_utf8_to_88591 != (iconv_t)(-1))
  91. {
  92. static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
  93. char buf[10];
  94. ICONV_CONST char *inptr = input;
  95. size_t inbytesleft = strlen (input);
  96. char *outptr = buf;
  97. size_t outbytesleft = sizeof (buf);
  98. size_t res = iconv (cd_utf8_to_88591,
  99. &inptr, &inbytesleft,
  100. &outptr, &outbytesleft);
  101. if (res == 0)
  102. result |= 1;
  103. iconv_close (cd_utf8_to_88591);
  104. }
  105. }
  106. /* Test against Solaris 10 bug: Failures are not distinguishable from
  107. successful returns. */
  108. {
  109. iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
  110. if (cd_ascii_to_88591 != (iconv_t)(-1))
  111. {
  112. static ICONV_CONST char input[] = "\263";
  113. char buf[10];
  114. ICONV_CONST char *inptr = input;
  115. size_t inbytesleft = strlen (input);
  116. char *outptr = buf;
  117. size_t outbytesleft = sizeof (buf);
  118. size_t res = iconv (cd_ascii_to_88591,
  119. &inptr, &inbytesleft,
  120. &outptr, &outbytesleft);
  121. if (res == 0)
  122. result |= 2;
  123. iconv_close (cd_ascii_to_88591);
  124. }
  125. }
  126. /* Test against AIX 6.1..7.1 bug: Buffer overrun. */
  127. {
  128. iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
  129. if (cd_88591_to_utf8 != (iconv_t)(-1))
  130. {
  131. static ICONV_CONST char input[] = "\304";
  132. static char buf[2] = { (char)0xDE, (char)0xAD };
  133. ICONV_CONST char *inptr = input;
  134. size_t inbytesleft = 1;
  135. char *outptr = buf;
  136. size_t outbytesleft = 1;
  137. size_t res = iconv (cd_88591_to_utf8,
  138. &inptr, &inbytesleft,
  139. &outptr, &outbytesleft);
  140. if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
  141. result |= 4;
  142. iconv_close (cd_88591_to_utf8);
  143. }
  144. }
  145. #if 0 /* This bug could be worked around by the caller. */
  146. /* Test against HP-UX 11.11 bug: Positive return value instead of 0. */
  147. {
  148. iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
  149. if (cd_88591_to_utf8 != (iconv_t)(-1))
  150. {
  151. static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
  152. char buf[50];
  153. ICONV_CONST char *inptr = input;
  154. size_t inbytesleft = strlen (input);
  155. char *outptr = buf;
  156. size_t outbytesleft = sizeof (buf);
  157. size_t res = iconv (cd_88591_to_utf8,
  158. &inptr, &inbytesleft,
  159. &outptr, &outbytesleft);
  160. if ((int)res > 0)
  161. result |= 8;
  162. iconv_close (cd_88591_to_utf8);
  163. }
  164. }
  165. #endif
  166. /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
  167. provided. */
  168. {
  169. /* Try standardized names. */
  170. iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP");
  171. /* Try IRIX, OSF/1 names. */
  172. iconv_t cd2 = iconv_open ("UTF-8", "eucJP");
  173. /* Try AIX names. */
  174. iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP");
  175. /* Try HP-UX names. */
  176. iconv_t cd4 = iconv_open ("utf8", "eucJP");
  177. if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1)
  178. && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1))
  179. result |= 16;
  180. if (cd1 != (iconv_t)(-1))
  181. iconv_close (cd1);
  182. if (cd2 != (iconv_t)(-1))
  183. iconv_close (cd2);
  184. if (cd3 != (iconv_t)(-1))
  185. iconv_close (cd3);
  186. if (cd4 != (iconv_t)(-1))
  187. iconv_close (cd4);
  188. }
  189. return result;
  190. ]])],
  191. [am_cv_func_iconv_works=yes], ,
  192. [case "$host_os" in
  193. aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
  194. *) am_cv_func_iconv_works="guessing yes" ;;
  195. esac])
  196. test "$am_cv_func_iconv_works" = no || break
  197. done
  198. LIBS="$am_save_LIBS"
  199. ])
  200. case "$am_cv_func_iconv_works" in
  201. *no) am_func_iconv=no am_cv_lib_iconv=no ;;
  202. *) am_func_iconv=yes ;;
  203. esac
  204. else
  205. am_func_iconv=no am_cv_lib_iconv=no
  206. fi
  207. if test "$am_func_iconv" = yes; then
  208. AC_DEFINE([HAVE_ICONV], [1],
  209. [Define if you have the iconv() function and it works.])
  210. fi
  211. if test "$am_cv_lib_iconv" = yes; then
  212. AC_MSG_CHECKING([how to link with libiconv])
  213. AC_MSG_RESULT([$LIBICONV])
  214. else
  215. dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
  216. dnl either.
  217. CPPFLAGS="$am_save_CPPFLAGS"
  218. LIBICONV=
  219. LTLIBICONV=
  220. fi
  221. AC_SUBST([LIBICONV])
  222. AC_SUBST([LTLIBICONV])
  223. ])
  224. dnl Define AM_ICONV using AC_DEFUN_ONCE, in order to avoid warnings like
  225. dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
  226. dnl This is tricky because of the way 'aclocal' is implemented:
  227. dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
  228. dnl Otherwise aclocal's initial scan pass would miss the macro definition.
  229. dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
  230. dnl Otherwise aclocal would emit many "Use of uninitialized value $1"
  231. dnl warnings.
  232. AC_DEFUN_ONCE([AM_ICONV],
  233. [
  234. AM_ICONV_LINK
  235. if test "$am_cv_func_iconv" = yes; then
  236. AC_CACHE_CHECK([whether iconv is compatible with its POSIX signature],
  237. [gl_cv_iconv_nonconst],
  238. [AC_COMPILE_IFELSE(
  239. [AC_LANG_PROGRAM(
  240. [[
  241. #include <stdlib.h>
  242. #include <iconv.h>
  243. extern
  244. #ifdef __cplusplus
  245. "C"
  246. #endif
  247. size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
  248. ]],
  249. [[]])],
  250. [gl_cv_iconv_nonconst=yes],
  251. [gl_cv_iconv_nonconst=no])
  252. ])
  253. else
  254. dnl When compiling GNU libiconv on a system that does not have iconv yet,
  255. dnl pick the POSIX compliant declaration without 'const'.
  256. gl_cv_iconv_nonconst=yes
  257. fi
  258. if test $gl_cv_iconv_nonconst = yes; then
  259. iconv_arg1=""
  260. else
  261. iconv_arg1="const"
  262. fi
  263. AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1],
  264. [Define as const if the declaration of iconv() needs const.])
  265. dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
  266. m4_ifdef([gl_ICONV_H_DEFAULTS],
  267. [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
  268. if test $gl_cv_iconv_nonconst != yes; then
  269. ICONV_CONST="const"
  270. fi
  271. ])
  272. ])