toutf8.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* toutf8.c --- Convert strings from system locale into UTF-8.
  2. * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
  3. *
  4. * This file is part of GNU Libidn.
  5. *
  6. * GNU Libidn is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * GNU Libidn is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with GNU Libidn; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include "idn_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. #if HAVE_LOCALE_H
  40. # include <locale.h>
  41. #endif
  42. #if HAVE_LANGINFO_CODESET
  43. # include <langinfo.h>
  44. #endif
  45. #ifdef _LIBC
  46. # define stringprep_locale_charset() nl_langinfo (CODESET)
  47. #else
  48. /**
  49. * stringprep_locale_charset - return charset used in current locale
  50. *
  51. * Find out current locale charset. The function respect the CHARSET
  52. * environment variable, but typically uses nl_langinfo(CODESET) when
  53. * it is supported. It fall back on "ASCII" if CHARSET isn't set and
  54. * nl_langinfo isn't supported or return anything.
  55. *
  56. * Note that this function return the application's locale's preferred
  57. * charset (or thread's locale's preffered charset, if your system
  58. * support thread-specific locales). It does not return what the
  59. * system may be using. Thus, if you receive data from external
  60. * sources you cannot in general use this function to guess what
  61. * charset it is encoded in. Use stringprep_convert from the external
  62. * representation into the charset returned by this function, to have
  63. * data in the locale encoding.
  64. *
  65. * Return value: Return the character set used by the current locale.
  66. * It will never return NULL, but use "ASCII" as a fallback.
  67. **/
  68. const char *
  69. stringprep_locale_charset (void)
  70. {
  71. const char *charset = getenv ("CHARSET"); /* flawfinder: ignore */
  72. if (charset && *charset)
  73. return charset;
  74. # ifdef HAVE_LANGINFO_CODESET
  75. charset = nl_langinfo (CODESET);
  76. if (charset && *charset)
  77. return charset;
  78. # endif
  79. return "ASCII";
  80. }
  81. #endif
  82. /**
  83. * stringprep_convert - encode string using new character set
  84. * @str: input zero-terminated string.
  85. * @to_codeset: name of destination character set.
  86. * @from_codeset: name of origin character set, as used by @str.
  87. *
  88. * Convert the string from one character set to another using the
  89. * system's iconv() function.
  90. *
  91. * Return value: Returns newly allocated zero-terminated string which
  92. * is @str transcoded into to_codeset.
  93. **/
  94. char *
  95. stringprep_convert (const char *str,
  96. const char *to_codeset, const char *from_codeset)
  97. {
  98. #if HAVE_ICONV
  99. return str_iconv (str, from_codeset, to_codeset);
  100. #else
  101. char *p;
  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 - convert locale encoded string to UTF-8
  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 - encode UTF-8 string to locale encoding
  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. }