1 /* toutf8.c Convert strings from system locale into UTF-8.
2 * Copyright (C) 2002, 2003, 2004 Simon Josefsson
4 * This file is part of GNU Libidn.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/param.h>
32 #include "stringprep.h"
36 # define LOCALE_WORKS 1
44 # include <langinfo.h>
49 # define stringprep_locale_charset() nl_langinfo (CODESET)
52 * stringprep_locale_charset:
54 * Find out current locale charset. The function respect the CHARSET
55 * environment variable, but typically uses nl_langinfo(CODESET) when
56 * it is supported. It fall back on "ASCII" if CHARSET isn't set and
57 * nl_langinfo isn't supported or return anything.
59 * Note that this function return the application's locale's preferred
60 * charset (or thread's locale's preffered charset, if your system
61 * support thread-specific locales). It does not return what the
62 * system may be using. Thus, if you receive data from external
63 * sources you cannot in general use this function to guess what
64 * charset it is encoded in. Use stringprep_convert from the external
65 * representation into the charset returned by this function, to have
66 * data in the locale encoding.
68 * Return value: Return the character set used by the current locale.
69 * It will never return NULL, but use "ASCII" as a fallback.
72 stringprep_locale_charset (void)
74 const char *charset
= getenv ("CHARSET"); /* flawfinder: ignore */
76 if (charset
&& *charset
)
80 charset
= nl_langinfo (CODESET
);
82 if (charset
&& *charset
)
92 * @str: input zero-terminated string.
93 * @to_codeset: name of destination character set.
94 * @from_codeset: name of origin character set, as used by @str.
96 * Convert the string from one character set to another using the
97 * system's iconv() function.
99 * Return value: Returns newly allocated zero-terminated string which
100 * is @str transcoded into to_codeset.
103 stringprep_convert (const char *str
,
104 const char *to_codeset
, const char *from_codeset
)
110 size_t inbytes_remaining
;
111 size_t outbytes_remaining
;
116 if (strcmp (to_codeset
, from_codeset
) == 0)
118 #if defined HAVE_STRDUP || defined _LIBC
122 p
= malloc (strlen (str
) + 1);
125 return strcpy (p
, str
);
129 cd
= iconv_open (to_codeset
, from_codeset
);
131 if (cd
== (iconv_t
) - 1)
134 p
= (ICONV_CONST
char *) str
;
136 inbytes_remaining
= strlen (p
);
137 /* Guess the maximum length the output string can have. */
138 outbuf_size
= (inbytes_remaining
+ 1) * MAX (7, MB_CUR_MAX
);
140 outp
= dest
= malloc (outbuf_size
);
143 outbytes_remaining
= outbuf_size
- 1; /* -1 for NUL */
147 err
= iconv (cd
, (ICONV_CONST
char **) &p
, &inbytes_remaining
,
148 &outp
, &outbytes_remaining
);
150 if (err
== (size_t) - 1)
155 /* Incomplete text, do not report an error */
160 size_t used
= outp
- dest
;
164 newdest
= realloc (dest
, outbuf_size
);
173 outbytes_remaining
= outbuf_size
- used
- 1; /* -1 for NUL */
206 #else /* HAVE_ICONV */
209 stringprep_locale_charset ()
215 stringprep_convert (const char *str
,
216 const char *to_codeset
, const char *from_codeset
)
219 fprintf (stderr
, "libidn: warning: libiconv not installed, cannot "
220 "convert data to UTF-8\n");
221 p
= malloc (strlen (str
) + 1);
228 #endif /* HAVE_ICONV */
231 * stringprep_locale_to_utf8:
232 * @str: input zero terminated string.
234 * Convert string encoded in the locale's character set into UTF-8 by
235 * using stringprep_convert().
237 * Return value: Returns newly allocated zero-terminated string which
238 * is @str transcoded into UTF-8.
241 stringprep_locale_to_utf8 (const char *str
)
243 return stringprep_convert (str
, "UTF-8", stringprep_locale_charset ());
247 * stringprep_utf8_to_locale:
248 * @str: input zero terminated string.
250 * Convert string encoded in UTF-8 into the locale's character set by
251 * using stringprep_convert().
253 * Return value: Returns newly allocated zero-terminated string which
254 * is @str transcoded into the locale's character set.
257 stringprep_utf8_to_locale (const char *str
)
259 return stringprep_convert (str
, stringprep_locale_charset (), "UTF-8");