Blame glib/gconvert.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 *
Packit ae235b
 * gconvert.c: Convert between character sets using iconv
Packit ae235b
 * Copyright Red Hat Inc., 2000
Packit ae235b
 * Authors: Havoc Pennington <hp@redhat.com>, Owen Taylor <otaylor@redhat.com>
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "glibconfig.h"
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
#include <iconv.h>
Packit ae235b
#endif
Packit ae235b
#include <errno.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include "win_iconv.c"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_PLATFORM_WIN32
Packit ae235b
#define STRICT
Packit ae235b
#include <windows.h>
Packit ae235b
#undef STRICT
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gconvert.h"
Packit ae235b
Packit ae235b
#include "gcharsetprivate.h"
Packit ae235b
#include "gslist.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "gunicode.h"
Packit ae235b
#include "gfileutils.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#if defined(USE_LIBICONV_GNU) && !defined (_LIBICONV_H)
Packit ae235b
#error GNU libiconv in use but included iconv.h not from libiconv
Packit ae235b
#endif
Packit ae235b
#if !defined(USE_LIBICONV_GNU) && defined (_LIBICONV_H) \
Packit ae235b
     && !defined (__APPLE_CC__) && !defined (__LP_64__)
Packit ae235b
#error GNU libiconv not in use but included iconv.h is from libiconv
Packit ae235b
#endif
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:conversions
Packit ae235b
 * @title: Character Set Conversion
Packit ae235b
 * @short_description: convert strings between different character sets
Packit ae235b
 *
Packit ae235b
 * The g_convert() family of function wraps the functionality of iconv().
Packit ae235b
 * In addition to pure character set conversions, GLib has functions to
Packit ae235b
 * deal with the extra complications of encodings for file names.
Packit ae235b
 *
Packit ae235b
 * ## File Name Encodings
Packit ae235b
 *
Packit ae235b
 * Historically, UNIX has not had a defined encoding for file names:
Packit ae235b
 * a file name is valid as long as it does not have path separators
Packit ae235b
 * in it ("/"). However, displaying file names may require conversion:
Packit ae235b
 * from the character set in which they were created, to the character
Packit ae235b
 * set in which the application operates. Consider the Spanish file name
Packit ae235b
 * "Presentación.sxi". If the application which created it uses
Packit ae235b
 * ISO-8859-1 for its encoding,
Packit ae235b
 * |[
Packit ae235b
 * Character:  P  r  e  s  e  n  t  a  c  i  ó  n  .  s  x  i
Packit ae235b
 * Hex code:   50 72 65 73 65 6e 74 61 63 69 f3 6e 2e 73 78 69
Packit ae235b
 * ]|
Packit ae235b
 * However, if the application use UTF-8, the actual file name on
Packit ae235b
 * disk would look like this:
Packit ae235b
 * |[
Packit ae235b
 * Character:  P  r  e  s  e  n  t  a  c  i  ó     n  .  s  x  i
Packit ae235b
 * Hex code:   50 72 65 73 65 6e 74 61 63 69 c3 b3 6e 2e 73 78 69
Packit ae235b
 * ]|
Packit ae235b
 * Glib uses UTF-8 for its strings, and GUI toolkits like GTK+ that use
Packit ae235b
 * GLib do the same thing. If you get a file name from the file system,
Packit ae235b
 * for example, from readdir() or from g_dir_read_name(), and you wish
Packit ae235b
 * to display the file name to the user, you  will need to convert it
Packit ae235b
 * into UTF-8. The opposite case is when the user types the name of a
Packit ae235b
 * file they wish to save: the toolkit will give you that string in
Packit ae235b
 * UTF-8 encoding, and you will need to convert it to the character
Packit ae235b
 * set used for file names before you can create the file with open()
Packit ae235b
 * or fopen().
Packit ae235b
 *
Packit ae235b
 * By default, GLib assumes that file names on disk are in UTF-8
Packit ae235b
 * encoding. This is a valid assumption for file systems which
Packit ae235b
 * were created relatively recently: most applications use UTF-8
Packit ae235b
 * encoding for their strings, and that is also what they use for
Packit ae235b
 * the file names they create. However, older file systems may
Packit ae235b
 * still contain file names created in "older" encodings, such as
Packit ae235b
 * ISO-8859-1. In this case, for compatibility reasons, you may want
Packit ae235b
 * to instruct GLib to use that particular encoding for file names
Packit ae235b
 * rather than UTF-8. You can do this by specifying the encoding for
Packit ae235b
 * file names in the [`G_FILENAME_ENCODING`][G_FILENAME_ENCODING]
Packit ae235b
 * environment variable. For example, if your installation uses
Packit ae235b
 * ISO-8859-1 for file names, you can put this in your `~/.profile`:
Packit ae235b
 * |[
Packit ae235b
 * export G_FILENAME_ENCODING=ISO-8859-1
Packit ae235b
 * ]|
Packit ae235b
 * GLib provides the functions g_filename_to_utf8() and
Packit ae235b
 * g_filename_from_utf8() to perform the necessary conversions.
Packit ae235b
 * These functions convert file names from the encoding specified
Packit ae235b
 * in `G_FILENAME_ENCODING` to UTF-8 and vice-versa. This
Packit ae235b
 * [diagram][file-name-encodings-diagram] illustrates how
Packit ae235b
 * these functions are used to convert between UTF-8 and the
Packit ae235b
 * encoding for file names in the file system.
Packit ae235b
 *
Packit ae235b
 * ## Conversion between file name encodings # {#file-name-encodings-diagram)
Packit ae235b
 *
Packit ae235b
 * ![](file-name-encodings.png)
Packit ae235b
 *
Packit ae235b
 * ## Checklist for Application Writers
Packit ae235b
 *
Packit ae235b
 * This section is a practical summary of the detailed
Packit ae235b
 * things to do to make sure your applications process file
Packit ae235b
 * name encodings correctly.
Packit ae235b
 * 
Packit ae235b
 * 1. If you get a file name from the file system from a function
Packit ae235b
 *    such as readdir() or gtk_file_chooser_get_filename(), you do
Packit ae235b
 *    not need to do any conversion to pass that file name to
Packit ae235b
 *    functions like open(), rename(), or fopen() -- those are "raw"
Packit ae235b
 *    file names which the file system understands.
Packit ae235b
 *
Packit ae235b
 * 2. If you need to display a file name, convert it to UTF-8 first
Packit ae235b
 *    by using g_filename_to_utf8(). If conversion fails, display a
Packit ae235b
 *    string like "Unknown file name". Do not convert this string back
Packit ae235b
 *    into the encoding used for file names if you wish to pass it to
Packit ae235b
 *    the file system; use the original file name instead.
Packit ae235b
 *
Packit ae235b
 *    For example, the document window of a word processor could display
Packit ae235b
 *    "Unknown file name" in its title bar but still let the user save
Packit ae235b
 *    the file, as it would keep the raw file name internally. This
Packit ae235b
 *    can happen if the user has not set the `G_FILENAME_ENCODING`
Packit ae235b
 *    environment variable even though he has files whose names are
Packit ae235b
 *    not encoded in UTF-8.
Packit ae235b
 *
Packit ae235b
 * 3. If your user interface lets the user type a file name for saving
Packit ae235b
 *    or renaming, convert it to the encoding used for file names in
Packit ae235b
 *    the file system by using g_filename_from_utf8(). Pass the converted
Packit ae235b
 *    file name to functions like fopen(). If conversion fails, ask the
Packit ae235b
 *    user to enter a different file name. This can happen if the user
Packit ae235b
 *    types Japanese characters when `G_FILENAME_ENCODING` is set to
Packit ae235b
 *    `ISO-8859-1`, for example.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* We try to terminate strings in unknown charsets with this many zero bytes
Packit ae235b
 * to ensure that multibyte strings really are nul-terminated when we return
Packit ae235b
 * them from g_convert() and friends.
Packit ae235b
 */
Packit ae235b
#define NUL_TERMINATOR_LENGTH 4
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g_convert_error, g_convert_error)
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
try_conversion (const char *to_codeset,
Packit ae235b
		const char *from_codeset,
Packit ae235b
		iconv_t    *cd)
Packit ae235b
{
Packit ae235b
  *cd = iconv_open (to_codeset, from_codeset);
Packit ae235b
Packit ae235b
  if (*cd == (iconv_t)-1 && errno == EINVAL)
Packit ae235b
    return FALSE;
Packit ae235b
  else
Packit ae235b
    return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
try_to_aliases (const char **to_aliases,
Packit ae235b
		const char  *from_codeset,
Packit ae235b
		iconv_t     *cd)
Packit ae235b
{
Packit ae235b
  if (to_aliases)
Packit ae235b
    {
Packit ae235b
      const char **p = to_aliases;
Packit ae235b
      while (*p)
Packit ae235b
	{
Packit ae235b
	  if (try_conversion (*p, from_codeset, cd))
Packit ae235b
	    return TRUE;
Packit ae235b
Packit ae235b
	  p++;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_iconv_open: (skip)
Packit ae235b
 * @to_codeset: destination codeset
Packit ae235b
 * @from_codeset: source codeset
Packit ae235b
 * 
Packit ae235b
 * Same as the standard UNIX routine iconv_open(), but
Packit ae235b
 * may be implemented via libiconv on UNIX flavors that lack
Packit ae235b
 * a native implementation.
Packit ae235b
 * 
Packit ae235b
 * GLib provides g_convert() and g_locale_to_utf8() which are likely
Packit ae235b
 * more convenient than the raw iconv wrappers.
Packit ae235b
 * 
Packit ae235b
 * Returns: a "conversion descriptor", or (GIConv)-1 if
Packit ae235b
 *  opening the converter failed.
Packit ae235b
 **/
Packit ae235b
GIConv
Packit ae235b
g_iconv_open (const gchar  *to_codeset,
Packit ae235b
	      const gchar  *from_codeset)
Packit ae235b
{
Packit ae235b
  iconv_t cd;
Packit ae235b
  
Packit ae235b
  if (!try_conversion (to_codeset, from_codeset, &cd))
Packit ae235b
    {
Packit ae235b
      const char **to_aliases = _g_charset_get_aliases (to_codeset);
Packit ae235b
      const char **from_aliases = _g_charset_get_aliases (from_codeset);
Packit ae235b
Packit ae235b
      if (from_aliases)
Packit ae235b
	{
Packit ae235b
	  const char **p = from_aliases;
Packit ae235b
	  while (*p)
Packit ae235b
	    {
Packit ae235b
	      if (try_conversion (to_codeset, *p, &cd))
Packit ae235b
		goto out;
Packit ae235b
Packit ae235b
	      if (try_to_aliases (to_aliases, *p, &cd))
Packit ae235b
		goto out;
Packit ae235b
Packit ae235b
	      p++;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (try_to_aliases (to_aliases, from_codeset, &cd))
Packit ae235b
	goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  return (cd == (iconv_t)-1) ? (GIConv)-1 : (GIConv)cd;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_iconv: (skip)
Packit ae235b
 * @converter: conversion descriptor from g_iconv_open()
Packit ae235b
 * @inbuf: bytes to convert
Packit ae235b
 * @inbytes_left: inout parameter, bytes remaining to convert in @inbuf
Packit ae235b
 * @outbuf: converted output bytes
Packit ae235b
 * @outbytes_left: inout parameter, bytes available to fill in @outbuf
Packit ae235b
 * 
Packit ae235b
 * Same as the standard UNIX routine iconv(), but
Packit ae235b
 * may be implemented via libiconv on UNIX flavors that lack
Packit ae235b
 * a native implementation.
Packit ae235b
 *
Packit ae235b
 * GLib provides g_convert() and g_locale_to_utf8() which are likely
Packit ae235b
 * more convenient than the raw iconv wrappers.
Packit ae235b
 * 
Packit ae235b
 * Note that the behaviour of iconv() for characters which are valid in the
Packit ae235b
 * input character set, but which have no representation in the output character
Packit ae235b
 * set, is implementation defined. This function may return success (with a
Packit ae235b
 * positive number of non-reversible conversions as replacement characters were
Packit ae235b
 * used), or it may return -1 and set an error such as %EILSEQ, in such a
Packit ae235b
 * situation.
Packit ae235b
 *
Packit ae235b
 * Returns: count of non-reversible conversions, or -1 on error
Packit ae235b
 **/
Packit ae235b
gsize 
Packit ae235b
g_iconv (GIConv   converter,
Packit ae235b
	 gchar  **inbuf,
Packit ae235b
	 gsize   *inbytes_left,
Packit ae235b
	 gchar  **outbuf,
Packit ae235b
	 gsize   *outbytes_left)
Packit ae235b
{
Packit ae235b
  iconv_t cd = (iconv_t)converter;
Packit ae235b
Packit ae235b
  return iconv (cd, inbuf, inbytes_left, outbuf, outbytes_left);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_iconv_close: (skip)
Packit ae235b
 * @converter: a conversion descriptor from g_iconv_open()
Packit ae235b
 *
Packit ae235b
 * Same as the standard UNIX routine iconv_close(), but
Packit ae235b
 * may be implemented via libiconv on UNIX flavors that lack
Packit ae235b
 * a native implementation. Should be called to clean up
Packit ae235b
 * the conversion descriptor from g_iconv_open() when
Packit ae235b
 * you are done converting things.
Packit ae235b
 *
Packit ae235b
 * GLib provides g_convert() and g_locale_to_utf8() which are likely
Packit ae235b
 * more convenient than the raw iconv wrappers.
Packit ae235b
 * 
Packit ae235b
 * Returns: -1 on error, 0 on success
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_iconv_close (GIConv converter)
Packit ae235b
{
Packit ae235b
  iconv_t cd = (iconv_t)converter;
Packit ae235b
Packit ae235b
  return iconv_close (cd);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIConv
Packit ae235b
open_converter (const gchar *to_codeset,
Packit ae235b
		const gchar *from_codeset,
Packit ae235b
		GError     **error)
Packit ae235b
{
Packit ae235b
  GIConv cd;
Packit ae235b
Packit ae235b
  cd = g_iconv_open (to_codeset, from_codeset);
Packit ae235b
Packit ae235b
  if (cd == (GIConv) -1)
Packit ae235b
    {
Packit ae235b
      /* Something went wrong.  */
Packit ae235b
      if (error)
Packit ae235b
	{
Packit ae235b
	  if (errno == EINVAL)
Packit ae235b
	    g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
Packit ae235b
			 _("Conversion from character set “%s” to “%s” is not supported"),
Packit ae235b
			 from_codeset, to_codeset);
Packit ae235b
	  else
Packit ae235b
	    g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
			 _("Could not open converter from “%s” to “%s”"),
Packit ae235b
			 from_codeset, to_codeset);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return cd;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
close_converter (GIConv cd)
Packit ae235b
{
Packit ae235b
  if (cd == (GIConv) -1)
Packit ae235b
    return 0;
Packit ae235b
  
Packit ae235b
  return g_iconv_close (cd);  
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_convert_with_iconv: (skip)
Packit ae235b
 * @str:           (array length=len) (element-type guint8):
Packit ae235b
 *                 the string to convert.
Packit ae235b
 * @len:           the length of the string in bytes, or -1 if the string is
Packit ae235b
 *                 nul-terminated (Note that some encodings may allow nul
Packit ae235b
 *                 bytes to occur inside strings. In that case, using -1
Packit ae235b
 *                 for the @len parameter is unsafe)
Packit ae235b
 * @converter:     conversion descriptor from g_iconv_open()
Packit ae235b
 * @bytes_read:    (out) (optional): location to store the number of bytes in
Packit ae235b
 *                 the input string that were successfully converted, or %NULL.
Packit ae235b
 *                 Even if the conversion was successful, this may be 
Packit ae235b
 *                 less than @len if there were partial characters
Packit ae235b
 *                 at the end of the input. If the error
Packit ae235b
 *                 #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
Packit ae235b
 *                 stored will be the byte offset after the last valid
Packit ae235b
 *                 input sequence.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in
Packit ae235b
 *                 the output buffer (not including the terminating nul).
Packit ae235b
 * @error:         location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                 errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 *
Packit ae235b
 * Converts a string from one character set to another. 
Packit ae235b
 * 
Packit ae235b
 * Note that you should use g_iconv() for streaming conversions. 
Packit ae235b
 * Despite the fact that @bytes_read can return information about partial
Packit ae235b
 * characters, the g_convert_... functions are not generally suitable
Packit ae235b
 * for streaming. If the underlying converter maintains internal state,
Packit ae235b
 * then this won't be preserved across successive calls to g_convert(),
Packit ae235b
 * g_convert_with_iconv() or g_convert_with_fallback(). (An example of
Packit ae235b
 * this is the GNU C converter for CP1255 which does not emit a base
Packit ae235b
 * character until it knows that the next character is not a mark that
Packit ae235b
 * could combine with the base character.)
Packit ae235b
 *
Packit ae235b
 * Characters which are valid in the input character set, but which have no
Packit ae235b
 * representation in the output character set will result in a
Packit ae235b
 * %G_CONVERT_ERROR_ILLEGAL_SEQUENCE error. This is in contrast to the iconv()
Packit ae235b
 * specification, which leaves this behaviour implementation defined. Note that
Packit ae235b
 * this is the same error code as is returned for an invalid byte sequence in
Packit ae235b
 * the input character set. To get defined behaviour for conversion of
Packit ae235b
 * unrepresentable characters, use g_convert_with_fallback().
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=bytes_written) (element-type guint8) (transfer full):
Packit ae235b
 *               If the conversion was successful, a newly allocated buffer
Packit ae235b
 *               containing the converted string, which must be freed with
Packit ae235b
 *               g_free(). Otherwise %NULL and @error will be set.
Packit ae235b
 **/
Packit ae235b
gchar*
Packit ae235b
g_convert_with_iconv (const gchar *str,
Packit ae235b
		      gssize       len,
Packit ae235b
		      GIConv       converter,
Packit ae235b
		      gsize       *bytes_read, 
Packit ae235b
		      gsize       *bytes_written, 
Packit ae235b
		      GError     **error)
Packit ae235b
{
Packit ae235b
  gchar *dest;
Packit ae235b
  gchar *outp;
Packit ae235b
  const gchar *p;
Packit ae235b
  gsize inbytes_remaining;
Packit ae235b
  gsize outbytes_remaining;
Packit ae235b
  gsize err;
Packit ae235b
  gsize outbuf_size;
Packit ae235b
  gboolean have_error = FALSE;
Packit ae235b
  gboolean done = FALSE;
Packit ae235b
  gboolean reset = FALSE;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (converter != (GIConv) -1, NULL);
Packit ae235b
     
Packit ae235b
  if (len < 0)
Packit ae235b
    len = strlen (str);
Packit ae235b
Packit ae235b
  p = str;
Packit ae235b
  inbytes_remaining = len;
Packit ae235b
  outbuf_size = len + NUL_TERMINATOR_LENGTH;
Packit ae235b
  
Packit ae235b
  outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
Packit ae235b
  outp = dest = g_malloc (outbuf_size);
Packit ae235b
Packit ae235b
  while (!done && !have_error)
Packit ae235b
    {
Packit ae235b
      if (reset)
Packit ae235b
        err = g_iconv (converter, NULL, &inbytes_remaining, &outp, &outbytes_remaining);
Packit ae235b
      else
Packit ae235b
        err = g_iconv (converter, (char **)&p, &inbytes_remaining, &outp, &outbytes_remaining);
Packit ae235b
Packit ae235b
      if (err == (gsize) -1)
Packit ae235b
	{
Packit ae235b
	  switch (errno)
Packit ae235b
	    {
Packit ae235b
	    case EINVAL:
Packit ae235b
	      /* Incomplete text, do not report an error */
Packit ae235b
	      done = TRUE;
Packit ae235b
	      break;
Packit ae235b
	    case E2BIG:
Packit ae235b
	      {
Packit ae235b
		gsize used = outp - dest;
Packit ae235b
		
Packit ae235b
		outbuf_size *= 2;
Packit ae235b
		dest = g_realloc (dest, outbuf_size);
Packit ae235b
		
Packit ae235b
		outp = dest + used;
Packit ae235b
		outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
Packit ae235b
	      }
Packit ae235b
	      break;
Packit ae235b
	    case EILSEQ:
Packit ae235b
              g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                                   _("Invalid byte sequence in conversion input"));
Packit ae235b
	      have_error = TRUE;
Packit ae235b
	      break;
Packit ae235b
	    default:
Packit ae235b
              {
Packit ae235b
                int errsv = errno;
Packit ae235b
Packit ae235b
                g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                             _("Error during conversion: %s"),
Packit ae235b
                             g_strerror (errsv));
Packit ae235b
              }
Packit ae235b
	      have_error = TRUE;
Packit ae235b
	      break;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else if (err > 0)
Packit ae235b
        {
Packit ae235b
          /* @err gives the number of replacement characters used. */
Packit ae235b
          g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                               _("Unrepresentable character in conversion input"));
Packit ae235b
          have_error = TRUE;
Packit ae235b
        }
Packit ae235b
      else 
Packit ae235b
	{
Packit ae235b
	  if (!reset)
Packit ae235b
	    {
Packit ae235b
	      /* call g_iconv with NULL inbuf to cleanup shift state */
Packit ae235b
	      reset = TRUE;
Packit ae235b
	      inbytes_remaining = 0;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    done = TRUE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  memset (outp, 0, NUL_TERMINATOR_LENGTH);
Packit ae235b
  
Packit ae235b
  if (bytes_read)
Packit ae235b
    *bytes_read = p - str;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if ((p - str) != len) 
Packit ae235b
	{
Packit ae235b
          if (!have_error)
Packit ae235b
            {
Packit ae235b
              g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
Packit ae235b
                                   _("Partial character sequence at end of input"));
Packit ae235b
              have_error = TRUE;
Packit ae235b
            }
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (bytes_written)
Packit ae235b
    *bytes_written = outp - dest;	/* Doesn't include '\0' */
Packit ae235b
Packit ae235b
  if (have_error)
Packit ae235b
    {
Packit ae235b
      g_free (dest);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return dest;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_convert:
Packit ae235b
 * @str:           (array length=len) (element-type guint8):
Packit ae235b
 *                 the string to convert.
Packit ae235b
 * @len:           the length of the string in bytes, or -1 if the string is
Packit ae235b
 *                 nul-terminated (Note that some encodings may allow nul
Packit ae235b
 *                 bytes to occur inside strings. In that case, using -1
Packit ae235b
 *                 for the @len parameter is unsafe)
Packit ae235b
 * @to_codeset:    name of character set into which to convert @str
Packit ae235b
 * @from_codeset:  character set of @str.
Packit ae235b
 * @bytes_read:    (out) (optional): location to store the number of bytes in
Packit ae235b
 *                 the input string that were successfully converted, or %NULL.
Packit ae235b
 *                 Even if the conversion was successful, this may be 
Packit ae235b
 *                 less than @len if there were partial characters
Packit ae235b
 *                 at the end of the input. If the error
Packit ae235b
 *                 #G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
Packit ae235b
 *                 stored will be the byte offset after the last valid
Packit ae235b
 *                 input sequence.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in
Packit ae235b
 *                 the output buffer (not including the terminating nul).
Packit ae235b
 * @error:         location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                 errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 *
Packit ae235b
 * Converts a string from one character set to another.
Packit ae235b
 *
Packit ae235b
 * Note that you should use g_iconv() for streaming conversions. 
Packit ae235b
 * Despite the fact that @bytes_read can return information about partial
Packit ae235b
 * characters, the g_convert_... functions are not generally suitable
Packit ae235b
 * for streaming. If the underlying converter maintains internal state,
Packit ae235b
 * then this won't be preserved across successive calls to g_convert(),
Packit ae235b
 * g_convert_with_iconv() or g_convert_with_fallback(). (An example of
Packit ae235b
 * this is the GNU C converter for CP1255 which does not emit a base
Packit ae235b
 * character until it knows that the next character is not a mark that
Packit ae235b
 * could combine with the base character.)
Packit ae235b
 *
Packit ae235b
 * Using extensions such as "//TRANSLIT" may not work (or may not work
Packit ae235b
 * well) on many platforms.  Consider using g_str_to_ascii() instead.
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=bytes_written) (element-type guint8) (transfer full):
Packit ae235b
 *          If the conversion was successful, a newly allocated buffer
Packit ae235b
 *          containing the converted string, which must be freed with g_free().
Packit ae235b
 *          Otherwise %NULL and @error will be set.
Packit ae235b
 **/
Packit ae235b
gchar*
Packit ae235b
g_convert (const gchar *str,
Packit ae235b
           gssize       len,  
Packit ae235b
           const gchar *to_codeset,
Packit ae235b
           const gchar *from_codeset,
Packit ae235b
           gsize       *bytes_read, 
Packit ae235b
	   gsize       *bytes_written, 
Packit ae235b
	   GError     **error)
Packit ae235b
{
Packit ae235b
  gchar *res;
Packit ae235b
  GIConv cd;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (str != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (to_codeset != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (from_codeset != NULL, NULL);
Packit ae235b
  
Packit ae235b
  cd = open_converter (to_codeset, from_codeset, error);
Packit ae235b
Packit ae235b
  if (cd == (GIConv) -1)
Packit ae235b
    {
Packit ae235b
      if (bytes_read)
Packit ae235b
        *bytes_read = 0;
Packit ae235b
      
Packit ae235b
      if (bytes_written)
Packit ae235b
        *bytes_written = 0;
Packit ae235b
      
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  res = g_convert_with_iconv (str, len, cd,
Packit ae235b
			      bytes_read, bytes_written,
Packit ae235b
			      error);
Packit ae235b
Packit ae235b
  close_converter (cd);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_convert_with_fallback:
Packit ae235b
 * @str:          (array length=len) (element-type guint8):
Packit ae235b
 *                the string to convert.
Packit ae235b
 * @len:          the length of the string in bytes, or -1 if the string is
Packit ae235b
 *                 nul-terminated (Note that some encodings may allow nul
Packit ae235b
 *                 bytes to occur inside strings. In that case, using -1
Packit ae235b
 *                 for the @len parameter is unsafe)
Packit ae235b
 * @to_codeset:   name of character set into which to convert @str
Packit ae235b
 * @from_codeset: character set of @str.
Packit ae235b
 * @fallback:     UTF-8 string to use in place of characters not
Packit ae235b
 *                present in the target encoding. (The string must be
Packit ae235b
 *                representable in the target encoding). 
Packit ae235b
 *                If %NULL, characters not in the target encoding will 
Packit ae235b
 *                be represented as Unicode escapes \uxxxx or \Uxxxxyyyy.
Packit ae235b
 * @bytes_read:   (out) (optional): location to store the number of bytes in
Packit ae235b
 *                the input string that were successfully converted, or %NULL.
Packit ae235b
 *                Even if the conversion was successful, this may be 
Packit ae235b
 *                less than @len if there were partial characters
Packit ae235b
 *                at the end of the input.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in
Packit ae235b
 *                 the output buffer (not including the terminating nul).
Packit ae235b
 * @error:        location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 *
Packit ae235b
 * Converts a string from one character set to another, possibly
Packit ae235b
 * including fallback sequences for characters not representable
Packit ae235b
 * in the output. Note that it is not guaranteed that the specification
Packit ae235b
 * for the fallback sequences in @fallback will be honored. Some
Packit ae235b
 * systems may do an approximate conversion from @from_codeset
Packit ae235b
 * to @to_codeset in their iconv() functions, 
Packit ae235b
 * in which case GLib will simply return that approximate conversion.
Packit ae235b
 *
Packit ae235b
 * Note that you should use g_iconv() for streaming conversions. 
Packit ae235b
 * Despite the fact that @bytes_read can return information about partial
Packit ae235b
 * characters, the g_convert_... functions are not generally suitable
Packit ae235b
 * for streaming. If the underlying converter maintains internal state,
Packit ae235b
 * then this won't be preserved across successive calls to g_convert(),
Packit ae235b
 * g_convert_with_iconv() or g_convert_with_fallback(). (An example of
Packit ae235b
 * this is the GNU C converter for CP1255 which does not emit a base
Packit ae235b
 * character until it knows that the next character is not a mark that
Packit ae235b
 * could combine with the base character.)
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=bytes_written) (element-type guint8) (transfer full):
Packit ae235b
 *          If the conversion was successful, a newly allocated buffer
Packit ae235b
 *          containing the converted string, which must be freed with g_free().
Packit ae235b
 *          Otherwise %NULL and @error will be set.
Packit ae235b
 **/
Packit ae235b
gchar*
Packit ae235b
g_convert_with_fallback (const gchar *str,
Packit ae235b
			 gssize       len,    
Packit ae235b
			 const gchar *to_codeset,
Packit ae235b
			 const gchar *from_codeset,
Packit ae235b
			 const gchar *fallback,
Packit ae235b
			 gsize       *bytes_read,
Packit ae235b
			 gsize       *bytes_written,
Packit ae235b
			 GError     **error)
Packit ae235b
{
Packit ae235b
  gchar *utf8;
Packit ae235b
  gchar *dest;
Packit ae235b
  gchar *outp;
Packit ae235b
  const gchar *insert_str = NULL;
Packit ae235b
  const gchar *p;
Packit ae235b
  gsize inbytes_remaining;   
Packit ae235b
  const gchar *save_p = NULL;
Packit ae235b
  gsize save_inbytes = 0;
Packit ae235b
  gsize outbytes_remaining; 
Packit ae235b
  gsize err;
Packit ae235b
  GIConv cd;
Packit ae235b
  gsize outbuf_size;
Packit ae235b
  gboolean have_error = FALSE;
Packit ae235b
  gboolean done = FALSE;
Packit ae235b
Packit ae235b
  GError *local_error = NULL;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (str != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (to_codeset != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (from_codeset != NULL, NULL);
Packit ae235b
     
Packit ae235b
  if (len < 0)
Packit ae235b
    len = strlen (str);
Packit ae235b
  
Packit ae235b
  /* Try an exact conversion; we only proceed if this fails
Packit ae235b
   * due to an illegal sequence in the input string.
Packit ae235b
   */
Packit ae235b
  dest = g_convert (str, len, to_codeset, from_codeset, 
Packit ae235b
		    bytes_read, bytes_written, &local_error);
Packit ae235b
  if (!local_error)
Packit ae235b
    return dest;
Packit ae235b
Packit ae235b
  if (!g_error_matches (local_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
Packit ae235b
    {
Packit ae235b
      g_propagate_error (error, local_error);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_error_free (local_error);
Packit ae235b
Packit ae235b
  local_error = NULL;
Packit ae235b
  
Packit ae235b
  /* No go; to proceed, we need a converter from "UTF-8" to
Packit ae235b
   * to_codeset, and the string as UTF-8.
Packit ae235b
   */
Packit ae235b
  cd = open_converter (to_codeset, "UTF-8", error);
Packit ae235b
  if (cd == (GIConv) -1)
Packit ae235b
    {
Packit ae235b
      if (bytes_read)
Packit ae235b
        *bytes_read = 0;
Packit ae235b
      
Packit ae235b
      if (bytes_written)
Packit ae235b
        *bytes_written = 0;
Packit ae235b
      
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  utf8 = g_convert (str, len, "UTF-8", from_codeset, 
Packit ae235b
		    bytes_read, &inbytes_remaining, error);
Packit ae235b
  if (!utf8)
Packit ae235b
    {
Packit ae235b
      close_converter (cd);
Packit ae235b
      if (bytes_written)
Packit ae235b
        *bytes_written = 0;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Now the heart of the code. We loop through the UTF-8 string, and
Packit ae235b
   * whenever we hit an offending character, we form fallback, convert
Packit ae235b
   * the fallback to the target codeset, and then go back to
Packit ae235b
   * converting the original string after finishing with the fallback.
Packit ae235b
   *
Packit ae235b
   * The variables save_p and save_inbytes store the input state
Packit ae235b
   * for the original string while we are converting the fallback
Packit ae235b
   */
Packit ae235b
  p = utf8;
Packit ae235b
Packit ae235b
  outbuf_size = len + NUL_TERMINATOR_LENGTH;
Packit ae235b
  outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
Packit ae235b
  outp = dest = g_malloc (outbuf_size);
Packit ae235b
Packit ae235b
  while (!done && !have_error)
Packit ae235b
    {
Packit ae235b
      gsize inbytes_tmp = inbytes_remaining;
Packit ae235b
      err = g_iconv (cd, (char **)&p, &inbytes_tmp, &outp, &outbytes_remaining);
Packit ae235b
      inbytes_remaining = inbytes_tmp;
Packit ae235b
Packit ae235b
      if (err == (gsize) -1)
Packit ae235b
	{
Packit ae235b
	  switch (errno)
Packit ae235b
	    {
Packit ae235b
	    case EINVAL:
Packit ae235b
	      g_assert_not_reached();
Packit ae235b
	      break;
Packit ae235b
	    case E2BIG:
Packit ae235b
	      {
Packit ae235b
		gsize used = outp - dest;
Packit ae235b
Packit ae235b
		outbuf_size *= 2;
Packit ae235b
		dest = g_realloc (dest, outbuf_size);
Packit ae235b
		
Packit ae235b
		outp = dest + used;
Packit ae235b
		outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
Packit ae235b
		
Packit ae235b
		break;
Packit ae235b
	      }
Packit ae235b
	    case EILSEQ:
Packit ae235b
	      if (save_p)
Packit ae235b
		{
Packit ae235b
		  /* Error converting fallback string - fatal
Packit ae235b
		   */
Packit ae235b
		  g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
			       _("Cannot convert fallback “%s” to codeset “%s”"),
Packit ae235b
			       insert_str, to_codeset);
Packit ae235b
		  have_error = TRUE;
Packit ae235b
		  break;
Packit ae235b
		}
Packit ae235b
	      else if (p)
Packit ae235b
		{
Packit ae235b
		  if (!fallback)
Packit ae235b
		    { 
Packit ae235b
		      gunichar ch = g_utf8_get_char (p);
Packit ae235b
		      insert_str = g_strdup_printf (ch < 0x10000 ? "\\u%04x" : "\\U%08x",
Packit ae235b
						    ch);
Packit ae235b
		    }
Packit ae235b
		  else
Packit ae235b
		    insert_str = fallback;
Packit ae235b
		  
Packit ae235b
		  save_p = g_utf8_next_char (p);
Packit ae235b
		  save_inbytes = inbytes_remaining - (save_p - p);
Packit ae235b
		  p = insert_str;
Packit ae235b
		  inbytes_remaining = strlen (p);
Packit ae235b
		  break;
Packit ae235b
		}
Packit ae235b
	      /* fall thru if p is NULL */
Packit ae235b
	    default:
Packit ae235b
              {
Packit ae235b
                int errsv = errno;
Packit ae235b
Packit ae235b
                g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
Packit ae235b
                             _("Error during conversion: %s"),
Packit ae235b
                             g_strerror (errsv));
Packit ae235b
              }
Packit ae235b
Packit ae235b
	      have_error = TRUE;
Packit ae235b
	      break;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	{
Packit ae235b
	  if (save_p)
Packit ae235b
	    {
Packit ae235b
	      if (!fallback)
Packit ae235b
		g_free ((gchar *)insert_str);
Packit ae235b
	      p = save_p;
Packit ae235b
	      inbytes_remaining = save_inbytes;
Packit ae235b
	      save_p = NULL;
Packit ae235b
	    }
Packit ae235b
	  else if (p)
Packit ae235b
	    {
Packit ae235b
	      /* call g_iconv with NULL inbuf to cleanup shift state */
Packit ae235b
	      p = NULL;
Packit ae235b
	      inbytes_remaining = 0;
Packit ae235b
	    }
Packit ae235b
	  else
Packit ae235b
	    done = TRUE;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Cleanup
Packit ae235b
   */
Packit ae235b
  memset (outp, 0, NUL_TERMINATOR_LENGTH);
Packit ae235b
  
Packit ae235b
  close_converter (cd);
Packit ae235b
Packit ae235b
  if (bytes_written)
Packit ae235b
    *bytes_written = outp - dest;	/* Doesn't include '\0' */
Packit ae235b
Packit ae235b
  g_free (utf8);
Packit ae235b
Packit ae235b
  if (have_error)
Packit ae235b
    {
Packit ae235b
      if (save_p && !fallback)
Packit ae235b
	g_free ((gchar *)insert_str);
Packit ae235b
      g_free (dest);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    return dest;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_locale_to_utf8
Packit ae235b
 *
Packit ae235b
 * 
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Validate @string as UTF-8. @len can be negative if @string is
Packit ae235b
 * nul-terminated, or a non-negative value in bytes. If @string ends in an
Packit ae235b
 * incomplete sequence, or contains any illegal sequences or nul codepoints,
Packit ae235b
 * %NULL will be returned and the error set to
Packit ae235b
 * %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
Packit ae235b
 * On success, @bytes_read and @bytes_written, if provided, will be set to
Packit ae235b
 * the number of bytes in @string up to @len or the terminating nul byte.
Packit ae235b
 * On error, @bytes_read will be set to the byte offset after the last valid
Packit ae235b
 * and non-nul UTF-8 sequence in @string, and @bytes_written will be set to 0.
Packit ae235b
 */
Packit ae235b
static gchar *
Packit ae235b
strdup_len (const gchar *string,
Packit ae235b
	    gssize       len,
Packit ae235b
	    gsize       *bytes_read,
Packit ae235b
	    gsize       *bytes_written,
Packit ae235b
	    GError     **error)
Packit ae235b
{
Packit ae235b
  gsize real_len;
Packit ae235b
  const gchar *end_valid;
Packit ae235b
Packit ae235b
  if (!g_utf8_validate (string, len, &end_valid))
Packit ae235b
    {
Packit ae235b
      if (bytes_read)
Packit ae235b
	*bytes_read = end_valid - string;
Packit ae235b
      if (bytes_written)
Packit ae235b
	*bytes_written = 0;
Packit ae235b
Packit ae235b
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                           _("Invalid byte sequence in conversion input"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  real_len = end_valid - string;
Packit ae235b
Packit ae235b
  if (bytes_read)
Packit ae235b
    *bytes_read = real_len;
Packit ae235b
  if (bytes_written)
Packit ae235b
    *bytes_written = real_len;
Packit ae235b
Packit ae235b
  return g_strndup (string, real_len);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  CONVERT_CHECK_NO_NULS_IN_INPUT  = 1 << 0,
Packit ae235b
  CONVERT_CHECK_NO_NULS_IN_OUTPUT = 1 << 1
Packit ae235b
} ConvertCheckFlags;
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Convert from @string in the encoding identified by @from_codeset,
Packit ae235b
 * returning a string in the encoding identifed by @to_codeset.
Packit ae235b
 * @len can be negative if @string is nul-terminated, or a non-negative
Packit ae235b
 * value in bytes. Flags defined in #ConvertCheckFlags can be set in @flags
Packit ae235b
 * to check the input, the output, or both, for embedded nul bytes.
Packit ae235b
 * On success, @bytes_read, if provided, will be set to the number of bytes
Packit ae235b
 * in @string up to @len or the terminating nul byte, and @bytes_written, if
Packit ae235b
 * provided, will be set to the number of output bytes written into the
Packit ae235b
 * returned buffer, excluding the terminating nul sequence.
Packit ae235b
 * On error, @bytes_read will be set to the byte offset after the last valid
Packit ae235b
 * sequence in @string, and @bytes_written will be set to 0.
Packit ae235b
 */
Packit ae235b
static gchar *
Packit ae235b
convert_checked (const gchar      *string,
Packit ae235b
                 gssize            len,
Packit ae235b
                 const gchar      *to_codeset,
Packit ae235b
                 const gchar      *from_codeset,
Packit ae235b
                 ConvertCheckFlags flags,
Packit ae235b
                 gsize            *bytes_read,
Packit ae235b
                 gsize            *bytes_written,
Packit ae235b
                 GError          **error)
Packit ae235b
{
Packit ae235b
  gchar *out;
Packit ae235b
  gsize outbytes;
Packit ae235b
Packit ae235b
  if ((flags & CONVERT_CHECK_NO_NULS_IN_INPUT) && len > 0)
Packit ae235b
    {
Packit ae235b
      const gchar *early_nul = memchr (string, '\0', len);
Packit ae235b
      if (early_nul != NULL)
Packit ae235b
        {
Packit ae235b
          if (bytes_read)
Packit ae235b
            *bytes_read = early_nul - string;
Packit ae235b
          if (bytes_written)
Packit ae235b
            *bytes_written = 0;
Packit ae235b
Packit ae235b
          g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                               _("Embedded NUL byte in conversion input"));
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  out = g_convert (string, len, to_codeset, from_codeset,
Packit ae235b
                   bytes_read, &outbytes, error);
Packit ae235b
  if (out == NULL)
Packit ae235b
    {
Packit ae235b
      if (bytes_written)
Packit ae235b
        *bytes_written = 0;
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((flags & CONVERT_CHECK_NO_NULS_IN_OUTPUT)
Packit ae235b
      && memchr (out, '\0', outbytes) != NULL)
Packit ae235b
    {
Packit ae235b
      g_free (out);
Packit ae235b
      if (bytes_written)
Packit ae235b
        *bytes_written = 0;
Packit ae235b
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_EMBEDDED_NUL,
Packit ae235b
                           _("Embedded NUL byte in conversion output"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (bytes_written)
Packit ae235b
    *bytes_written = outbytes;
Packit ae235b
  return out;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_locale_to_utf8:
Packit ae235b
 * @opsysstring:   (array length=len) (element-type guint8): a string in the
Packit ae235b
 *                 encoding of the current locale. On Windows
Packit ae235b
 *                 this means the system codepage.
Packit ae235b
 * @len:           the length of the string, or -1 if the string is
Packit ae235b
 *                 nul-terminated (Note that some encodings may allow nul
Packit ae235b
 *                 bytes to occur inside strings. In that case, using -1
Packit ae235b
 *                 for the @len parameter is unsafe)
Packit ae235b
 * @bytes_read: (out) (optional): location to store the number of bytes in the
Packit ae235b
 *                 input string that were successfully converted, or %NULL.
Packit ae235b
 *                 Even if the conversion was successful, this may be 
Packit ae235b
 *                 less than @len if there were partial characters
Packit ae235b
 *                 at the end of the input. If the error
Packit ae235b
 *                 %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
Packit ae235b
 *                 stored will be the byte offset after the last valid
Packit ae235b
 *                 input sequence.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in the output
Packit ae235b
 *                 buffer (not including the terminating nul).
Packit ae235b
 * @error:         location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                 errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 * 
Packit ae235b
 * Converts a string which is in the encoding used for strings by
Packit ae235b
 * the C runtime (usually the same as that used by the operating
Packit ae235b
 * system) in the [current locale][setlocale] into a UTF-8 string.
Packit ae235b
 *
Packit ae235b
 * If the source encoding is not UTF-8 and the conversion output contains a
Packit ae235b
 * nul character, the error %G_CONVERT_ERROR_EMBEDDED_NUL is set and the
Packit ae235b
 * function returns %NULL.
Packit ae235b
 * If the source encoding is UTF-8, an embedded nul character is treated with
Packit ae235b
 * the %G_CONVERT_ERROR_ILLEGAL_SEQUENCE error for backward compatibility with
Packit ae235b
 * earlier versions of this library. Use g_convert() to produce output that
Packit ae235b
 * may contain embedded nul characters.
Packit ae235b
 * 
Packit ae235b
 * Returns: (type utf8): The converted string, or %NULL on an error.
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_locale_to_utf8 (const gchar  *opsysstring,
Packit ae235b
		  gssize        len,            
Packit ae235b
		  gsize        *bytes_read,    
Packit ae235b
		  gsize        *bytes_written,
Packit ae235b
		  GError      **error)
Packit ae235b
{
Packit ae235b
  const char *charset;
Packit ae235b
Packit ae235b
  if (g_get_charset (&charset))
Packit ae235b
    return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
Packit ae235b
  else
Packit ae235b
    return convert_checked (opsysstring, len, "UTF-8", charset,
Packit ae235b
                            CONVERT_CHECK_NO_NULS_IN_OUTPUT,
Packit ae235b
                            bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_locale_from_utf8:
Packit ae235b
 * @utf8string:    a UTF-8 encoded string 
Packit ae235b
 * @len:           the length of the string, or -1 if the string is
Packit ae235b
 *                 nul-terminated.
Packit ae235b
 * @bytes_read: (out) (optional): location to store the number of bytes in the
Packit ae235b
 *                 input string that were successfully converted, or %NULL.
Packit ae235b
 *                 Even if the conversion was successful, this may be 
Packit ae235b
 *                 less than @len if there were partial characters
Packit ae235b
 *                 at the end of the input. If the error
Packit ae235b
 *                 %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
Packit ae235b
 *                 stored will be the byte offset after the last valid
Packit ae235b
 *                 input sequence.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in the output
Packit ae235b
 *                 buffer (not including the terminating nul).
Packit ae235b
 * @error:         location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                 errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 * 
Packit ae235b
 * Converts a string from UTF-8 to the encoding used for strings by
Packit ae235b
 * the C runtime (usually the same as that used by the operating
Packit ae235b
 * system) in the [current locale][setlocale]. On Windows this means
Packit ae235b
 * the system codepage.
Packit ae235b
 *
Packit ae235b
 * The input string shall not contain nul characters even if the @len
Packit ae235b
 * argument is positive. A nul character found inside the string will result
Packit ae235b
 * in error %G_CONVERT_ERROR_ILLEGAL_SEQUENCE. Use g_convert() to convert
Packit ae235b
 * input that may contain embedded nul characters.
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=bytes_written) (element-type guint8) (transfer full):
Packit ae235b
 *          A newly-allocated buffer containing the converted string,
Packit ae235b
 *          or %NULL on an error, and error will be set.
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_locale_from_utf8 (const gchar *utf8string,
Packit ae235b
		    gssize       len,            
Packit ae235b
		    gsize       *bytes_read,    
Packit ae235b
		    gsize       *bytes_written,
Packit ae235b
		    GError     **error)
Packit ae235b
{
Packit ae235b
  const gchar *charset;
Packit ae235b
Packit ae235b
  if (g_get_charset (&charset))
Packit ae235b
    return strdup_len (utf8string, len, bytes_read, bytes_written, error);
Packit ae235b
  else
Packit ae235b
    return convert_checked (utf8string, len, charset, "UTF-8",
Packit ae235b
                            CONVERT_CHECK_NO_NULS_IN_INPUT,
Packit ae235b
                            bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef G_PLATFORM_WIN32
Packit ae235b
Packit ae235b
typedef struct _GFilenameCharsetCache GFilenameCharsetCache;
Packit ae235b
Packit ae235b
struct _GFilenameCharsetCache {
Packit ae235b
  gboolean is_utf8;
Packit ae235b
  gchar *charset;
Packit ae235b
  gchar **filename_charsets;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
filename_charset_cache_free (gpointer data)
Packit ae235b
{
Packit ae235b
  GFilenameCharsetCache *cache = data;
Packit ae235b
  g_free (cache->charset);
Packit ae235b
  g_strfreev (cache->filename_charsets);
Packit ae235b
  g_free (cache);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_filename_charsets:
Packit ae235b
 * @filename_charsets: (out) (transfer none) (array zero-terminated=1):
Packit ae235b
 *    return location for the %NULL-terminated list of encoding names
Packit ae235b
 *
Packit ae235b
 * Determines the preferred character sets used for filenames.
Packit ae235b
 * The first character set from the @charsets is the filename encoding, the
Packit ae235b
 * subsequent character sets are used when trying to generate a displayable
Packit ae235b
 * representation of a filename, see g_filename_display_name().
Packit ae235b
 *
Packit ae235b
 * On Unix, the character sets are determined by consulting the
Packit ae235b
 * environment variables `G_FILENAME_ENCODING` and `G_BROKEN_FILENAMES`.
Packit ae235b
 * On Windows, the character set used in the GLib API is always UTF-8
Packit ae235b
 * and said environment variables have no effect.
Packit ae235b
 *
Packit ae235b
 * `G_FILENAME_ENCODING` may be set to a comma-separated list of
Packit ae235b
 * character set names. The special token "\@locale" is taken
Packit ae235b
 * to  mean the character set for the [current locale][setlocale].
Packit ae235b
 * If `G_FILENAME_ENCODING` is not set, but `G_BROKEN_FILENAMES` is,
Packit ae235b
 * the character set of the current locale is taken as the filename
Packit ae235b
 * encoding. If neither environment variable  is set, UTF-8 is taken
Packit ae235b
 * as the filename encoding, but the character set of the current locale
Packit ae235b
 * is also put in the list of encodings.
Packit ae235b
 *
Packit ae235b
 * The returned @charsets belong to GLib and must not be freed.
Packit ae235b
 *
Packit ae235b
 * Note that on Unix, regardless of the locale character set or
Packit ae235b
 * `G_FILENAME_ENCODING` value, the actual file names present 
Packit ae235b
 * on a system might be in any random encoding or just gibberish.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the filename encoding is UTF-8.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_get_filename_charsets (const gchar ***filename_charsets)
Packit ae235b
{
Packit ae235b
  static GPrivate cache_private = G_PRIVATE_INIT (filename_charset_cache_free);
Packit ae235b
  GFilenameCharsetCache *cache = g_private_get (&cache_private);
Packit ae235b
  const gchar *charset;
Packit ae235b
Packit ae235b
  if (!cache)
Packit ae235b
    {
Packit ae235b
      cache = g_new0 (GFilenameCharsetCache, 1);
Packit ae235b
      g_private_set (&cache_private, cache);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_get_charset (&charset);
Packit ae235b
Packit ae235b
  if (!(cache->charset && strcmp (cache->charset, charset) == 0))
Packit ae235b
    {
Packit ae235b
      const gchar *new_charset;
Packit ae235b
      gchar *p;
Packit ae235b
      gint i;
Packit ae235b
Packit ae235b
      g_free (cache->charset);
Packit ae235b
      g_strfreev (cache->filename_charsets);
Packit ae235b
      cache->charset = g_strdup (charset);
Packit ae235b
      
Packit ae235b
      p = getenv ("G_FILENAME_ENCODING");
Packit ae235b
      if (p != NULL && p[0] != '\0') 
Packit ae235b
	{
Packit ae235b
	  cache->filename_charsets = g_strsplit (p, ",", 0);
Packit ae235b
	  cache->is_utf8 = (strcmp (cache->filename_charsets[0], "UTF-8") == 0);
Packit ae235b
Packit ae235b
	  for (i = 0; cache->filename_charsets[i]; i++)
Packit ae235b
	    {
Packit ae235b
	      if (strcmp ("@locale", cache->filename_charsets[i]) == 0)
Packit ae235b
		{
Packit ae235b
		  g_get_charset (&new_charset);
Packit ae235b
		  g_free (cache->filename_charsets[i]);
Packit ae235b
		  cache->filename_charsets[i] = g_strdup (new_charset);
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      else if (getenv ("G_BROKEN_FILENAMES") != NULL)
Packit ae235b
	{
Packit ae235b
	  cache->filename_charsets = g_new0 (gchar *, 2);
Packit ae235b
	  cache->is_utf8 = g_get_charset (&new_charset);
Packit ae235b
	  cache->filename_charsets[0] = g_strdup (new_charset);
Packit ae235b
	}
Packit ae235b
      else 
Packit ae235b
	{
Packit ae235b
	  cache->filename_charsets = g_new0 (gchar *, 3);
Packit ae235b
	  cache->is_utf8 = TRUE;
Packit ae235b
	  cache->filename_charsets[0] = g_strdup ("UTF-8");
Packit ae235b
	  if (!g_get_charset (&new_charset))
Packit ae235b
	    cache->filename_charsets[1] = g_strdup (new_charset);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (filename_charsets)
Packit ae235b
    *filename_charsets = (const gchar **)cache->filename_charsets;
Packit ae235b
Packit ae235b
  return cache->is_utf8;
Packit ae235b
}
Packit ae235b
Packit ae235b
#else /* G_PLATFORM_WIN32 */
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
g_get_filename_charsets (const gchar ***filename_charsets) 
Packit ae235b
{
Packit ae235b
  static const gchar *charsets[] = {
Packit ae235b
    "UTF-8",
Packit ae235b
    NULL
Packit ae235b
  };
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  /* On Windows GLib pretends that the filename charset is UTF-8 */
Packit ae235b
  if (filename_charsets)
Packit ae235b
    *filename_charsets = charsets;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
#else
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  /* Cygwin works like before */
Packit ae235b
  result = g_get_charset (&(charsets[0]));
Packit ae235b
Packit ae235b
  if (filename_charsets)
Packit ae235b
    *filename_charsets = charsets;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* G_PLATFORM_WIN32 */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_filename_charset (const gchar **filename_charset)
Packit ae235b
{
Packit ae235b
  const gchar **charsets;
Packit ae235b
  gboolean is_utf8;
Packit ae235b
  
Packit ae235b
  is_utf8 = g_get_filename_charsets (&charsets);
Packit ae235b
Packit ae235b
  if (filename_charset)
Packit ae235b
    *filename_charset = charsets[0];
Packit ae235b
  
Packit ae235b
  return is_utf8;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_filename_to_utf8:
Packit ae235b
 * @opsysstring: (type filename): a string in the encoding for filenames
Packit ae235b
 * @len:           the length of the string, or -1 if the string is
Packit ae235b
 *                 nul-terminated (Note that some encodings may allow nul
Packit ae235b
 *                 bytes to occur inside strings. In that case, using -1
Packit ae235b
 *                 for the @len parameter is unsafe)
Packit ae235b
 * @bytes_read: (out) (optional): location to store the number of bytes in the
Packit ae235b
 *                 input string that were successfully converted, or %NULL.
Packit ae235b
 *                 Even if the conversion was successful, this may be 
Packit ae235b
 *                 less than @len if there were partial characters
Packit ae235b
 *                 at the end of the input. If the error
Packit ae235b
 *                 %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
Packit ae235b
 *                 stored will be the byte offset after the last valid
Packit ae235b
 *                 input sequence.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in the output
Packit ae235b
 *                 buffer (not including the terminating nul).
Packit ae235b
 * @error:         location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                 errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 * 
Packit ae235b
 * Converts a string which is in the encoding used by GLib for
Packit ae235b
 * filenames into a UTF-8 string. Note that on Windows GLib uses UTF-8
Packit ae235b
 * for filenames; on other platforms, this function indirectly depends on 
Packit ae235b
 * the [current locale][setlocale].
Packit ae235b
 *
Packit ae235b
 * The input string shall not contain nul characters even if the @len
Packit ae235b
 * argument is positive. A nul character found inside the string will result
Packit ae235b
 * in error %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
Packit ae235b
 * If the source encoding is not UTF-8 and the conversion output contains a
Packit ae235b
 * nul character, the error %G_CONVERT_ERROR_EMBEDDED_NUL is set and the
Packit ae235b
 * function returns %NULL. Use g_convert() to produce output that
Packit ae235b
 * may contain embedded nul characters.
Packit ae235b
 * 
Packit ae235b
 * Returns: (type utf8): The converted string, or %NULL on an error.
Packit ae235b
 **/
Packit ae235b
gchar*
Packit ae235b
g_filename_to_utf8 (const gchar *opsysstring, 
Packit ae235b
		    gssize       len,           
Packit ae235b
		    gsize       *bytes_read,   
Packit ae235b
		    gsize       *bytes_written,
Packit ae235b
		    GError     **error)
Packit ae235b
{
Packit ae235b
  const gchar *charset;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (opsysstring != NULL, NULL);
Packit ae235b
Packit ae235b
  if (get_filename_charset (&charset))
Packit ae235b
    return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
Packit ae235b
  else
Packit ae235b
    return convert_checked (opsysstring, len, "UTF-8", charset,
Packit ae235b
                            CONVERT_CHECK_NO_NULS_IN_INPUT |
Packit ae235b
                            CONVERT_CHECK_NO_NULS_IN_OUTPUT,
Packit ae235b
                            bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_filename_from_utf8:
Packit ae235b
 * @utf8string:    (type utf8): a UTF-8 encoded string.
Packit ae235b
 * @len:           the length of the string, or -1 if the string is
Packit ae235b
 *                 nul-terminated.
Packit ae235b
 * @bytes_read:    (out) (optional): location to store the number of bytes in
Packit ae235b
 *                 the input string that were successfully converted, or %NULL.
Packit ae235b
 *                 Even if the conversion was successful, this may be 
Packit ae235b
 *                 less than @len if there were partial characters
Packit ae235b
 *                 at the end of the input. If the error
Packit ae235b
 *                 %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
Packit ae235b
 *                 stored will be the byte offset after the last valid
Packit ae235b
 *                 input sequence.
Packit ae235b
 * @bytes_written: (out) (optional): the number of bytes stored in
Packit ae235b
 *                 the output buffer (not including the terminating nul).
Packit ae235b
 * @error:         location to store the error occurring, or %NULL to ignore
Packit ae235b
 *                 errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 * 
Packit ae235b
 * Converts a string from UTF-8 to the encoding GLib uses for
Packit ae235b
 * filenames. Note that on Windows GLib uses UTF-8 for filenames;
Packit ae235b
 * on other platforms, this function indirectly depends on the 
Packit ae235b
 * [current locale][setlocale].
Packit ae235b
 *
Packit ae235b
 * The input string shall not contain nul characters even if the @len
Packit ae235b
 * argument is positive. A nul character found inside the string will result
Packit ae235b
 * in error %G_CONVERT_ERROR_ILLEGAL_SEQUENCE. If the filename encoding is
Packit ae235b
 * not UTF-8 and the conversion output contains a nul character, the error
Packit ae235b
 * %G_CONVERT_ERROR_EMBEDDED_NUL is set and the function returns %NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename):
Packit ae235b
 *               The converted string, or %NULL on an error.
Packit ae235b
 **/
Packit ae235b
gchar*
Packit ae235b
g_filename_from_utf8 (const gchar *utf8string,
Packit ae235b
		      gssize       len,            
Packit ae235b
		      gsize       *bytes_read,    
Packit ae235b
		      gsize       *bytes_written,
Packit ae235b
		      GError     **error)
Packit ae235b
{
Packit ae235b
  const gchar *charset;
Packit ae235b
Packit ae235b
  if (get_filename_charset (&charset))
Packit ae235b
    return strdup_len (utf8string, len, bytes_read, bytes_written, error);
Packit ae235b
  else
Packit ae235b
    return convert_checked (utf8string, len, charset, "UTF-8",
Packit ae235b
                            CONVERT_CHECK_NO_NULS_IN_INPUT |
Packit ae235b
                            CONVERT_CHECK_NO_NULS_IN_OUTPUT,
Packit ae235b
                            bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Test of haystack has the needle prefix, comparing case
Packit ae235b
 * insensitive. haystack may be UTF-8, but needle must
Packit ae235b
 * contain only ascii. */
Packit ae235b
static gboolean
Packit ae235b
has_case_prefix (const gchar *haystack, const gchar *needle)
Packit ae235b
{
Packit ae235b
  const gchar *h, *n;
Packit ae235b
  
Packit ae235b
  /* Eat one character at a time. */
Packit ae235b
  h = haystack;
Packit ae235b
  n = needle;
Packit ae235b
Packit ae235b
  while (*n && *h &&
Packit ae235b
	 g_ascii_tolower (*n) == g_ascii_tolower (*h))
Packit ae235b
    {
Packit ae235b
      n++;
Packit ae235b
      h++;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return *n == '\0';
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef enum {
Packit ae235b
  UNSAFE_ALL        = 0x1,  /* Escape all unsafe characters   */
Packit ae235b
  UNSAFE_ALLOW_PLUS = 0x2,  /* Allows '+'  */
Packit ae235b
  UNSAFE_PATH       = 0x8,  /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */
Packit ae235b
  UNSAFE_HOST       = 0x10, /* Allows '/' and ':' and '@' */
Packit ae235b
  UNSAFE_SLASHES    = 0x20  /* Allows all characters except for '/' and '%' */
Packit ae235b
} UnsafeCharacterSet;
Packit ae235b
Packit ae235b
static const guchar acceptable[96] = {
Packit ae235b
  /* A table of the ASCII chars from space (32) to DEL (127) */
Packit ae235b
  /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */ 
Packit ae235b
  0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
Packit ae235b
  /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
Packit ae235b
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
Packit ae235b
  /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
Packit ae235b
  0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
Packit ae235b
  /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
Packit ae235b
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
Packit ae235b
  /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
Packit ae235b
  0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
Packit ae235b
  /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
Packit ae235b
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
Packit ae235b
};
Packit ae235b
Packit ae235b
static const gchar hex[16] = "0123456789ABCDEF";
Packit ae235b
Packit ae235b
/* Note: This escape function works on file: URIs, but if you want to
Packit ae235b
 * escape something else, please read RFC-2396 */
Packit ae235b
static gchar *
Packit ae235b
g_escape_uri_string (const gchar *string, 
Packit ae235b
		     UnsafeCharacterSet mask)
Packit ae235b
{
Packit ae235b
#define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
Packit ae235b
Packit ae235b
  const gchar *p;
Packit ae235b
  gchar *q;
Packit ae235b
  gchar *result;
Packit ae235b
  int c;
Packit ae235b
  gint unacceptable;
Packit ae235b
  UnsafeCharacterSet use_mask;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (mask == UNSAFE_ALL
Packit ae235b
			|| mask == UNSAFE_ALLOW_PLUS
Packit ae235b
			|| mask == UNSAFE_PATH
Packit ae235b
			|| mask == UNSAFE_HOST
Packit ae235b
			|| mask == UNSAFE_SLASHES, NULL);
Packit ae235b
  
Packit ae235b
  unacceptable = 0;
Packit ae235b
  use_mask = mask;
Packit ae235b
  for (p = string; *p != '\0'; p++)
Packit ae235b
    {
Packit ae235b
      c = (guchar) *p;
Packit ae235b
      if (!ACCEPTABLE (c)) 
Packit ae235b
	unacceptable++;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  result = g_malloc (p - string + unacceptable * 2 + 1);
Packit ae235b
  
Packit ae235b
  use_mask = mask;
Packit ae235b
  for (q = result, p = string; *p != '\0'; p++)
Packit ae235b
    {
Packit ae235b
      c = (guchar) *p;
Packit ae235b
      
Packit ae235b
      if (!ACCEPTABLE (c))
Packit ae235b
	{
Packit ae235b
	  *q++ = '%'; /* means hex coming */
Packit ae235b
	  *q++ = hex[c >> 4];
Packit ae235b
	  *q++ = hex[c & 15];
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	*q++ = *p;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  *q = '\0';
Packit ae235b
  
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_escape_file_uri (const gchar *hostname,
Packit ae235b
		   const gchar *pathname)
Packit ae235b
{
Packit ae235b
  char *escaped_hostname = NULL;
Packit ae235b
  char *escaped_path;
Packit ae235b
  char *res;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  char *p, *backslash;
Packit ae235b
Packit ae235b
  /* Turn backslashes into forward slashes. That's what Netscape
Packit ae235b
   * does, and they are actually more or less equivalent in Windows.
Packit ae235b
   */
Packit ae235b
  
Packit ae235b
  pathname = g_strdup (pathname);
Packit ae235b
  p = (char *) pathname;
Packit ae235b
  
Packit ae235b
  while ((backslash = strchr (p, '\\')) != NULL)
Packit ae235b
    {
Packit ae235b
      *backslash = '/';
Packit ae235b
      p = backslash + 1;
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (hostname && *hostname != '\0')
Packit ae235b
    {
Packit ae235b
      escaped_hostname = g_escape_uri_string (hostname, UNSAFE_HOST);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  escaped_path = g_escape_uri_string (pathname, UNSAFE_PATH);
Packit ae235b
Packit ae235b
  res = g_strconcat ("file://",
Packit ae235b
		     (escaped_hostname) ? escaped_hostname : "",
Packit ae235b
		     (*escaped_path != '/') ? "/" : "",
Packit ae235b
		     escaped_path,
Packit ae235b
		     NULL);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  g_free ((char *) pathname);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_free (escaped_hostname);
Packit ae235b
  g_free (escaped_path);
Packit ae235b
  
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
unescape_character (const char *scanner)
Packit ae235b
{
Packit ae235b
  int first_digit;
Packit ae235b
  int second_digit;
Packit ae235b
Packit ae235b
  first_digit = g_ascii_xdigit_value (scanner[0]);
Packit ae235b
  if (first_digit < 0) 
Packit ae235b
    return -1;
Packit ae235b
  
Packit ae235b
  second_digit = g_ascii_xdigit_value (scanner[1]);
Packit ae235b
  if (second_digit < 0) 
Packit ae235b
    return -1;
Packit ae235b
  
Packit ae235b
  return (first_digit << 4) | second_digit;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_unescape_uri_string (const char *escaped,
Packit ae235b
		       int         len,
Packit ae235b
		       const char *illegal_escaped_characters,
Packit ae235b
		       gboolean    ascii_must_not_be_escaped)
Packit ae235b
{
Packit ae235b
  const gchar *in, *in_end;
Packit ae235b
  gchar *out, *result;
Packit ae235b
  int c;
Packit ae235b
  
Packit ae235b
  if (escaped == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (len < 0)
Packit ae235b
    len = strlen (escaped);
Packit ae235b
Packit ae235b
  result = g_malloc (len + 1);
Packit ae235b
  
Packit ae235b
  out = result;
Packit ae235b
  for (in = escaped, in_end = escaped + len; in < in_end; in++)
Packit ae235b
    {
Packit ae235b
      c = *in;
Packit ae235b
Packit ae235b
      if (c == '%')
Packit ae235b
	{
Packit ae235b
	  /* catch partial escape sequences past the end of the substring */
Packit ae235b
	  if (in + 3 > in_end)
Packit ae235b
	    break;
Packit ae235b
Packit ae235b
	  c = unescape_character (in + 1);
Packit ae235b
Packit ae235b
	  /* catch bad escape sequences and NUL characters */
Packit ae235b
	  if (c <= 0)
Packit ae235b
	    break;
Packit ae235b
Packit ae235b
	  /* catch escaped ASCII */
Packit ae235b
	  if (ascii_must_not_be_escaped && c <= 0x7F)
Packit ae235b
	    break;
Packit ae235b
Packit ae235b
	  /* catch other illegal escaped characters */
Packit ae235b
	  if (strchr (illegal_escaped_characters, c) != NULL)
Packit ae235b
	    break;
Packit ae235b
Packit ae235b
	  in += 2;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      *out++ = c;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  g_assert (out - result <= len);
Packit ae235b
  *out = '\0';
Packit ae235b
Packit ae235b
  if (in != in_end)
Packit ae235b
    {
Packit ae235b
      g_free (result);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_asciialphanum (gunichar c)
Packit ae235b
{
Packit ae235b
  return c <= 0x7F && g_ascii_isalnum (c);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_asciialpha (gunichar c)
Packit ae235b
{
Packit ae235b
  return c <= 0x7F && g_ascii_isalpha (c);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* allows an empty string */
Packit ae235b
static gboolean
Packit ae235b
hostname_validate (const char *hostname)
Packit ae235b
{
Packit ae235b
  const char *p;
Packit ae235b
  gunichar c, first_char, last_char;
Packit ae235b
Packit ae235b
  p = hostname;
Packit ae235b
  if (*p == '\0')
Packit ae235b
    return TRUE;
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      /* read in a label */
Packit ae235b
      c = g_utf8_get_char (p);
Packit ae235b
      p = g_utf8_next_char (p);
Packit ae235b
      if (!is_asciialphanum (c))
Packit ae235b
	return FALSE;
Packit ae235b
      first_char = c;
Packit ae235b
      do
Packit ae235b
	{
Packit ae235b
	  last_char = c;
Packit ae235b
	  c = g_utf8_get_char (p);
Packit ae235b
	  p = g_utf8_next_char (p);
Packit ae235b
	}
Packit ae235b
      while (is_asciialphanum (c) || c == '-');
Packit ae235b
      if (last_char == '-')
Packit ae235b
	return FALSE;
Packit ae235b
      
Packit ae235b
      /* if that was the last label, check that it was a toplabel */
Packit ae235b
      if (c == '\0' || (c == '.' && *p == '\0'))
Packit ae235b
	return is_asciialpha (first_char);
Packit ae235b
    }
Packit ae235b
  while (c == '.');
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_filename_from_uri:
Packit ae235b
 * @uri: a uri describing a filename (escaped, encoded in ASCII).
Packit ae235b
 * @hostname: (out) (optional) (nullable): Location to store hostname for the URI.
Packit ae235b
 *            If there is no hostname in the URI, %NULL will be
Packit ae235b
 *            stored in this location.
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *         errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 * 
Packit ae235b
 * Converts an escaped ASCII-encoded URI to a local filename in the
Packit ae235b
 * encoding used for filenames. 
Packit ae235b
 * 
Packit ae235b
 * Returns: (type filename): a newly-allocated string holding
Packit ae235b
 *               the resulting filename, or %NULL on an error.
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_filename_from_uri (const gchar *uri,
Packit ae235b
		     gchar      **hostname,
Packit ae235b
		     GError     **error)
Packit ae235b
{
Packit ae235b
  const char *path_part;
Packit ae235b
  const char *host_part;
Packit ae235b
  char *unescaped_hostname;
Packit ae235b
  char *result;
Packit ae235b
  char *filename;
Packit ae235b
  int offs;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  char *p, *slash;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (hostname)
Packit ae235b
    *hostname = NULL;
Packit ae235b
Packit ae235b
  if (!has_case_prefix (uri, "file:/"))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
Packit ae235b
		   _("The URI “%s” is not an absolute URI using the “file” scheme"),
Packit ae235b
		   uri);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  path_part = uri + strlen ("file:");
Packit ae235b
  
Packit ae235b
  if (strchr (path_part, '#') != NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
Packit ae235b
		   _("The local file URI “%s” may not include a “#”"),
Packit ae235b
		   uri);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
	
Packit ae235b
  if (has_case_prefix (path_part, "///")) 
Packit ae235b
    path_part += 2;
Packit ae235b
  else if (has_case_prefix (path_part, "//"))
Packit ae235b
    {
Packit ae235b
      path_part += 2;
Packit ae235b
      host_part = path_part;
Packit ae235b
Packit ae235b
      path_part = strchr (path_part, '/');
Packit ae235b
Packit ae235b
      if (path_part == NULL)
Packit ae235b
	{
Packit ae235b
	  g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
Packit ae235b
		       _("The URI “%s” is invalid"),
Packit ae235b
		       uri);
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
Packit ae235b
      unescaped_hostname = g_unescape_uri_string (host_part, path_part - host_part, "", TRUE);
Packit ae235b
Packit ae235b
      if (unescaped_hostname == NULL ||
Packit ae235b
	  !hostname_validate (unescaped_hostname))
Packit ae235b
	{
Packit ae235b
	  g_free (unescaped_hostname);
Packit ae235b
	  g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
Packit ae235b
		       _("The hostname of the URI “%s” is invalid"),
Packit ae235b
		       uri);
Packit ae235b
	  return NULL;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      if (hostname)
Packit ae235b
	*hostname = unescaped_hostname;
Packit ae235b
      else
Packit ae235b
	g_free (unescaped_hostname);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  filename = g_unescape_uri_string (path_part, -1, "/", FALSE);
Packit ae235b
Packit ae235b
  if (filename == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
Packit ae235b
		   _("The URI “%s” contains invalidly escaped characters"),
Packit ae235b
		   uri);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  offs = 0;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  /* Drop localhost */
Packit ae235b
  if (hostname && *hostname != NULL &&
Packit ae235b
      g_ascii_strcasecmp (*hostname, "localhost") == 0)
Packit ae235b
    {
Packit ae235b
      g_free (*hostname);
Packit ae235b
      *hostname = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Turn slashes into backslashes, because that's the canonical spelling */
Packit ae235b
  p = filename;
Packit ae235b
  while ((slash = strchr (p, '/')) != NULL)
Packit ae235b
    {
Packit ae235b
      *slash = '\\';
Packit ae235b
      p = slash + 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Windows URIs with a drive letter can be like "file://host/c:/foo"
Packit ae235b
   * or "file://host/c|/foo" (some Netscape versions). In those cases, start
Packit ae235b
   * the filename from the drive letter.
Packit ae235b
   */
Packit ae235b
  if (g_ascii_isalpha (filename[1]))
Packit ae235b
    {
Packit ae235b
      if (filename[2] == ':')
Packit ae235b
	offs = 1;
Packit ae235b
      else if (filename[2] == '|')
Packit ae235b
	{
Packit ae235b
	  filename[2] = ':';
Packit ae235b
	  offs = 1;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  result = g_strdup (filename + offs);
Packit ae235b
  g_free (filename);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_filename_to_uri:
Packit ae235b
 * @filename: (type filename): an absolute filename specified in the GLib file
Packit ae235b
 *     name encoding, which is the on-disk file name bytes on Unix, and UTF-8
Packit ae235b
 *     on Windows
Packit ae235b
 * @hostname: (nullable): A UTF-8 encoded hostname, or %NULL for none.
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *         errors. Any of the errors in #GConvertError may occur.
Packit ae235b
 * 
Packit ae235b
 * Converts an absolute filename to an escaped ASCII-encoded URI, with the path
Packit ae235b
 * component following Section 3.3. of RFC 2396.
Packit ae235b
 * 
Packit ae235b
 * Returns: a newly-allocated string holding the resulting
Packit ae235b
 *               URI, or %NULL on an error.
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_filename_to_uri (const gchar *filename,
Packit ae235b
		   const gchar *hostname,
Packit ae235b
		   GError     **error)
Packit ae235b
{
Packit ae235b
  char *escaped_uri;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
Packit ae235b
  if (!g_path_is_absolute (filename))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
Packit ae235b
		   _("The pathname “%s” is not an absolute path"),
Packit ae235b
		   filename);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (hostname &&
Packit ae235b
      !(g_utf8_validate (hostname, -1, NULL)
Packit ae235b
	&& hostname_validate (hostname)))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
Packit ae235b
                           _("Invalid hostname"));
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  /* Don't use localhost unnecessarily */
Packit ae235b
  if (hostname && g_ascii_strcasecmp (hostname, "localhost") == 0)
Packit ae235b
    hostname = NULL;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  escaped_uri = g_escape_file_uri (hostname, filename);
Packit ae235b
Packit ae235b
  return escaped_uri;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_uri_list_extract_uris:
Packit ae235b
 * @uri_list: an URI list 
Packit ae235b
 *
Packit ae235b
 * Splits an URI list conforming to the text/uri-list
Packit ae235b
 * mime type defined in RFC 2483 into individual URIs,
Packit ae235b
 * discarding any comments. The URIs are not validated.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a newly allocated %NULL-terminated list
Packit ae235b
 *   of strings holding the individual URIs. The array should be freed
Packit ae235b
 *   with g_strfreev().
Packit ae235b
 *
Packit ae235b
 * Since: 2.6
Packit ae235b
 */
Packit ae235b
gchar **
Packit ae235b
g_uri_list_extract_uris (const gchar *uri_list)
Packit ae235b
{
Packit ae235b
  GSList *uris, *u;
Packit ae235b
  const gchar *p, *q;
Packit ae235b
  gchar **result;
Packit ae235b
  gint n_uris = 0;
Packit ae235b
Packit ae235b
  uris = NULL;
Packit ae235b
Packit ae235b
  p = uri_list;
Packit ae235b
Packit ae235b
  /* We don't actually try to validate the URI according to RFC
Packit ae235b
   * 2396, or even check for allowed characters - we just ignore
Packit ae235b
   * comments and trim whitespace off the ends.  We also
Packit ae235b
   * allow LF delimination as well as the specified CRLF.
Packit ae235b
   *
Packit ae235b
   * We do allow comments like specified in RFC 2483.
Packit ae235b
   */
Packit ae235b
  while (p)
Packit ae235b
    {
Packit ae235b
      if (*p != '#')
Packit ae235b
	{
Packit ae235b
	  while (g_ascii_isspace (*p))
Packit ae235b
	    p++;
Packit ae235b
Packit ae235b
	  q = p;
Packit ae235b
	  while (*q && (*q != '\n') && (*q != '\r'))
Packit ae235b
	    q++;
Packit ae235b
Packit ae235b
	  if (q > p)
Packit ae235b
	    {
Packit ae235b
	      q--;
Packit ae235b
	      while (q > p && g_ascii_isspace (*q))
Packit ae235b
		q--;
Packit ae235b
Packit ae235b
	      if (q > p)
Packit ae235b
		{
Packit ae235b
		  uris = g_slist_prepend (uris, g_strndup (p, q - p + 1));
Packit ae235b
		  n_uris++;
Packit ae235b
		}
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      p = strchr (p, '\n');
Packit ae235b
      if (p)
Packit ae235b
	p++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  result = g_new (gchar *, n_uris + 1);
Packit ae235b
Packit ae235b
  result[n_uris--] = NULL;
Packit ae235b
  for (u = uris; u; u = u->next)
Packit ae235b
    result[n_uris--] = u->data;
Packit ae235b
Packit ae235b
  g_slist_free (uris);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_filename_display_basename:
Packit ae235b
 * @filename: (type filename): an absolute pathname in the
Packit ae235b
 *     GLib file name encoding
Packit ae235b
 *
Packit ae235b
 * Returns the display basename for the particular filename, guaranteed
Packit ae235b
 * to be valid UTF-8. The display name might not be identical to the filename,
Packit ae235b
 * for instance there might be problems converting it to UTF-8, and some files
Packit ae235b
 * can be translated in the display.
Packit ae235b
 *
Packit ae235b
 * If GLib cannot make sense of the encoding of @filename, as a last resort it 
Packit ae235b
 * replaces unknown characters with U+FFFD, the Unicode replacement character.
Packit ae235b
 * You can search the result for the UTF-8 encoding of this character (which is
Packit ae235b
 * "\357\277\275" in octal notation) to find out if @filename was in an invalid
Packit ae235b
 * encoding.
Packit ae235b
 *
Packit ae235b
 * You must pass the whole absolute pathname to this functions so that
Packit ae235b
 * translation of well known locations can be done.
Packit ae235b
 *
Packit ae235b
 * This function is preferred over g_filename_display_name() if you know the
Packit ae235b
 * whole path, as it allows translation.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string containing
Packit ae235b
 *   a rendition of the basename of the filename in valid UTF-8
Packit ae235b
 *
Packit ae235b
 * Since: 2.6
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_filename_display_basename (const gchar *filename)
Packit ae235b
{
Packit ae235b
  char *basename;
Packit ae235b
  char *display_name;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
  
Packit ae235b
  basename = g_path_get_basename (filename);
Packit ae235b
  display_name = g_filename_display_name (basename);
Packit ae235b
  g_free (basename);
Packit ae235b
  return display_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_filename_display_name:
Packit ae235b
 * @filename: (type filename): a pathname hopefully in the
Packit ae235b
 *     GLib file name encoding
Packit ae235b
 * 
Packit ae235b
 * Converts a filename into a valid UTF-8 string. The conversion is 
Packit ae235b
 * not necessarily reversible, so you should keep the original around 
Packit ae235b
 * and use the return value of this function only for display purposes.
Packit ae235b
 * Unlike g_filename_to_utf8(), the result is guaranteed to be non-%NULL 
Packit ae235b
 * even if the filename actually isn't in the GLib file name encoding.
Packit ae235b
 *
Packit ae235b
 * If GLib cannot make sense of the encoding of @filename, as a last resort it 
Packit ae235b
 * replaces unknown characters with U+FFFD, the Unicode replacement character.
Packit ae235b
 * You can search the result for the UTF-8 encoding of this character (which is
Packit ae235b
 * "\357\277\275" in octal notation) to find out if @filename was in an invalid
Packit ae235b
 * encoding.
Packit ae235b
 *
Packit ae235b
 * If you know the whole pathname of the file you should use
Packit ae235b
 * g_filename_display_basename(), since that allows location-based
Packit ae235b
 * translation of filenames.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated string containing
Packit ae235b
 *   a rendition of the filename in valid UTF-8
Packit ae235b
 *
Packit ae235b
 * Since: 2.6
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_filename_display_name (const gchar *filename)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
  const gchar **charsets;
Packit ae235b
  gchar *display_name = NULL;
Packit ae235b
  gboolean is_utf8;
Packit ae235b
 
Packit ae235b
  is_utf8 = g_get_filename_charsets (&charsets);
Packit ae235b
Packit ae235b
  if (is_utf8)
Packit ae235b
    {
Packit ae235b
      if (g_utf8_validate (filename, -1, NULL))
Packit ae235b
	display_name = g_strdup (filename);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (!display_name)
Packit ae235b
    {
Packit ae235b
      /* Try to convert from the filename charsets to UTF-8.
Packit ae235b
       * Skip the first charset if it is UTF-8.
Packit ae235b
       */
Packit ae235b
      for (i = is_utf8 ? 1 : 0; charsets[i]; i++)
Packit ae235b
	{
Packit ae235b
	  display_name = g_convert (filename, -1, "UTF-8", charsets[i], 
Packit ae235b
				    NULL, NULL, NULL);
Packit ae235b
Packit ae235b
	  if (display_name)
Packit ae235b
	    break;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* if all conversions failed, we replace invalid UTF-8
Packit ae235b
   * by a question mark
Packit ae235b
   */
Packit ae235b
  if (!display_name) 
Packit ae235b
    display_name = g_utf8_make_valid (filename, -1);
Packit ae235b
Packit ae235b
  return display_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/* Binary compatibility versions. Not for newly compiled code. */
Packit ae235b
Packit ae235b
_GLIB_EXTERN gchar *g_filename_to_utf8_utf8   (const gchar  *opsysstring,
Packit ae235b
                                               gssize        len,
Packit ae235b
                                               gsize        *bytes_read,
Packit ae235b
                                               gsize        *bytes_written,
Packit ae235b
                                               GError      **error) G_GNUC_MALLOC;
Packit ae235b
_GLIB_EXTERN gchar *g_filename_from_utf8_utf8 (const gchar  *utf8string,
Packit ae235b
                                               gssize        len,
Packit ae235b
                                               gsize        *bytes_read,
Packit ae235b
                                               gsize        *bytes_written,
Packit ae235b
                                               GError      **error) G_GNUC_MALLOC;
Packit ae235b
_GLIB_EXTERN gchar *g_filename_from_uri_utf8  (const gchar  *uri,
Packit ae235b
                                               gchar       **hostname,
Packit ae235b
                                               GError      **error) G_GNUC_MALLOC;
Packit ae235b
_GLIB_EXTERN gchar *g_filename_to_uri_utf8    (const gchar  *filename,
Packit ae235b
                                               const gchar  *hostname,
Packit ae235b
                                               GError      **error) G_GNUC_MALLOC;
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_filename_to_utf8_utf8 (const gchar *opsysstring,
Packit ae235b
                         gssize       len,
Packit ae235b
                         gsize       *bytes_read,
Packit ae235b
                         gsize       *bytes_written,
Packit ae235b
                         GError     **error)
Packit ae235b
{
Packit ae235b
  return g_filename_to_utf8 (opsysstring, len, bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_filename_from_utf8_utf8 (const gchar *utf8string,
Packit ae235b
                           gssize       len,
Packit ae235b
                           gsize       *bytes_read,
Packit ae235b
                           gsize       *bytes_written,
Packit ae235b
                           GError     **error)
Packit ae235b
{
Packit ae235b
  return g_filename_from_utf8 (utf8string, len, bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_filename_from_uri_utf8 (const gchar *uri,
Packit ae235b
                          gchar      **hostname,
Packit ae235b
                          GError     **error)
Packit ae235b
{
Packit ae235b
  return g_filename_from_uri (uri, hostname, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
gchar *
Packit ae235b
g_filename_to_uri_utf8 (const gchar *filename,
Packit ae235b
                        const gchar *hostname,
Packit ae235b
                        GError     **error)
Packit ae235b
{
Packit ae235b
  return g_filename_to_uri (filename, hostname, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif