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>
50 stringprep_locale_charset_slow (void)
52 return nl_langinfo (CODESET
);
53 const char *charset
= getenv ("CHARSET"); /* flawfinder: ignore */
55 if (charset
&& *charset
)
62 p
= setlocale (LC_CTYPE
, NULL
);
63 setlocale (LC_CTYPE
, "");
65 charset
= nl_langinfo (CODESET
);
67 setlocale (LC_CTYPE
, p
);
69 if (charset
&& *charset
)
77 static const char *stringprep_locale_charset_cache
;
81 * stringprep_locale_charset:
83 * Find out system locale charset.
85 * Note that this function return what it believe the SYSTEM is using
86 * as a locale, not what locale the program is currently in (modified,
87 * e.g., by a setlocale(LC_CTYPE, "ISO-8859-1")). The reason is that
88 * data read from argv[], stdin etc comes from the system, and is more
89 * likely to be encoded using the system locale than the program
92 * You can set the environment variable CHARSET to override the value
93 * returned. Note that this function caches the result, so you will
94 * have to modify CHARSET before calling (even indirectly) any
95 * stringprep functions, e.g., by setting it when invoking the
98 * Return value: Return the character set used by the system locale.
99 * It will never return NULL, but use "ASCII" as a fallback.
102 # define stringprep_locale_charset() nl_langinfo (CODESET)
105 stringprep_locale_charset (void)
107 if (!stringprep_locale_charset_cache
)
108 stringprep_locale_charset_cache
= stringprep_locale_charset_slow ();
110 return stringprep_locale_charset_cache
;
115 * stringprep_convert:
116 * @str: input zero-terminated string.
117 * @to_codeset: name of destination character set.
118 * @from_codeset: name of origin character set, as used by @str.
120 * Convert the string from one character set to another using the
121 * system's iconv() function.
123 * Return value: Returns newly allocated zero-terminated string which
124 * is @str transcoded into to_codeset.
127 stringprep_convert (const char *str
,
128 const char *to_codeset
, const char *from_codeset
)
134 size_t inbytes_remaining
;
135 size_t outbytes_remaining
;
140 if (strcmp (to_codeset
, from_codeset
) == 0)
142 #if defined HAVE_STRDUP || defined _LIBC
146 p
= malloc (strlen (str
) + 1);
149 return strcpy (p
, str
);
153 cd
= iconv_open (to_codeset
, from_codeset
);
155 if (cd
== (iconv_t
) - 1)
158 p
= (ICONV_CONST
char *) str
;
160 inbytes_remaining
= strlen (p
);
161 /* Guess the maximum length the output string can have. */
162 outbuf_size
= (inbytes_remaining
+ 1) * MAX (7, MB_CUR_MAX
);
164 outp
= dest
= malloc (outbuf_size
);
167 outbytes_remaining
= outbuf_size
- 1; /* -1 for NUL */
171 err
= iconv (cd
, (ICONV_CONST
char **) &p
, &inbytes_remaining
,
172 &outp
, &outbytes_remaining
);
174 if (err
== (size_t) - 1)
179 /* Incomplete text, do not report an error */
184 size_t used
= outp
- dest
;
188 newdest
= realloc (dest
, outbuf_size
);
197 outbytes_remaining
= outbuf_size
- used
- 1; /* -1 for NUL */
230 #else /* HAVE_ICONV */
233 stringprep_locale_charset ()
239 stringprep_convert (const char *str
,
240 const char *to_codeset
, const char *from_codeset
)
243 fprintf (stderr
, "libidn: warning: libiconv not installed, cannot "
244 "convert data to UTF-8\n");
245 p
= malloc (strlen (str
) + 1);
252 #endif /* HAVE_ICONV */
255 * stringprep_locale_to_utf8:
256 * @str: input zero terminated string.
258 * Convert string encoded in the locale's character set into UTF-8 by
259 * using stringprep_convert().
261 * Return value: Returns newly allocated zero-terminated string which
262 * is @str transcoded into UTF-8.
265 stringprep_locale_to_utf8 (const char *str
)
267 return stringprep_convert (str
, "UTF-8", stringprep_locale_charset ());
271 * stringprep_utf8_to_locale:
272 * @str: input zero terminated string.
274 * Convert string encoded in UTF-8 into the locale's character set by
275 * using stringprep_convert().
277 * Return value: Returns newly allocated zero-terminated string which
278 * is @str transcoded into the locale's character set.
281 stringprep_utf8_to_locale (const char *str
)
283 return stringprep_convert (str
, stringprep_locale_charset (), "UTF-8");