1 /* GLIB - Library of useful routines for C programming
3 * gconvert.c: Convert between character sets using iconv
4 * Copyright Red Hat Inc., 2000
5 * Authors: Havoc Pennington <hp@redhat.com>, Owen Taylor <otaylor@redhat.com
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
30 g_convert_error_quark()
34 quark = g_quark_from_static_string ("g_convert_error");
39 open_converter (const gchar *to_codeset,
40 const gchar *from_codeset,
43 iconv_t cd = iconv_open (to_codeset, from_codeset);
45 if (cd == (iconv_t) -1)
47 /* Something went wrong. */
49 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
50 "Conversion from character set `%s' to `%s' is not supported",
51 from_codeset, to_codeset);
53 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_OTHER,
54 "Could not open converter from `%s' to `%s': %s",
55 from_codeset, to_codeset, strerror (errno));
64 * @str: the string to convert
65 * @len: the length of the string
66 * @to_codeset: name of character set into which to convert @str
67 * @from_codeset: character set of @str.
68 * @bytes_read: location to store the number of bytes in the
69 * input string that were successfully converted, or %NULL.
70 * Even if the conversion was succesful, this may be
71 * less than len if there were partial characters
72 * at the end of the input. If the error
73 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
74 * stored will the byte fofset after the last valid
76 * @error: location to store the error occuring, or %NULL to ignore
77 * errors. Any of the errors in #GConvertError may occur.
79 * Convert a string from one character set to another.
81 * Return value: If the conversion was successful, a newly allocated
82 * NUL-terminated string, which must be freed with
83 * g_free. Otherwise %NULL and @error will be set.
86 g_convert (const gchar *str,
88 const gchar *to_codeset,
89 const gchar *from_codeset,
97 size_t inbytes_remaining;
98 size_t outbytes_remaining;
102 gboolean have_error = FALSE;
104 g_return_val_if_fail (str != NULL, NULL);
105 g_return_val_if_fail (to_codeset != NULL, NULL);
106 g_return_val_if_fail (from_codeset != NULL, NULL);
108 cd = open_converter (to_codeset, from_codeset, error);
110 if (cd == (iconv_t) -1)
125 inbytes_remaining = len;
126 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
127 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
128 outp = dest = g_malloc (outbuf_size);
132 err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
134 if (err == (size_t) -1)
139 /* Incomplete text, do not report an error */
143 size_t used = outp - dest;
145 dest = g_realloc (dest, outbuf_size);
148 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
153 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
154 "Invalid byte sequence in conversion input");
158 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_OTHER,
159 "Error during conversion: %s",
171 *bytes_read = p - str;
174 *bytes_written = outp - dest; /* Doesn't include '\0' */
186 * g_convert_with_fallback:
187 * @str: the string to convert
188 * @len: the length of the string
189 * @to_codeset: name of character set into which to convert @str
190 * @from_codeset: character set of @str.
191 * @fallback: UTF-8 string to use in place of character not
192 * present in the target encoding. (This must be
193 * in the target encoding), if %NULL, characters
194 * not in the target encoding will be represented
195 * as Unicode escapes \x{XXXX} or \x{XXXXXX}.
196 * @bytes_read: location to store the number of bytes in the
197 * input string that were successfully converted, or %NULL.
198 * Even if the conversion was succesful, this may be
199 * less than len if there were partial characters
200 * at the end of the input. If the error
201 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
202 * stored will the byte fofset after the last valid
204 * @error: location to store the error occuring, or %NULL to ignore
205 * errors. Any of the errors in #GConvertError may occur.
207 * Convert a string from one character set to another, possibly
208 * including fallback sequences for characters not representable
209 * in the output. Note that it is not guaranteed that the specification
210 * for the fallback sequences in @fallback will be honored. Some
211 * systems may do a approximate conversion from @from_codeset
212 * to @to_codeset in their iconv() functions, in which case GLib
213 * will simply return that approximate conversion.
215 * Return value: If the conversion was successful, a newly allocated
216 * NUL-terminated string, which must be freed with
217 * g_free. Otherwise %NULL and @error will be set.
220 g_convert_with_fallback (const gchar *str,
222 const gchar *to_codeset,
223 const gchar *from_codeset,
232 const gchar *insert_str = NULL;
234 size_t inbytes_remaining;
235 const gchar *save_p = NULL;
236 size_t save_inbytes = 0;
237 size_t outbytes_remaining;
241 gboolean have_error = FALSE;
242 gboolean done = FALSE;
244 GError *local_error = NULL;
246 g_return_val_if_fail (str != NULL, NULL);
247 g_return_val_if_fail (to_codeset != NULL, NULL);
248 g_return_val_if_fail (from_codeset != NULL, NULL);
253 /* Try an exact conversion; we only proceed if this fails
254 * due to an illegal sequence in the input string.
256 dest = g_convert (str, len, to_codeset, from_codeset,
257 bytes_read, bytes_written, &local_error);
261 if (!g_error_matches (local_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
263 g_propagate_error (error, local_error);
267 g_error_free (local_error);
269 /* No go; to proceed, we need a converter from "UTF-8" to
270 * to_codeset, and the string as UTF-8.
272 cd = open_converter (to_codeset, "UTF-8", error);
273 if (cd == (iconv_t) -1)
284 utf8 = g_convert (str, len, "UTF-8", from_codeset,
285 bytes_read, &inbytes_remaining, error);
289 /* Now the heart of the code. We loop through the UTF-8 string, and
290 * whenever we hit an offending character, we form fallback, convert
291 * the fallback to the target codeset, and then go back to
292 * converting the original string after finishing with the fallback.
294 * The variables save_p and save_inbytes store the input state
295 * for the original string while we are converting the fallback
298 outbuf_size = len + 1; /* + 1 for nul in case len == 1 */
299 outbytes_remaining = outbuf_size - 1; /* -1 for nul */
300 outp = dest = g_malloc (outbuf_size);
302 while (!done && !have_error)
304 err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
306 if (err == (size_t) -1)
311 g_assert_not_reached();
315 size_t used = outp - dest;
317 dest = g_realloc (dest, outbuf_size);
320 outbytes_remaining = outbuf_size - used - 1; /* -1 for nul */
327 /* Error converting fallback string - fatal
329 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
330 "Cannot convert fallback '%s' to codeset '%s'",
331 insert_str, to_codeset);
339 gunichar ch = g_utf8_get_char (p);
340 insert_str = g_strdup_printf ("\\x{%0*X}",
341 (ch < 0x10000) ? 4 : 6,
345 insert_str = fallback;
347 save_p = g_utf8_next_char (p);
348 save_inbytes = inbytes_remaining - (save_p - p);
350 inbytes_remaining = strlen (p);
354 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_OTHER,
355 "Error during conversion: %s",
366 g_free ((gchar *)insert_str);
368 inbytes_remaining = save_inbytes;
383 *bytes_written = outp - str; /* Doesn't include '\0' */
389 if (save_p && !fallback)
390 g_free ((gchar *)insert_str);