Ignore idn-int.h.
[libidn.git] / toutf8.c
blobad89c68cd52c60c42100a75d59b64572095873d3
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 * Return value: Return the character set used by the system locale.
66 * It will never return NULL, but use "ASCII" as a fallback.
67 **/
68 const char *
69 stringprep_locale_charset (void)
71 if (!stringprep_locale_charset_cache)
72 stringprep_locale_charset_cache = stringprep_locale_charset_slow ();
74 return stringprep_locale_charset_cache;
77 /**
78 * stringprep_convert:
79 * @str: input zero-terminated string.
80 * @to_codeset: name of destination character set.
81 * @from_codeset: name of origin character set, as used by @str.
83 * Convert the string from one character set to another using the
84 * system's iconv() function.
86 * Return value: Returns newly allocated zero-terminated string which
87 * is @str transcoded into to_codeset.
88 **/
89 char *
90 stringprep_convert (const char *str,
91 const char *to_codeset, const char *from_codeset)
93 iconv_t cd;
94 char *dest;
95 char *outp;
96 char *p, *startp;
97 size_t inbytes_remaining;
98 size_t outbytes_remaining;
99 size_t err;
100 size_t outbuf_size;
101 int have_error = 0;
102 int len;
104 if (strcmp (to_codeset, from_codeset) == 0)
105 return (char *) strdup (str);
107 cd = iconv_open (to_codeset, from_codeset);
109 if (cd == (iconv_t) - 1)
110 return NULL;
112 p = (char *) strdup (str);
113 if (p == NULL)
114 return NULL;
115 len = strlen (p);
116 startp = p;
117 inbytes_remaining = len;
118 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
120 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
121 outp = dest = malloc (outbuf_size);
123 again:
125 err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
127 if (err == (size_t) - 1)
129 switch (errno)
131 case EINVAL:
132 /* Incomplete text, do not report an error */
133 break;
135 case E2BIG:
137 size_t used = outp - dest;
139 outbuf_size *= 2;
140 dest = realloc (dest, outbuf_size);
142 outp = dest + used;
143 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
145 goto again;
147 break;
149 case EILSEQ:
150 have_error = 1;
151 break;
153 default:
154 have_error = 1;
155 break;
159 *outp = '\0';
161 if ((p - startp) != len)
162 have_error = 1;
165 free (startp);
167 iconv_close (cd);
169 if (have_error)
171 free (dest);
172 dest = NULL;
175 return dest;
178 #else
180 const char *
181 stringprep_locale_charset ()
183 return "ASCII";
186 char *
187 stringprep_convert (const char *str,
188 const char *to_codeset, const char *from_codeset)
190 fprintf (stderr,
191 "warning: cannot convert data to UTF-8, returning source\n");
192 fprintf (stderr, "warning: this indicate a badly installed GNU Libidn\n");
193 return strdup (str);
196 #endif
199 * stringprep_locale_to_utf8:
200 * @str: input zero terminated string.
202 * Convert string encoded in the locale's character set into UTF-8 by
203 * using stringprep_convert().
205 * Return value: Returns newly allocated zero-terminated string which
206 * is @str transcoded into UTF-8.
208 char *
209 stringprep_locale_to_utf8 (const char *str)
211 return stringprep_convert (str, "UTF-8", stringprep_locale_charset ());
215 * stringprep_utf8_to_locale:
216 * @str: input zero terminated string.
218 * Convert string encoded in UTF-8 into the locale's character set by
219 * using stringprep_convert().
221 * Return value: Returns newly allocated zero-terminated string which
222 * is @str transcoded into the locale's character set.
224 char *
225 stringprep_utf8_to_locale (const char *str)
227 return stringprep_convert (str, stringprep_locale_charset (), "UTF-8");