toutf8.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* toutf8.c --- Convert strings from system locale into UTF-8.
  2. Copyright (C) 2002-2024 Simon Josefsson
  3. This file is part of GNU Libidn.
  4. GNU Libidn is free software: you can redistribute it and/or
  5. modify it under the terms of either:
  6. * the GNU Lesser General Public License as published by the Free
  7. Software Foundation; either version 3 of the License, or (at
  8. your option) any later version.
  9. or
  10. * the GNU General Public License as published by the Free
  11. Software Foundation; either version 2 of the License, or (at
  12. your option) any later version.
  13. or both in parallel, as here.
  14. GNU Libidn is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received copies of the GNU General Public License and
  19. the GNU Lesser General Public License along with this program. If
  20. not, see <https://www.gnu.org/licenses/>. */
  21. #ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. #endif
  24. /* Get prototypes. */
  25. #include "stringprep.h"
  26. /* Get fprintf. */
  27. #include <stdio.h>
  28. /* Get getenv. */
  29. #include <stdlib.h>
  30. /* Get strlen. */
  31. #include <string.h>
  32. /* Get iconv_string. */
  33. #include "striconv.h"
  34. #ifdef _LIBC
  35. # define HAVE_ICONV 1
  36. # define HAVE_LOCALE_H 1
  37. # define HAVE_LANGINFO_CODESET 1
  38. #endif
  39. #include <locale.h>
  40. #ifdef HAVE_LANGINFO_CODESET
  41. # include <langinfo.h>
  42. #endif
  43. #ifdef _LIBC
  44. # define stringprep_locale_charset() nl_langinfo (CODESET)
  45. #else
  46. /**
  47. * stringprep_locale_charset:
  48. *
  49. * Find out current locale charset. The function respect the CHARSET
  50. * environment variable, but typically uses nl_langinfo(CODESET) when
  51. * it is supported. It fall back on "ASCII" if CHARSET isn't set and
  52. * nl_langinfo isn't supported or return anything.
  53. *
  54. * Note that this function return the application's locale's preferred
  55. * charset (or thread's locale's preferred charset, if your system
  56. * support thread-specific locales). It does not return what the
  57. * system may be using. Thus, if you receive data from external
  58. * sources you cannot in general use this function to guess what
  59. * charset it is encoded in. Use stringprep_convert from the external
  60. * representation into the charset returned by this function, to have
  61. * data in the locale encoding.
  62. *
  63. * Return value: Return the character set used by the current locale.
  64. * It will never return NULL, but use "ASCII" as a fallback.
  65. **/
  66. const char *
  67. stringprep_locale_charset (void)
  68. {
  69. const char *charset = getenv ("CHARSET"); /* flawfinder: ignore */
  70. if (charset && *charset)
  71. return charset;
  72. # ifdef HAVE_LANGINFO_CODESET
  73. charset = nl_langinfo (CODESET);
  74. if (charset && *charset)
  75. return charset;
  76. # endif
  77. return "ASCII";
  78. }
  79. #endif
  80. /**
  81. * stringprep_convert:
  82. * @str: input zero-terminated string.
  83. * @to_codeset: name of destination character set.
  84. * @from_codeset: name of origin character set, as used by @str.
  85. *
  86. * Convert the string from one character set to another using the
  87. * system's iconv() function.
  88. *
  89. * Return value: Returns newly allocated zero-terminated string which
  90. * is @str transcoded into to_codeset.
  91. **/
  92. char *
  93. stringprep_convert (const char *str,
  94. const char *to_codeset, const char *from_codeset)
  95. {
  96. #if HAVE_ICONV
  97. return str_iconv (str, from_codeset, to_codeset);
  98. #else
  99. char *p;
  100. (void) to_codeset;
  101. (void) from_codeset;
  102. fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
  103. "convert data to UTF-8\n");
  104. p = malloc (strlen (str) + 1);
  105. if (!p)
  106. return NULL;
  107. return strcpy (p, str);
  108. #endif
  109. }
  110. /**
  111. * stringprep_locale_to_utf8:
  112. * @str: input zero terminated string.
  113. *
  114. * Convert string encoded in the locale's character set into UTF-8 by
  115. * using stringprep_convert().
  116. *
  117. * Return value: Returns newly allocated zero-terminated string which
  118. * is @str transcoded into UTF-8.
  119. **/
  120. char *
  121. stringprep_locale_to_utf8 (const char *str)
  122. {
  123. return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
  124. }
  125. /**
  126. * stringprep_utf8_to_locale:
  127. * @str: input zero terminated string.
  128. *
  129. * Convert string encoded in UTF-8 into the locale's character set by
  130. * using stringprep_convert().
  131. *
  132. * Return value: Returns newly allocated zero-terminated string which
  133. * is @str transcoded into the locale's character set.
  134. **/
  135. char *
  136. stringprep_utf8_to_locale (const char *str)
  137. {
  138. return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");
  139. }