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.
31 #ifdef G_PLATFORM_WIN32
40 g_convert_error_quark()
44 quark
= g_quark_from_static_string ("g_convert_error");
49 #if defined(USE_LIBICONV) && !defined (_LIBICONV_H)
50 #error libiconv in use but included iconv.h not from libiconv
52 #if !defined(USE_LIBICONV) && defined (_LIBICONV_H)
53 #error libiconv not in use but included iconv.h is from libiconv
57 g_iconv_open (const gchar
*to_codeset
,
58 const gchar
*from_codeset
)
60 iconv_t cd
= iconv_open (to_codeset
, from_codeset
);
66 g_iconv (GIConv converter
,
70 size_t *outbytes_left
)
72 iconv_t cd
= (iconv_t
)converter
;
74 return iconv (cd
, inbuf
, inbytes_left
, outbuf
, outbytes_left
);
78 g_iconv_close (GIConv converter
)
80 iconv_t cd
= (iconv_t
)converter
;
82 return iconv_close (cd
);
86 open_converter (const gchar
*to_codeset
,
87 const gchar
*from_codeset
,
90 GIConv cd
= g_iconv_open (to_codeset
, from_codeset
);
92 if (cd
== (iconv_t
) -1)
94 /* Something went wrong. */
96 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_NO_CONVERSION
,
97 _("Conversion from character set `%s' to `%s' is not supported"),
98 from_codeset
, to_codeset
);
100 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_FAILED
,
101 _("Could not open converter from `%s' to `%s': %s"),
102 from_codeset
, to_codeset
, strerror (errno
));
111 * @str: the string to convert
112 * @len: the length of the string
113 * @to_codeset: name of character set into which to convert @str
114 * @from_codeset: character set of @str.
115 * @bytes_read: location to store the number of bytes in the
116 * input string that were successfully converted, or %NULL.
117 * Even if the conversion was succesful, this may be
118 * less than len if there were partial characters
119 * at the end of the input. If the error
120 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
121 * stored will the byte fofset after the last valid
123 * @bytes_written: the stored in the output buffer (not including the
125 * @error: location to store the error occuring, or %NULL to ignore
126 * errors. Any of the errors in #GConvertError may occur.
128 * Convert a string from one character set to another.
130 * Return value: If the conversion was successful, a newly allocated
131 * NUL-terminated string, which must be freed with
132 * g_free. Otherwise %NULL and @error will be set.
135 g_convert (const gchar
*str
,
137 const gchar
*to_codeset
,
138 const gchar
*from_codeset
,
146 size_t inbytes_remaining
;
147 size_t outbytes_remaining
;
151 gboolean have_error
= FALSE
;
153 g_return_val_if_fail (str
!= NULL
, NULL
);
154 g_return_val_if_fail (to_codeset
!= NULL
, NULL
);
155 g_return_val_if_fail (from_codeset
!= NULL
, NULL
);
157 cd
= open_converter (to_codeset
, from_codeset
, error
);
159 if (cd
== (GIConv
) -1)
174 inbytes_remaining
= len
;
176 /* Due to a GLIBC bug, round outbuf_size up to a multiple of 4 */
177 /* + 1 for nul in case len == 1 */
178 outbuf_size
= ((len
+ 3) & ~3) + 1;
180 outbytes_remaining
= outbuf_size
- 1; /* -1 for nul */
181 outp
= dest
= g_malloc (outbuf_size
);
185 err
= g_iconv (cd
, (char **)&p
, &inbytes_remaining
, &outp
, &outbytes_remaining
);
187 if (err
== (size_t) -1)
192 /* Incomplete text, do not report an error */
196 size_t used
= outp
- dest
;
198 /* glibc's iconv can return E2BIG even if there is space
199 * remaining if an internal buffer is exhausted. The
200 * folllowing is a heuristic to catch this. The 16 is
203 if (used
+ 16 > outbuf_size
)
205 outbuf_size
= (outbuf_size
- 1) * 2 + 1;
206 dest
= g_realloc (dest
, outbuf_size
);
209 outbytes_remaining
= outbuf_size
- used
- 1; /* -1 for nul */
215 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
216 _("Invalid byte sequence in conversion input"));
220 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_FAILED
,
221 _("Error during conversion: %s"),
233 *bytes_read
= p
- str
;
236 if ((p
- str
) != len
)
240 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_PARTIAL_INPUT
,
241 _("Partial character sequence at end of input"));
248 *bytes_written
= outp
- dest
; /* Doesn't include '\0' */
260 * g_convert_with_fallback:
261 * @str: the string to convert
262 * @len: the length of the string
263 * @to_codeset: name of character set into which to convert @str
264 * @from_codeset: character set of @str.
265 * @fallback: UTF-8 string to use in place of character not
266 * present in the target encoding. (This must be
267 * in the target encoding), if %NULL, characters
268 * not in the target encoding will be represented
269 * as Unicode escapes \x{XXXX} or \x{XXXXXX}.
270 * @bytes_read: location to store the number of bytes in the
271 * input string that were successfully converted, or %NULL.
272 * Even if the conversion was succesful, this may be
273 * less than len if there were partial characters
274 * at the end of the input. If the error
275 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
276 * stored will the byte fofset after the last valid
278 * @bytes_written: the stored in the output buffer (not including the
280 * @error: location to store the error occuring, or %NULL to ignore
281 * errors. Any of the errors in #GConvertError may occur.
283 * Convert a string from one character set to another, possibly
284 * including fallback sequences for characters not representable
285 * in the output. Note that it is not guaranteed that the specification
286 * for the fallback sequences in @fallback will be honored. Some
287 * systems may do a approximate conversion from @from_codeset
288 * to @to_codeset in their iconv() functions, in which case GLib
289 * will simply return that approximate conversion.
291 * Return value: If the conversion was successful, a newly allocated
292 * NUL-terminated string, which must be freed with
293 * g_free. Otherwise %NULL and @error will be set.
296 g_convert_with_fallback (const gchar
*str
,
298 const gchar
*to_codeset
,
299 const gchar
*from_codeset
,
308 const gchar
*insert_str
= NULL
;
310 int inbytes_remaining
;
311 const gchar
*save_p
= NULL
;
312 size_t save_inbytes
= 0;
313 size_t outbytes_remaining
;
317 gboolean have_error
= FALSE
;
318 gboolean done
= FALSE
;
320 GError
*local_error
= NULL
;
322 g_return_val_if_fail (str
!= NULL
, NULL
);
323 g_return_val_if_fail (to_codeset
!= NULL
, NULL
);
324 g_return_val_if_fail (from_codeset
!= NULL
, NULL
);
329 /* Try an exact conversion; we only proceed if this fails
330 * due to an illegal sequence in the input string.
332 dest
= g_convert (str
, len
, to_codeset
, from_codeset
,
333 bytes_read
, bytes_written
, &local_error
);
337 if (!g_error_matches (local_error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
))
339 g_propagate_error (error
, local_error
);
343 g_error_free (local_error
);
347 /* No go; to proceed, we need a converter from "UTF-8" to
348 * to_codeset, and the string as UTF-8.
350 cd
= open_converter (to_codeset
, "UTF-8", error
);
351 if (cd
== (GIConv
) -1)
362 utf8
= g_convert (str
, len
, "UTF-8", from_codeset
,
363 bytes_read
, &inbytes_remaining
, error
);
367 /* Now the heart of the code. We loop through the UTF-8 string, and
368 * whenever we hit an offending character, we form fallback, convert
369 * the fallback to the target codeset, and then go back to
370 * converting the original string after finishing with the fallback.
372 * The variables save_p and save_inbytes store the input state
373 * for the original string while we are converting the fallback
376 /* Due to a GLIBC bug, round outbuf_size up to a multiple of 4 */
377 /* + 1 for nul in case len == 1 */
378 outbuf_size
= ((len
+ 3) & ~3) + 1;
379 outbytes_remaining
= outbuf_size
- 1; /* -1 for nul */
380 outp
= dest
= g_malloc (outbuf_size
);
382 while (!done
&& !have_error
)
384 size_t inbytes_tmp
= inbytes_remaining
;
385 err
= g_iconv (cd
, (char **)&p
, &inbytes_tmp
, &outp
, &outbytes_remaining
);
386 inbytes_remaining
= inbytes_tmp
;
388 if (err
== (size_t) -1)
393 g_assert_not_reached();
397 size_t used
= outp
- dest
;
399 /* glibc's iconv can return E2BIG even if there is space
400 * remaining if an internal buffer is exhausted. The
401 * folllowing is a heuristic to catch this. The 16 is
404 if (used
+ 16 > outbuf_size
)
406 outbuf_size
= (outbuf_size
- 1) * 2 + 1;
407 dest
= g_realloc (dest
, outbuf_size
);
410 outbytes_remaining
= outbuf_size
- used
- 1; /* -1 for nul */
418 /* Error converting fallback string - fatal
420 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
,
421 _("Cannot convert fallback '%s' to codeset '%s'"),
422 insert_str
, to_codeset
);
430 gunichar ch
= g_utf8_get_char (p
);
431 insert_str
= g_strdup_printf ("\\x{%0*X}",
432 (ch
< 0x10000) ? 4 : 6,
436 insert_str
= fallback
;
438 save_p
= g_utf8_next_char (p
);
439 save_inbytes
= inbytes_remaining
- (save_p
- p
);
441 inbytes_remaining
= strlen (p
);
445 g_set_error (error
, G_CONVERT_ERROR
, G_CONVERT_ERROR_FAILED
,
446 _("Error during conversion: %s"),
457 g_free ((gchar
*)insert_str
);
459 inbytes_remaining
= save_inbytes
;
474 *bytes_written
= outp
- str
; /* Doesn't include '\0' */
480 if (save_p
&& !fallback
)
481 g_free ((gchar
*)insert_str
);
497 * @opsysstring: a string in the encoding of the current locale
498 * @len: the length of the string, or -1 if the string is
500 * @bytes_read: location to store the number of bytes in the
501 * input string that were successfully converted, or %NULL.
502 * Even if the conversion was succesful, this may be
503 * less than len if there were partial characters
504 * at the end of the input. If the error
505 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
506 * stored will the byte fofset after the last valid
508 * @bytes_written: the stored in the output buffer (not including the
510 * @error: location to store the error occuring, or %NULL to ignore
511 * errors. Any of the errors in #GConvertError may occur.
513 * Converts a string which is in the encoding used for strings by
514 * the C runtime (usually the same as that used by the operating
515 * system) in the current locale into a UTF-8 string.
517 * Return value: The converted string, or %NULL on an error.
520 g_locale_to_utf8 (const gchar
*opsysstring
,
526 #ifdef G_PLATFORM_WIN32
528 gint i
, clen
, total_len
, wclen
, first
;
534 len
= strlen (opsysstring
);
536 wcs
= g_new (wchar_t, len
);
537 wclen
= MultiByteToWideChar (CP_ACP
, 0, opsysstring
, len
, wcs
, len
);
541 for (i
= 0; i
< wclen
; i
++)
549 else if (wc
< 0x10000)
551 else if (wc
< 0x200000)
553 else if (wc
< 0x4000000)
559 result
= g_malloc (total_len
+ 1);
563 for (i
= 0; i
< wclen
; i
++)
577 else if (wc
< 0x10000)
582 else if (wc
< 0x200000)
587 else if (wc
< 0x4000000)
601 case 6: bp
[5] = (wc
& 0x3f) | 0x80; wc
>>= 6; /* Fall through */
602 case 5: bp
[4] = (wc
& 0x3f) | 0x80; wc
>>= 6; /* Fall through */
603 case 4: bp
[3] = (wc
& 0x3f) | 0x80; wc
>>= 6; /* Fall through */
604 case 3: bp
[2] = (wc
& 0x3f) | 0x80; wc
>>= 6; /* Fall through */
605 case 2: bp
[1] = (wc
& 0x3f) | 0x80; wc
>>= 6; /* Fall through */
606 case 1: bp
[0] = wc
| first
;
618 *bytes_written
= total_len
;
622 #else /* !G_PLATFORM_WIN32 */
626 if (g_get_charset (&charset
))
627 return g_strdup (opsysstring
);
629 str
= g_convert (opsysstring
, len
,
630 "UTF-8", charset
, bytes_read
, bytes_written
, error
);
633 #endif /* !G_PLATFORM_WIN32 */
637 * g_locale_from_utf8:
638 * @utf8string: a UTF-8 encoded string
639 * @len: the length of the string, or -1 if the string is
641 * @bytes_read: location to store the number of bytes in the
642 * input string that were successfully converted, or %NULL.
643 * Even if the conversion was succesful, this may be
644 * less than len if there were partial characters
645 * at the end of the input. If the error
646 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
647 * stored will the byte fofset after the last valid
649 * @bytes_written: the stored in the output buffer (not including the
651 * @error: location to store the error occuring, or %NULL to ignore
652 * errors. Any of the errors in #GConvertError may occur.
654 * Converts a string from UTF-8 to the encoding used for strings by
655 * the C runtime (usually the same as that used by the operating
656 * system) in the current locale.
658 * Return value: The converted string, or %NULL on an error.
661 g_locale_from_utf8 (const gchar
*utf8string
,
667 #ifdef G_PLATFORM_WIN32
669 gint i
, mask
, clen
, mblen
;
676 len
= strlen (utf8string
);
678 /* First convert to wide chars */
679 cp
= (guchar
*) utf8string
;
682 wcs
= g_new (wchar_t, len
+ 1);
694 else if ((c
& 0xe0) == 0xc0)
699 else if ((c
& 0xf0) == 0xe0)
704 else if ((c
& 0xf8) == 0xf0)
709 else if ((c
& 0xfc) == 0xf8)
714 else if ((c
& 0xfc) == 0xfc)
731 *wcp
= (cp
[0] & mask
);
732 for (i
= 1; i
< clen
; i
++)
734 if ((cp
[i
] & 0xc0) != 0x80)
740 *wcp
|= (cp
[i
] & 0x3f);
753 /* n is the number of wide chars constructed */
755 /* Convert to a string in the current ANSI codepage */
757 result
= g_new (gchar
, 3 * n
+ 1);
758 mblen
= WideCharToMultiByte (CP_ACP
, 0, wcs
, n
, result
, 3*n
, NULL
, NULL
);
765 *bytes_written
= mblen
;
769 #else /* !G_PLATFORM_WIN32 */
771 gchar
*charset
, *str
;
773 if (g_get_charset (&charset
))
774 return g_strdup (utf8string
);
776 str
= g_convert (utf8string
, strlen (utf8string
),
777 charset
, "UTF-8", bytes_read
, bytes_written
, error
);
781 #endif /* !G_PLATFORM_WIN32 */
785 * g_filename_to_utf8:
786 * @opsysstring: a string in the encoding for filenames
787 * @len: the length of the string, or -1 if the string is
789 * @bytes_read: location to store the number of bytes in the
790 * input string that were successfully converted, or %NULL.
791 * Even if the conversion was succesful, this may be
792 * less than len if there were partial characters
793 * at the end of the input. If the error
794 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
795 * stored will the byte fofset after the last valid
797 * @bytes_written: the stored in the output buffer (not including the
799 * @error: location to store the error occuring, or %NULL to ignore
800 * errors. Any of the errors in #GConvertError may occur.
802 * Converts a string which is in the encoding used for filenames
803 * into a UTF-8 string.
805 * Return value: The converted string, or %NULL on an error.
808 g_filename_to_utf8 (const gchar
*opsysstring
,
814 #ifdef G_PLATFORM_WIN32
815 return g_locale_to_utf8 (opsysstring
, len
,
816 bytes_read
, bytes_written
,
818 #else /* !G_PLATFORM_WIN32 */
819 if (getenv ("G_BROKEN_FILENAMES"))
820 return g_locale_to_utf8 (opsysstring
, len
,
821 bytes_read
, bytes_written
,
824 if (bytes_read
|| bytes_written
)
826 gint len
= strlen (opsysstring
);
831 *bytes_written
= len
;
835 return g_strdup (opsysstring
);
837 return g_strndup (opsysstring
, len
);
838 #endif /* !G_PLATFORM_WIN32 */
842 * g_filename_from_utf8:
843 * @utf8string: a UTF-8 encoded string
844 * @len: the length of the string, or -1 if the string is
846 * @bytes_read: location to store the number of bytes in the
847 * input string that were successfully converted, or %NULL.
848 * Even if the conversion was succesful, this may be
849 * less than len if there were partial characters
850 * at the end of the input. If the error
851 * G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
852 * stored will the byte fofset after the last valid
854 * @bytes_written: the stored in the output buffer (not including the
856 * @error: location to store the error occuring, or %NULL to ignore
857 * errors. Any of the errors in #GConvertError may occur.
859 * Converts a string from UTF-8 to the encoding used for filenames.
861 * Return value: The converted string, or %NULL on an error.
864 g_filename_from_utf8 (const gchar
*utf8string
,
870 #ifdef G_PLATFORM_WIN32
871 return g_locale_from_utf8 (utf8string
, len
,
872 bytes_read
, bytes_written
,
874 #else /* !G_PLATFORM_WIN32 */
875 if (getenv ("G_BROKEN_FILENAMES"))
876 return g_locale_from_utf8 (utf8string
, len
,
877 bytes_read
, bytes_written
,
880 if (bytes_read
|| bytes_written
)
882 gint len
= strlen (utf8string
);
887 *bytes_written
= len
;
891 return g_strdup (utf8string
);
893 return g_strndup (utf8string
, len
);
894 #endif /* !G_PLATFORM_WIN32 */