.
[libidn.git] / lib / toutf8.c
blobe1ff05a7374bd094f5528cf6d5f16cc9c1211237
1 /* toutf8.c Convert strings from system locale into UTF-8.
2 * Copyright (C) 2002, 2003 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
22 #include "internal.h"
24 #ifdef HAVE_ICONV
26 #include <iconv.h>
28 #if LOCALE_WORKS
29 #include <langinfo.h>
30 #include <locale.h>
31 #endif
33 static const char *
34 stringprep_locale_charset_slow (void)
36 const char *charset = getenv ("CHARSET"); /* flawfinder: ignore */
38 if (charset && *charset)
39 return charset;
41 #if LOCALE_WORKS
43 char *p;
45 p = setlocale (LC_CTYPE, NULL);
46 setlocale (LC_CTYPE, "");
48 charset = nl_langinfo (CODESET);
50 setlocale (LC_CTYPE, p);
52 if (charset && *charset)
53 return charset;
55 #endif
57 return "ASCII";
60 static const char *stringprep_locale_charset_cache = NULL;
62 /**
63 * stringprep_locale_charset:
65 * Find out system locale charset.
67 * Note that this function return what it believe the SYSTEM is using
68 * as a locale, not what locale the program is currently in (modified,
69 * e.g., by a setlocale(LC_CTYPE, "ISO-8859-1")). The reason is that
70 * data read from argv[], stdin etc comes from the system, and is more
71 * likely to be encoded using the system locale than the program
72 * locale.
74 * You can set the environment variable CHARSET to override the value
75 * returned. Note that this function caches the result, so you will
76 * have to modify CHARSET before calling (even indirectly) any
77 * stringprep functions, e.g., by setting it when invoking the
78 * application.
80 * Return value: Return the character set used by the system locale.
81 * It will never return NULL, but use "ASCII" as a fallback.
82 **/
83 const char *
84 stringprep_locale_charset (void)
86 if (!stringprep_locale_charset_cache)
87 stringprep_locale_charset_cache = stringprep_locale_charset_slow ();
89 return stringprep_locale_charset_cache;
92 /**
93 * stringprep_convert:
94 * @str: input zero-terminated string.
95 * @to_codeset: name of destination character set.
96 * @from_codeset: name of origin character set, as used by @str.
98 * Convert the string from one character set to another using the
99 * system's iconv() function.
101 * Return value: Returns newly allocated zero-terminated string which
102 * is @str transcoded into to_codeset.
104 char *
105 stringprep_convert (const char *str,
106 const char *to_codeset, const char *from_codeset)
108 iconv_t cd;
109 char *dest;
110 char *outp;
111 char *p, *startp;
112 size_t inbytes_remaining;
113 size_t outbytes_remaining;
114 size_t err;
115 size_t outbuf_size;
116 int have_error = 0;
117 int len;
119 if (strcmp (to_codeset, from_codeset) == 0)
121 char *p;
122 p = malloc (strlen (str) + 1);
123 if (!p)
124 return NULL;
125 strcpy (p, str);
126 return p;
129 cd = iconv_open (to_codeset, from_codeset);
131 if (cd == (iconv_t) - 1)
132 return NULL;
134 p = (char *) malloc (strlen (str) + 1);
135 strcpy (p, str);
136 if (p == NULL)
137 return NULL;
138 len = strlen (p);
139 startp = p;
140 inbytes_remaining = len;
141 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
143 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
144 outp = dest = malloc (outbuf_size);
146 again:
148 err = iconv (cd, (ICONV_CONST char **) &p, &inbytes_remaining,
149 &outp, &outbytes_remaining);
151 if (err == (size_t) - 1)
153 switch (errno)
155 case EINVAL:
156 /* Incomplete text, do not report an error */
157 break;
159 case E2BIG:
161 size_t used = outp - dest;
163 outbuf_size *= 2;
164 dest = realloc (dest, outbuf_size);
166 outp = dest + used;
167 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
169 goto again;
171 break;
173 case EILSEQ:
174 have_error = 1;
175 break;
177 default:
178 have_error = 1;
179 break;
183 *outp = '\0';
185 if ((p - startp) != len)
186 have_error = 1;
189 free (startp);
191 iconv_close (cd);
193 if (have_error)
195 free (dest);
196 dest = NULL;
199 return dest;
202 #else
204 const char *
205 stringprep_locale_charset ()
207 return "ASCII";
210 char *
211 stringprep_convert (const char *str,
212 const char *to_codeset, const char *from_codeset)
214 char *p;
215 fprintf (stderr, "libidn: warning: libiconv not installed, cannot "
216 "convert data to UTF-8\n");
217 p = malloc (strlen (str) + 1);
218 if (!p)
219 return NULL;
220 strcpy (p, str);
221 return p;
224 #endif
227 * stringprep_locale_to_utf8:
228 * @str: input zero terminated string.
230 * Convert string encoded in the locale's character set into UTF-8 by
231 * using stringprep_convert().
233 * Return value: Returns newly allocated zero-terminated string which
234 * is @str transcoded into UTF-8.
236 char *
237 stringprep_locale_to_utf8 (const char *str)
239 return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
243 * stringprep_utf8_to_locale:
244 * @str: input zero terminated string.
246 * Convert string encoded in UTF-8 into the locale's character set by
247 * using stringprep_convert().
249 * Return value: Returns newly allocated zero-terminated string which
250 * is @str transcoded into the locale's character set.
252 char *
253 stringprep_utf8_to_locale (const char *str)
255 return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");