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, see <http://www.gnu.org/licenses/>.
22 #include "glibconfig.h"
33 #include "win_iconv.c"
36 #ifdef G_PLATFORM_WIN32
44 #include "gcharsetprivate.h"
46 #include "gstrfuncs.h"
47 #include "gtestutils.h"
50 #include "gfileutils.h"
54 #if defined(USE_LIBICONV_GNU) && !defined (_LIBICONV_H)
55 #error GNU libiconv in use but included iconv.h not from libiconv
57 #if !defined(USE_LIBICONV_GNU) && defined (_LIBICONV_H) \
58 && !defined (__APPLE_CC__) && !defined (__LP_64__)
59 #error GNU libiconv not in use but included iconv.h is from libiconv
65 * @title: Character Set Conversion
66 * @short_description: convert strings between different character sets
68 * The g_convert() family of function wraps the functionality of iconv().
69 * In addition to pure character set conversions, GLib has functions to
70 * deal with the extra complications of encodings for file names.
72 * ## File Name Encodings
74 * Historically, UNIX has not had a defined encoding for file names:
75 * a file name is valid as long as it does not have path separators
76 * in it ("/"). However, displaying file names may require conversion:
77 * from the character set in which they were created, to the character
78 * set in which the application operates. Consider the Spanish file name
79 * "Presentación.sxi". If the application which created it uses
80 * ISO-8859-1 for its encoding,
82 * Character: P r e s e n t a c i ó n . s x i
83 * Hex code: 50 72 65 73 65 6e 74 61 63 69 f3 6e 2e 73 78 69
85 * However, if the application use UTF-8, the actual file name on
86 * disk would look like this:
87 * <programlisting id="filename-utf-8">
88 * Character: P r e s e n t a c i ó n . s x i
89 * Hex code: 50 72 65 73 65 6e 74 61 63 69 c3 b3 6e 2e 73 78 69
91 * Glib uses UTF-8 for its strings, and GUI toolkits like GTK+ that use
92 * Glib do the same thing. If you get a file name from the file system,
93 * for example, from readdir() or from g_dir_read_name(), and you wish
94 * to display the file name to the user, you will need to convert it
95 * into UTF-8. The opposite case is when the user types the name of a
96 * file he wishes to save: the toolkit will give you that string in
97 * UTF-8 encoding, and you will need to convert it to the character
98 * set used for file names before you can create the file with open()
101 * By default, Glib assumes that file names on disk are in UTF-8
102 * encoding. This is a valid assumption for file systems which
103 * were created relatively recently: most applications use UTF-8
104 * encoding for their strings, and that is also what they use for
105 * the file names they create. However, older file systems may
106 * still contain file names created in "older" encodings, such as
107 * ISO-8859-1. In this case, for compatibility reasons, you may
108 * want to instruct Glib to use that particular encoding for file
109 * names rather than UTF-8. You can do this by specifying the
110 * encoding for file names in the <link
111 * linkend="G_FILENAME_ENCODING"><envar>G_FILENAME_ENCODING</envar></link>
112 * environment variable. For example, if your installation uses
113 * ISO-8859-1 for file names, you can put this in your
114 * <filename>~/.profile</filename>:
116 * export G_FILENAME_ENCODING=ISO-8859-1
118 * Glib provides the functions g_filename_to_utf8() and
119 * g_filename_from_utf8() to perform the necessary conversions.
120 * These functions convert file names from the encoding specified
121 * in <envar>G_FILENAME_ENCODING</envar> to UTF-8 and vice-versa.
122 * <xref linkend="file-name-encodings-diagram"/> illustrates how
123 * these functions are used to convert between UTF-8 and the
124 * encoding for file names in the file system.
126 * <figure id="file-name-encodings-diagram">
127 * <title>Conversion between File Name Encodings</title>
128 * <graphic fileref="file-name-encodings.png" format="PNG"/>
131 * ## Checklist for Application Writers
133 * This section is a practical summary of the detailed
135 * things to do to make sure your applications process file
136 * name encodings correctly.
138 * 1. If you get a file name from the file system from a function
139 * such as readdir() or gtk_file_chooser_get_filename(), you do
140 * not need to do any conversion to pass that file name to
141 * functions like open(), rename(), or fopen() -- those are "raw"
142 * file names which the file system understands.
144 * 2. If you need to display a file name, convert it to UTF-8 first
145 * by using g_filename_to_utf8(). If conversion fails, display a
146 * string like "Unknown file name". Do not convert this string back
147 * into the encoding used for file names if you wish to pass it to
148 * the file system; use the original file name instead.
150 * For example, the document window of a word processor could display
151 * "Unknown file name" in its title bar but still let the user save
152 * the file, as it would keep the raw file name internally. This can
153 * happen if the user has not set the <envar>G_FILENAME_ENCODING</envar>
154 * environment variable even though he has files whose names are not
157 * 3. If your user interface lets the user type a file name for saving
158 * or renaming, convert it to the encoding used for file names in
159 * the file system by using g_filename_from_utf8(). Pass the converted
160 * file name to functions like fopen(). If conversion fails, ask the
161 * user to enter a different file name. This can happen if the user
162 * types Japanese characters when <envar>G_FILENAME_ENCODING</envar>
163 * is set to <literal>ISO-8859-1</literal>, for example.
166 /* We try to terminate strings in unknown charsets with this many zero bytes
167 * to ensure that multibyte strings really are nul-terminated when we return
168 * them from g_convert() and friends.
170 #define NUL_TERMINATOR_LENGTH 4
172 G_DEFINE_QUARK (g_convert_error, g_convert_error)
175 try_conversion (const char *to_codeset,
176 const char *from_codeset,
179 *cd = iconv_open (to_codeset, from_codeset);
181 if (*cd == (iconv_t)-1 && errno == EINVAL)
188 try_to_aliases (const char **to_aliases,
189 const char *from_codeset,
194 const char **p = to_aliases;
197 if (try_conversion (*p, from_codeset, cd))
209 * @to_codeset: destination codeset
210 * @from_codeset: source codeset
212 * Same as the standard UNIX routine iconv_open(), but
213 * may be implemented via libiconv on UNIX flavors that lack
214 * a native implementation.
216 * GLib provides g_convert() and g_locale_to_utf8() which are likely
217 * more convenient than the raw iconv wrappers.
219 * Return value: a "conversion descriptor", or (GIConv)-1 if
220 * opening the converter failed.
223 g_iconv_open (const gchar *to_codeset,
224 const gchar *from_codeset)
228 if (!try_conversion (to_codeset, from_codeset, &cd))
230 const char **to_aliases = _g_charset_get_aliases (to_codeset);
231 const char **from_aliases = _g_charset_get_aliases (from_codeset);
235 const char **p = from_aliases;
238 if (try_conversion (to_codeset, *p, &cd))
241 if (try_to_aliases (to_aliases, *p, &cd))
248 if (try_to_aliases (to_aliases, from_codeset, &cd))
253 return (cd == (iconv_t)-1) ? (GIConv)-1 : (GIConv)cd;
258 * @converter: conversion descriptor from g_iconv_open()
259 * @inbuf: bytes to convert
260 * @inbytes_left: inout parameter, bytes remaining to convert in @inbuf
261 * @outbuf: converted output bytes
262 * @outbytes_left: inout parameter, bytes available to fill in @outbuf
264 * Same as the standard UNIX routine iconv(), but
265 * may be implemented via libiconv on UNIX flavors that lack
266 * a native implementation.
268 * GLib provides g_convert() and g_locale_to_utf8() which are likely
269 * more convenient than the raw iconv wrappers.
271 * Return value: count of non-reversible conversions, or -1 on error
274 g_iconv (GIConv converter,
278 gsize *outbytes_left)
280 iconv_t cd = (iconv_t)converter;
282 return iconv (cd, inbuf, inbytes_left, outbuf, outbytes_left);
287 * @converter: a conversion descriptor from g_iconv_open()
289 * Same as the standard UNIX routine iconv_close(), but
290 * may be implemented via libiconv on UNIX flavors that lack
291 * a native implementation. Should be called to clean up
292 * the conversion descriptor from g_iconv_open() when
293 * you are done converting things.
295 * GLib provides g_convert() and g_locale_to_utf8() which are likely
296 * more convenient than the raw iconv wrappers.
298 * Return value: -1 on error, 0 on success
301 g_iconv_close (GIConv converter)
303 iconv_t cd = (iconv_t)converter;
305 return iconv_close (cd);
309 open_converter (const gchar *to_codeset,
310 const gchar *from_codeset,
315 cd = g_iconv_open (to_codeset, from_codeset);
317 if (cd == (GIConv) -1)
319 /* Something went wrong. */
323 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
324 _("Conversion from character set '%s' to '%s' is not supported"),
325 from_codeset, to_codeset);
327 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
328 _("Could not open converter from '%s' to '%s'"),
329 from_codeset, to_codeset);
337 close_converter (GIConv cd)
339 if (cd == (GIConv) -1)
342 return g_iconv_close (cd);
346 * g_convert_with_iconv:
347 * @str: the string to convert
348 * @len: the length of the string, or -1 if the string is
349 * nul-terminated<footnoteref linkend="nul-unsafe"/>.
350 * @converter: conversion descriptor from g_iconv_open()
351 * @bytes_read: location to store the number of bytes in the
352 * input string that were successfully converted, or %NULL.
353 * Even if the conversion was successful, this may be
354 * less than @len if there were partial characters
355 * at the end of the input. If the error
356 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
357 * stored will the byte offset after the last valid
359 * @bytes_written: the number of bytes stored in the output buffer (not
360 * including the terminating nul).
361 * @error: location to store the error occurring, or %NULL to ignore
362 * errors. Any of the errors in #GConvertError may occur.
364 * Converts a string from one character set to another.
366 * Note that you should use g_iconv() for streaming
367 * conversions<footnote id="streaming-state">
369 * Despite the fact that @byes_read can return information about partial
370 * characters, the <literal>g_convert_...</literal> functions
371 * are not generally suitable for streaming. If the underlying converter
372 * being used maintains internal state, then this won't be preserved
373 * across successive calls to g_convert(), g_convert_with_iconv() or
374 * g_convert_with_fallback(). (An example of this is the GNU C converter
375 * for CP1255 which does not emit a base character until it knows that
376 * the next character is not a mark that could combine with the base
381 * Return value: If the conversion was successful, a newly allocated
382 * nul-terminated string, which must be freed with
383 * g_free(). Otherwise %NULL and @error will be set.
386 g_convert_with_iconv (const gchar *str,
390 gsize *bytes_written,
396 gsize inbytes_remaining;
397 gsize outbytes_remaining;
400 gboolean have_error = FALSE;
401 gboolean done = FALSE;
402 gboolean reset = FALSE;
404 g_return_val_if_fail (converter != (GIConv) -1, NULL);
410 inbytes_remaining = len;
411 outbuf_size = len + NUL_TERMINATOR_LENGTH;
413 outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
414 outp = dest = g_malloc (outbuf_size);
416 while (!done && !have_error)
419 err = g_iconv (converter, NULL, &inbytes_remaining, &outp, &outbytes_remaining);
421 err = g_iconv (converter, (char **)&p, &inbytes_remaining, &outp, &outbytes_remaining);
423 if (err == (gsize) -1)
428 /* Incomplete text, do not report an error */
433 gsize used = outp - dest;
436 dest = g_realloc (dest, outbuf_size);
439 outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
443 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
444 _("Invalid byte sequence in conversion input"));
451 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
452 _("Error during conversion: %s"),
463 /* call g_iconv with NULL inbuf to cleanup shift state */
465 inbytes_remaining = 0;
472 memset (outp, 0, NUL_TERMINATOR_LENGTH);
475 *bytes_read = p - str;
478 if ((p - str) != len)
482 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
483 _("Partial character sequence at end of input"));
490 *bytes_written = outp - dest; /* Doesn't include '\0' */
503 * @str: the string to convert
504 * @len: the length of the string, or -1 if the string is
505 * nul-terminated<footnote id="nul-unsafe">
507 Note that some encodings may allow nul bytes to
508 occur inside strings. In that case, using -1 for
509 the @len parameter is unsafe.
512 * @to_codeset: name of character set into which to convert @str
513 * @from_codeset: character set of @str.
514 * @bytes_read: (out): location to store the number of bytes in the
515 * input string that were successfully converted, or %NULL.
516 * Even if the conversion was successful, this may be
517 * less than @len if there were partial characters
518 * at the end of the input. If the error
519 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
520 * stored will the byte offset after the last valid
522 * @bytes_written: (out): the number of bytes stored in the output buffer (not
523 * including the terminating nul).
524 * @error: location to store the error occurring, or %NULL to ignore
525 * errors. Any of the errors in #GConvertError may occur.
527 * Converts a string from one character set to another.
529 * Note that you should use g_iconv() for streaming
530 * conversions<footnoteref linkend="streaming-state"/>.
532 * Return value: If the conversion was successful, a newly allocated
533 * nul-terminated string, which must be freed with
534 * g_free(). Otherwise %NULL and @error will be set.
537 g_convert (const gchar *str,
539 const gchar *to_codeset,
540 const gchar *from_codeset,
542 gsize *bytes_written,
548 g_return_val_if_fail (str != NULL, NULL);
549 g_return_val_if_fail (to_codeset != NULL, NULL);
550 g_return_val_if_fail (from_codeset != NULL, NULL);
552 cd = open_converter (to_codeset, from_codeset, error);
554 if (cd == (GIConv) -1)
565 res = g_convert_with_iconv (str, len, cd,
566 bytes_read, bytes_written,
569 close_converter (cd);
575 * g_convert_with_fallback:
576 * @str: the string to convert
577 * @len: the length of the string, or -1 if the string is
578 * nul-terminated<footnoteref linkend="nul-unsafe"/>.
579 * @to_codeset: name of character set into which to convert @str
580 * @from_codeset: character set of @str.
581 * @fallback: UTF-8 string to use in place of character not
582 * present in the target encoding. (The string must be
583 * representable in the target encoding).
584 If %NULL, characters not in the target encoding will
585 be represented as Unicode escapes \uxxxx or \Uxxxxyyyy.
586 * @bytes_read: location to store the number of bytes in the
587 * input string that were successfully converted, or %NULL.
588 * Even if the conversion was successful, this may be
589 * less than @len if there were partial characters
590 * at the end of the input.
591 * @bytes_written: the number of bytes stored in the output buffer (not
592 * including the terminating nul).
593 * @error: location to store the error occurring, or %NULL to ignore
594 * errors. Any of the errors in #GConvertError may occur.
596 * Converts a string from one character set to another, possibly
597 * including fallback sequences for characters not representable
598 * in the output. Note that it is not guaranteed that the specification
599 * for the fallback sequences in @fallback will be honored. Some
600 * systems may do an approximate conversion from @from_codeset
601 * to @to_codeset in their iconv() functions,
602 * in which case GLib will simply return that approximate conversion.
604 * Note that you should use g_iconv() for streaming
605 * conversions<footnoteref linkend="streaming-state"/>.
607 * Return value: If the conversion was successful, a newly allocated
608 * nul-terminated string, which must be freed with
609 * g_free(). Otherwise %NULL and @error will be set.
612 g_convert_with_fallback (const gchar *str,
614 const gchar *to_codeset,
615 const gchar *from_codeset,
616 const gchar *fallback,
618 gsize *bytes_written,
624 const gchar *insert_str = NULL;
626 gsize inbytes_remaining;
627 const gchar *save_p = NULL;
628 gsize save_inbytes = 0;
629 gsize outbytes_remaining;
633 gboolean have_error = FALSE;
634 gboolean done = FALSE;
636 GError *local_error = NULL;
638 g_return_val_if_fail (str != NULL, NULL);
639 g_return_val_if_fail (to_codeset != NULL, NULL);
640 g_return_val_if_fail (from_codeset != NULL, NULL);
645 /* Try an exact conversion; we only proceed if this fails
646 * due to an illegal sequence in the input string.
648 dest = g_convert (str, len, to_codeset, from_codeset,
649 bytes_read, bytes_written, &local_error);
653 if (!g_error_matches (local_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
655 g_propagate_error (error, local_error);
659 g_error_free (local_error);
663 /* No go; to proceed, we need a converter from "UTF-8" to
664 * to_codeset, and the string as UTF-8.
666 cd = open_converter (to_codeset, "UTF-8", error);
667 if (cd == (GIConv) -1)
678 utf8 = g_convert (str, len, "UTF-8", from_codeset,
679 bytes_read, &inbytes_remaining, error);
682 close_converter (cd);
688 /* Now the heart of the code. We loop through the UTF-8 string, and
689 * whenever we hit an offending character, we form fallback, convert
690 * the fallback to the target codeset, and then go back to
691 * converting the original string after finishing with the fallback.
693 * The variables save_p and save_inbytes store the input state
694 * for the original string while we are converting the fallback
698 outbuf_size = len + NUL_TERMINATOR_LENGTH;
699 outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
700 outp = dest = g_malloc (outbuf_size);
702 while (!done && !have_error)
704 gsize inbytes_tmp = inbytes_remaining;
705 err = g_iconv (cd, (char **)&p, &inbytes_tmp, &outp, &outbytes_remaining);
706 inbytes_remaining = inbytes_tmp;
708 if (err == (gsize) -1)
713 g_assert_not_reached();
717 gsize used = outp - dest;
720 dest = g_realloc (dest, outbuf_size);
723 outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
730 /* Error converting fallback string - fatal
732 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
733 _("Cannot convert fallback '%s' to codeset '%s'"),
734 insert_str, to_codeset);
742 gunichar ch = g_utf8_get_char (p);
743 insert_str = g_strdup_printf (ch < 0x10000 ? "\\u%04x" : "\\U%08x",
747 insert_str = fallback;
749 save_p = g_utf8_next_char (p);
750 save_inbytes = inbytes_remaining - (save_p - p);
752 inbytes_remaining = strlen (p);
755 /* fall thru if p is NULL */
760 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
761 _("Error during conversion: %s"),
774 g_free ((gchar *)insert_str);
776 inbytes_remaining = save_inbytes;
781 /* call g_iconv with NULL inbuf to cleanup shift state */
783 inbytes_remaining = 0;
792 memset (outp, 0, NUL_TERMINATOR_LENGTH);
794 close_converter (cd);
797 *bytes_written = outp - dest; /* Doesn't include '\0' */
803 if (save_p && !fallback)
804 g_free ((gchar *)insert_str);
819 strdup_len (const gchar *string,
821 gsize *bytes_written,
828 if (!g_utf8_validate (string, len, NULL))
835 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
836 _("Invalid byte sequence in conversion input"));
841 real_len = strlen (string);
846 while (real_len < len && string[real_len])
851 *bytes_read = real_len;
853 *bytes_written = real_len;
855 return g_strndup (string, real_len);
860 * @opsysstring: a string in the encoding of the current locale. On Windows
861 * this means the system codepage.
862 * @len: the length of the string, or -1 if the string is
863 * nul-terminated<footnoteref linkend="nul-unsafe"/>.
864 * @bytes_read: location to store the number of bytes in the
865 * input string that were successfully converted, or %NULL.
866 * Even if the conversion was successful, this may be
867 * less than @len if there were partial characters
868 * at the end of the input. If the error
869 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
870 * stored will the byte offset after the last valid
872 * @bytes_written: the number of bytes stored in the output buffer (not
873 * including the terminating nul).
874 * @error: location to store the error occurring, or %NULL to ignore
875 * errors. Any of the errors in #GConvertError may occur.
877 * Converts a string which is in the encoding used for strings by
878 * the C runtime (usually the same as that used by the operating
879 * system) in the <link linkend="setlocale">current locale</link> into a
882 * Return value: A newly-allocated buffer containing the converted string,
883 * or %NULL on an error, and error will be set.
886 g_locale_to_utf8 (const gchar *opsysstring,
889 gsize *bytes_written,
894 if (g_get_charset (&charset))
895 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
897 return g_convert (opsysstring, len,
898 "UTF-8", charset, bytes_read, bytes_written, error);
902 * g_locale_from_utf8:
903 * @utf8string: a UTF-8 encoded string
904 * @len: the length of the string, or -1 if the string is
905 * nul-terminated<footnoteref linkend="nul-unsafe"/>.
906 * @bytes_read: location to store the number of bytes in the
907 * input string that were successfully converted, or %NULL.
908 * Even if the conversion was successful, this may be
909 * less than @len if there were partial characters
910 * at the end of the input. If the error
911 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
912 * stored will the byte offset after the last valid
914 * @bytes_written: the number of bytes stored in the output buffer (not
915 * including the terminating nul).
916 * @error: location to store the error occurring, or %NULL to ignore
917 * errors. Any of the errors in #GConvertError may occur.
919 * Converts a string from UTF-8 to the encoding used for strings by
920 * the C runtime (usually the same as that used by the operating
921 * system) in the <link linkend="setlocale">current locale</link>. On
922 * Windows this means the system codepage.
924 * Return value: A newly-allocated buffer containing the converted string,
925 * or %NULL on an error, and error will be set.
928 g_locale_from_utf8 (const gchar *utf8string,
931 gsize *bytes_written,
934 const gchar *charset;
936 if (g_get_charset (&charset))
937 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
939 return g_convert (utf8string, len,
940 charset, "UTF-8", bytes_read, bytes_written, error);
943 #ifndef G_PLATFORM_WIN32
945 typedef struct _GFilenameCharsetCache GFilenameCharsetCache;
947 struct _GFilenameCharsetCache {
950 gchar **filename_charsets;
954 filename_charset_cache_free (gpointer data)
956 GFilenameCharsetCache *cache = data;
957 g_free (cache->charset);
958 g_strfreev (cache->filename_charsets);
963 * g_get_filename_charsets:
964 * @charsets: return location for the %NULL-terminated list of encoding names
966 * Determines the preferred character sets used for filenames.
967 * The first character set from the @charsets is the filename encoding, the
968 * subsequent character sets are used when trying to generate a displayable
969 * representation of a filename, see g_filename_display_name().
971 * On Unix, the character sets are determined by consulting the
972 * environment variables <envar>G_FILENAME_ENCODING</envar> and
973 * <envar>G_BROKEN_FILENAMES</envar>. On Windows, the character set
974 * used in the GLib API is always UTF-8 and said environment variables
977 * <envar>G_FILENAME_ENCODING</envar> may be set to a comma-separated list
978 * of character set names. The special token "@locale" is taken to
979 * mean the character set for the <link linkend="setlocale">current
980 * locale</link>. If <envar>G_FILENAME_ENCODING</envar> is not set, but
981 * <envar>G_BROKEN_FILENAMES</envar> is, the character set of the current
982 * locale is taken as the filename encoding. If neither environment variable
983 * is set, UTF-8 is taken as the filename encoding, but the character
984 * set of the current locale is also put in the list of encodings.
986 * The returned @charsets belong to GLib and must not be freed.
988 * Note that on Unix, regardless of the locale character set or
989 * <envar>G_FILENAME_ENCODING</envar> value, the actual file names present
990 * on a system might be in any random encoding or just gibberish.
992 * Return value: %TRUE if the filename encoding is UTF-8.
997 g_get_filename_charsets (const gchar ***filename_charsets)
999 static GPrivate cache_private = G_PRIVATE_INIT (filename_charset_cache_free);
1000 GFilenameCharsetCache *cache = g_private_get (&cache_private);
1001 const gchar *charset;
1005 cache = g_new0 (GFilenameCharsetCache, 1);
1006 g_private_set (&cache_private, cache);
1009 g_get_charset (&charset);
1011 if (!(cache->charset && strcmp (cache->charset, charset) == 0))
1013 const gchar *new_charset;
1017 g_free (cache->charset);
1018 g_strfreev (cache->filename_charsets);
1019 cache->charset = g_strdup (charset);
1021 p = getenv ("G_FILENAME_ENCODING");
1022 if (p != NULL && p[0] != '\0')
1024 cache->filename_charsets = g_strsplit (p, ",", 0);
1025 cache->is_utf8 = (strcmp (cache->filename_charsets[0], "UTF-8") == 0);
1027 for (i = 0; cache->filename_charsets[i]; i++)
1029 if (strcmp ("@locale", cache->filename_charsets[i]) == 0)
1031 g_get_charset (&new_charset);
1032 g_free (cache->filename_charsets[i]);
1033 cache->filename_charsets[i] = g_strdup (new_charset);
1037 else if (getenv ("G_BROKEN_FILENAMES") != NULL)
1039 cache->filename_charsets = g_new0 (gchar *, 2);
1040 cache->is_utf8 = g_get_charset (&new_charset);
1041 cache->filename_charsets[0] = g_strdup (new_charset);
1045 cache->filename_charsets = g_new0 (gchar *, 3);
1046 cache->is_utf8 = TRUE;
1047 cache->filename_charsets[0] = g_strdup ("UTF-8");
1048 if (!g_get_charset (&new_charset))
1049 cache->filename_charsets[1] = g_strdup (new_charset);
1053 if (filename_charsets)
1054 *filename_charsets = (const gchar **)cache->filename_charsets;
1056 return cache->is_utf8;
1059 #else /* G_PLATFORM_WIN32 */
1062 g_get_filename_charsets (const gchar ***filename_charsets)
1064 static const gchar *charsets[] = {
1070 /* On Windows GLib pretends that the filename charset is UTF-8 */
1071 if (filename_charsets)
1072 *filename_charsets = charsets;
1078 /* Cygwin works like before */
1079 result = g_get_charset (&(charsets[0]));
1081 if (filename_charsets)
1082 *filename_charsets = charsets;
1088 #endif /* G_PLATFORM_WIN32 */
1091 get_filename_charset (const gchar **filename_charset)
1093 const gchar **charsets;
1096 is_utf8 = g_get_filename_charsets (&charsets);
1098 if (filename_charset)
1099 *filename_charset = charsets[0];
1105 * g_filename_to_utf8:
1106 * @opsysstring: a string in the encoding for filenames
1107 * @len: the length of the string, or -1 if the string is
1108 * nul-terminated<footnoteref linkend="nul-unsafe"/>.
1109 * @bytes_read: location to store the number of bytes in the
1110 * input string that were successfully converted, or %NULL.
1111 * Even if the conversion was successful, this may be
1112 * less than @len if there were partial characters
1113 * at the end of the input. If the error
1114 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
1115 * stored will the byte offset after the last valid
1117 * @bytes_written: the number of bytes stored in the output buffer (not
1118 * including the terminating nul).
1119 * @error: location to store the error occurring, or %NULL to ignore
1120 * errors. Any of the errors in #GConvertError may occur.
1122 * Converts a string which is in the encoding used by GLib for
1123 * filenames into a UTF-8 string. Note that on Windows GLib uses UTF-8
1124 * for filenames; on other platforms, this function indirectly depends on
1125 * the <link linkend="setlocale">current locale</link>.
1127 * Return value: The converted string, or %NULL on an error.
1130 g_filename_to_utf8 (const gchar *opsysstring,
1133 gsize *bytes_written,
1136 const gchar *charset;
1138 g_return_val_if_fail (opsysstring != NULL, NULL);
1140 if (get_filename_charset (&charset))
1141 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
1143 return g_convert (opsysstring, len,
1144 "UTF-8", charset, bytes_read, bytes_written, error);
1147 #if defined (G_OS_WIN32) && !defined (_WIN64)
1149 #undef g_filename_to_utf8
1151 /* Binary compatibility version. Not for newly compiled code. Also not needed for
1152 * 64-bit versions as there should be no old deployed binaries that would use
1157 g_filename_to_utf8 (const gchar *opsysstring,
1160 gsize *bytes_written,
1163 const gchar *charset;
1165 g_return_val_if_fail (opsysstring != NULL, NULL);
1167 if (g_get_charset (&charset))
1168 return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
1170 return g_convert (opsysstring, len,
1171 "UTF-8", charset, bytes_read, bytes_written, error);
1177 * g_filename_from_utf8:
1178 * @utf8string: a UTF-8 encoded string.
1179 * @len: the length of the string, or -1 if the string is
1181 * @bytes_read: (out) (allow-none): location to store the number of bytes in
1182 * the input string that were successfully converted, or %NULL.
1183 * Even if the conversion was successful, this may be
1184 * less than @len if there were partial characters
1185 * at the end of the input. If the error
1186 * #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
1187 * stored will the byte offset after the last valid
1189 * @bytes_written: (out): the number of bytes stored in the output buffer (not
1190 * including the terminating nul).
1191 * @error: location to store the error occurring, or %NULL to ignore
1192 * errors. Any of the errors in #GConvertError may occur.
1194 * Converts a string from UTF-8 to the encoding GLib uses for
1195 * filenames. Note that on Windows GLib uses UTF-8 for filenames;
1196 * on other platforms, this function indirectly depends on the
1197 * <link linkend="setlocale">current locale</link>.
1199 * Return value: (array length=bytes_written) (element-type guint8) (transfer full):
1200 * The converted string, or %NULL on an error.
1203 g_filename_from_utf8 (const gchar *utf8string,
1206 gsize *bytes_written,
1209 const gchar *charset;
1211 if (get_filename_charset (&charset))
1212 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
1214 return g_convert (utf8string, len,
1215 charset, "UTF-8", bytes_read, bytes_written, error);
1218 #if defined (G_OS_WIN32) && !defined (_WIN64)
1220 #undef g_filename_from_utf8
1222 /* Binary compatibility version. Not for newly compiled code. */
1225 g_filename_from_utf8 (const gchar *utf8string,
1228 gsize *bytes_written,
1231 const gchar *charset;
1233 if (g_get_charset (&charset))
1234 return strdup_len (utf8string, len, bytes_read, bytes_written, error);
1236 return g_convert (utf8string, len,
1237 charset, "UTF-8", bytes_read, bytes_written, error);
1242 /* Test of haystack has the needle prefix, comparing case
1243 * insensitive. haystack may be UTF-8, but needle must
1244 * contain only ascii. */
1246 has_case_prefix (const gchar *haystack, const gchar *needle)
1250 /* Eat one character at a time. */
1255 g_ascii_tolower (*n) == g_ascii_tolower (*h))
1265 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
1266 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
1267 UNSAFE_PATH = 0x8, /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */
1268 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
1269 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
1270 } UnsafeCharacterSet;
1272 static const guchar acceptable[96] = {
1273 /* A table of the ASCII chars from space (32) to DEL (127) */
1274 /* ! " # $ % & ' ( ) * + , - . / */
1275 0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
1276 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
1277 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
1278 /* @ A B C D E F G H I J K L M N O */
1279 0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
1280 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
1281 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
1282 /* ` a b c d e f g h i j k l m n o */
1283 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
1284 /* p q r s t u v w x y z { | } ~ DEL */
1285 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
1288 static const gchar hex[16] = "0123456789ABCDEF";
1290 /* Note: This escape function works on file: URIs, but if you want to
1291 * escape something else, please read RFC-2396 */
1293 g_escape_uri_string (const gchar *string,
1294 UnsafeCharacterSet mask)
1296 #define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
1303 UnsafeCharacterSet use_mask;
1305 g_return_val_if_fail (mask == UNSAFE_ALL
1306 || mask == UNSAFE_ALLOW_PLUS
1307 || mask == UNSAFE_PATH
1308 || mask == UNSAFE_HOST
1309 || mask == UNSAFE_SLASHES, NULL);
1313 for (p = string; *p != '\0'; p++)
1316 if (!ACCEPTABLE (c))
1320 result = g_malloc (p - string + unacceptable * 2 + 1);
1323 for (q = result, p = string; *p != '\0'; p++)
1327 if (!ACCEPTABLE (c))
1329 *q++ = '%'; /* means hex coming */
1344 g_escape_file_uri (const gchar *hostname,
1345 const gchar *pathname)
1347 char *escaped_hostname = NULL;
1352 char *p, *backslash;
1354 /* Turn backslashes into forward slashes. That's what Netscape
1355 * does, and they are actually more or less equivalent in Windows.
1358 pathname = g_strdup (pathname);
1359 p = (char *) pathname;
1361 while ((backslash = strchr (p, '\\')) != NULL)
1368 if (hostname && *hostname != '\0')
1370 escaped_hostname = g_escape_uri_string (hostname, UNSAFE_HOST);
1373 escaped_path = g_escape_uri_string (pathname, UNSAFE_PATH);
1375 res = g_strconcat ("file://",
1376 (escaped_hostname) ? escaped_hostname : "",
1377 (*escaped_path != '/') ? "/" : "",
1382 g_free ((char *) pathname);
1385 g_free (escaped_hostname);
1386 g_free (escaped_path);
1392 unescape_character (const char *scanner)
1397 first_digit = g_ascii_xdigit_value (scanner[0]);
1398 if (first_digit < 0)
1401 second_digit = g_ascii_xdigit_value (scanner[1]);
1402 if (second_digit < 0)
1405 return (first_digit << 4) | second_digit;
1409 g_unescape_uri_string (const char *escaped,
1411 const char *illegal_escaped_characters,
1412 gboolean ascii_must_not_be_escaped)
1414 const gchar *in, *in_end;
1415 gchar *out, *result;
1418 if (escaped == NULL)
1422 len = strlen (escaped);
1424 result = g_malloc (len + 1);
1427 for (in = escaped, in_end = escaped + len; in < in_end; in++)
1433 /* catch partial escape sequences past the end of the substring */
1434 if (in + 3 > in_end)
1437 c = unescape_character (in + 1);
1439 /* catch bad escape sequences and NUL characters */
1443 /* catch escaped ASCII */
1444 if (ascii_must_not_be_escaped && c <= 0x7F)
1447 /* catch other illegal escaped characters */
1448 if (strchr (illegal_escaped_characters, c) != NULL)
1457 g_assert (out - result <= len);
1470 is_asciialphanum (gunichar c)
1472 return c <= 0x7F && g_ascii_isalnum (c);
1476 is_asciialpha (gunichar c)
1478 return c <= 0x7F && g_ascii_isalpha (c);
1481 /* allows an empty string */
1483 hostname_validate (const char *hostname)
1486 gunichar c, first_char, last_char;
1493 /* read in a label */
1494 c = g_utf8_get_char (p);
1495 p = g_utf8_next_char (p);
1496 if (!is_asciialphanum (c))
1502 c = g_utf8_get_char (p);
1503 p = g_utf8_next_char (p);
1505 while (is_asciialphanum (c) || c == '-');
1506 if (last_char == '-')
1509 /* if that was the last label, check that it was a toplabel */
1510 if (c == '\0' || (c == '.' && *p == '\0'))
1511 return is_asciialpha (first_char);
1518 * g_filename_from_uri:
1519 * @uri: a uri describing a filename (escaped, encoded in ASCII).
1520 * @hostname: (out) (allow-none): Location to store hostname for the URI, or %NULL.
1521 * If there is no hostname in the URI, %NULL will be
1522 * stored in this location.
1523 * @error: location to store the error occurring, or %NULL to ignore
1524 * errors. Any of the errors in #GConvertError may occur.
1526 * Converts an escaped ASCII-encoded URI to a local filename in the
1527 * encoding used for filenames.
1529 * Return value: (type filename): a newly-allocated string holding
1530 * the resulting filename, or %NULL on an error.
1533 g_filename_from_uri (const gchar *uri,
1537 const char *path_part;
1538 const char *host_part;
1539 char *unescaped_hostname;
1550 if (!has_case_prefix (uri, "file:/"))
1552 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1553 _("The URI '%s' is not an absolute URI using the \"file\" scheme"),
1558 path_part = uri + strlen ("file:");
1560 if (strchr (path_part, '#') != NULL)
1562 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1563 _("The local file URI '%s' may not include a '#'"),
1568 if (has_case_prefix (path_part, "///"))
1570 else if (has_case_prefix (path_part, "//"))
1573 host_part = path_part;
1575 path_part = strchr (path_part, '/');
1577 if (path_part == NULL)
1579 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1580 _("The URI '%s' is invalid"),
1585 unescaped_hostname = g_unescape_uri_string (host_part, path_part - host_part, "", TRUE);
1587 if (unescaped_hostname == NULL ||
1588 !hostname_validate (unescaped_hostname))
1590 g_free (unescaped_hostname);
1591 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1592 _("The hostname of the URI '%s' is invalid"),
1598 *hostname = unescaped_hostname;
1600 g_free (unescaped_hostname);
1603 filename = g_unescape_uri_string (path_part, -1, "/", FALSE);
1605 if (filename == NULL)
1607 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
1608 _("The URI '%s' contains invalidly escaped characters"),
1615 /* Drop localhost */
1616 if (hostname && *hostname != NULL &&
1617 g_ascii_strcasecmp (*hostname, "localhost") == 0)
1623 /* Turn slashes into backslashes, because that's the canonical spelling */
1625 while ((slash = strchr (p, '/')) != NULL)
1631 /* Windows URIs with a drive letter can be like "file://host/c:/foo"
1632 * or "file://host/c|/foo" (some Netscape versions). In those cases, start
1633 * the filename from the drive letter.
1635 if (g_ascii_isalpha (filename[1]))
1637 if (filename[2] == ':')
1639 else if (filename[2] == '|')
1647 result = g_strdup (filename + offs);
1653 #if defined (G_OS_WIN32) && !defined (_WIN64)
1655 #undef g_filename_from_uri
1658 g_filename_from_uri (const gchar *uri,
1662 gchar *utf8_filename;
1663 gchar *retval = NULL;
1665 utf8_filename = g_filename_from_uri_utf8 (uri, hostname, error);
1668 retval = g_locale_from_utf8 (utf8_filename, -1, NULL, NULL, error);
1669 g_free (utf8_filename);
1677 * g_filename_to_uri:
1678 * @filename: an absolute filename specified in the GLib file name encoding,
1679 * which is the on-disk file name bytes on Unix, and UTF-8 on
1681 * @hostname: (allow-none): A UTF-8 encoded hostname, or %NULL for none.
1682 * @error: location to store the error occurring, or %NULL to ignore
1683 * errors. Any of the errors in #GConvertError may occur.
1685 * Converts an absolute filename to an escaped ASCII-encoded URI, with the path
1686 * component following Section 3.3. of RFC 2396.
1688 * Return value: a newly-allocated string holding the resulting
1689 * URI, or %NULL on an error.
1692 g_filename_to_uri (const gchar *filename,
1693 const gchar *hostname,
1698 g_return_val_if_fail (filename != NULL, NULL);
1700 if (!g_path_is_absolute (filename))
1702 g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
1703 _("The pathname '%s' is not an absolute path"),
1709 !(g_utf8_validate (hostname, -1, NULL)
1710 && hostname_validate (hostname)))
1712 g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
1713 _("Invalid hostname"));
1718 /* Don't use localhost unnecessarily */
1719 if (hostname && g_ascii_strcasecmp (hostname, "localhost") == 0)
1723 escaped_uri = g_escape_file_uri (hostname, filename);
1728 #if defined (G_OS_WIN32) && !defined (_WIN64)
1730 #undef g_filename_to_uri
1733 g_filename_to_uri (const gchar *filename,
1734 const gchar *hostname,
1737 gchar *utf8_filename;
1738 gchar *retval = NULL;
1740 utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1744 retval = g_filename_to_uri_utf8 (utf8_filename, hostname, error);
1745 g_free (utf8_filename);
1754 * g_uri_list_extract_uris:
1755 * @uri_list: an URI list
1757 * Splits an URI list conforming to the text/uri-list
1758 * mime type defined in RFC 2483 into individual URIs,
1759 * discarding any comments. The URIs are not validated.
1761 * Returns: (transfer full): a newly allocated %NULL-terminated list
1762 * of strings holding the individual URIs. The array should be freed
1763 * with g_strfreev().
1768 g_uri_list_extract_uris (const gchar *uri_list)
1779 /* We don't actually try to validate the URI according to RFC
1780 * 2396, or even check for allowed characters - we just ignore
1781 * comments and trim whitespace off the ends. We also
1782 * allow LF delimination as well as the specified CRLF.
1784 * We do allow comments like specified in RFC 2483.
1790 while (g_ascii_isspace (*p))
1794 while (*q && (*q != '\n') && (*q != '\r'))
1800 while (q > p && g_ascii_isspace (*q))
1805 uris = g_slist_prepend (uris, g_strndup (p, q - p + 1));
1810 p = strchr (p, '\n');
1815 result = g_new (gchar *, n_uris + 1);
1817 result[n_uris--] = NULL;
1818 for (u = uris; u; u = u->next)
1819 result[n_uris--] = u->data;
1821 g_slist_free (uris);
1827 * g_filename_display_basename:
1828 * @filename: an absolute pathname in the GLib file name encoding
1830 * Returns the display basename for the particular filename, guaranteed
1831 * to be valid UTF-8. The display name might not be identical to the filename,
1832 * for instance there might be problems converting it to UTF-8, and some files
1833 * can be translated in the display.
1835 * If GLib cannot make sense of the encoding of @filename, as a last resort it
1836 * replaces unknown characters with U+FFFD, the Unicode replacement character.
1837 * You can search the result for the UTF-8 encoding of this character (which is
1838 * "\357\277\275" in octal notation) to find out if @filename was in an invalid
1841 * You must pass the whole absolute pathname to this functions so that
1842 * translation of well known locations can be done.
1844 * This function is preferred over g_filename_display_name() if you know the
1845 * whole path, as it allows translation.
1847 * Return value: a newly allocated string containing
1848 * a rendition of the basename of the filename in valid UTF-8
1853 g_filename_display_basename (const gchar *filename)
1858 g_return_val_if_fail (filename != NULL, NULL);
1860 basename = g_path_get_basename (filename);
1861 display_name = g_filename_display_name (basename);
1863 return display_name;
1867 * g_filename_display_name:
1868 * @filename: a pathname hopefully in the GLib file name encoding
1870 * Converts a filename into a valid UTF-8 string. The conversion is
1871 * not necessarily reversible, so you should keep the original around
1872 * and use the return value of this function only for display purposes.
1873 * Unlike g_filename_to_utf8(), the result is guaranteed to be non-%NULL
1874 * even if the filename actually isn't in the GLib file name encoding.
1876 * If GLib cannot make sense of the encoding of @filename, as a last resort it
1877 * replaces unknown characters with U+FFFD, the Unicode replacement character.
1878 * You can search the result for the UTF-8 encoding of this character (which is
1879 * "\357\277\275" in octal notation) to find out if @filename was in an invalid
1882 * If you know the whole pathname of the file you should use
1883 * g_filename_display_basename(), since that allows location-based
1884 * translation of filenames.
1886 * Return value: a newly allocated string containing
1887 * a rendition of the filename in valid UTF-8
1892 g_filename_display_name (const gchar *filename)
1895 const gchar **charsets;
1896 gchar *display_name = NULL;
1899 is_utf8 = g_get_filename_charsets (&charsets);
1903 if (g_utf8_validate (filename, -1, NULL))
1904 display_name = g_strdup (filename);
1909 /* Try to convert from the filename charsets to UTF-8.
1910 * Skip the first charset if it is UTF-8.
1912 for (i = is_utf8 ? 1 : 0; charsets[i]; i++)
1914 display_name = g_convert (filename, -1, "UTF-8", charsets[i],
1922 /* if all conversions failed, we replace invalid UTF-8
1923 * by a question mark
1926 display_name = _g_utf8_make_valid (filename);
1928 return display_name;