2000-09-11 05:03:18 +02:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
*
|
|
|
|
* gconvert.c: Convert between character sets using iconv
|
|
|
|
* Copyright Red Hat Inc., 2000
|
2006-12-15 05:35:13 +01:00
|
|
|
* Authors: Havoc Pennington <hp@redhat.com>, Owen Taylor <otaylor@redhat.com>
|
2000-09-11 05:03:18 +02:00
|
|
|
*
|
2022-05-18 10:15:38 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2000-09-11 05:03:18 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2000-09-11 05:03:18 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2000-09-11 05:03:18 +02:00
|
|
|
*/
|
|
|
|
|
2002-12-04 02:27:44 +01:00
|
|
|
#include "config.h"
|
2010-09-06 14:56:16 +02:00
|
|
|
#include "glibconfig.h"
|
2002-02-18 00:28:43 +01:00
|
|
|
|
2007-11-26 00:27:45 +01:00
|
|
|
#ifndef G_OS_WIN32
|
2000-09-11 05:03:18 +02:00
|
|
|
#include <iconv.h>
|
2007-11-26 00:27:45 +01:00
|
|
|
#endif
|
2000-09-11 05:03:18 +02:00
|
|
|
#include <errno.h>
|
2002-01-29 19:25:45 +01:00
|
|
|
#include <stdio.h>
|
2000-09-11 05:03:18 +02:00
|
|
|
#include <string.h>
|
2000-11-12 22:23:55 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2008-04-02 04:43:45 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include "win_iconv.c"
|
|
|
|
#endif
|
|
|
|
|
2001-03-09 22:18:33 +01:00
|
|
|
#ifdef G_PLATFORM_WIN32
|
|
|
|
#define STRICT
|
2000-11-12 22:23:55 +01:00
|
|
|
#include <windows.h>
|
2001-03-09 22:18:33 +01:00
|
|
|
#undef STRICT
|
2000-11-12 22:23:55 +01:00
|
|
|
#endif
|
2000-09-11 05:03:18 +02:00
|
|
|
|
2010-09-04 05:03:14 +02:00
|
|
|
#include "gconvert.h"
|
2020-12-01 12:47:27 +01:00
|
|
|
#include "gconvertprivate.h"
|
2010-09-04 05:03:14 +02:00
|
|
|
|
2012-11-02 00:38:44 +01:00
|
|
|
#include "gcharsetprivate.h"
|
2010-09-04 05:03:14 +02:00
|
|
|
#include "gslist.h"
|
|
|
|
#include "gstrfuncs.h"
|
|
|
|
#include "gtestutils.h"
|
|
|
|
#include "gthread.h"
|
2018-12-06 16:16:33 +01:00
|
|
|
#include "gthreadprivate.h"
|
2010-09-04 05:03:14 +02:00
|
|
|
#include "gunicode.h"
|
2011-10-17 07:30:09 +02:00
|
|
|
#include "gfileutils.h"
|
2020-07-23 09:50:39 +02:00
|
|
|
#include "genviron.h"
|
2010-09-04 05:03:14 +02:00
|
|
|
|
2001-01-16 03:24:24 +01:00
|
|
|
#include "glibintl.h"
|
2000-10-19 17:21:04 +02:00
|
|
|
|
2005-03-14 05:26:57 +01:00
|
|
|
|
2010-08-09 04:11:38 +02:00
|
|
|
/**
|
|
|
|
* SECTION:conversions
|
|
|
|
* @title: Character Set Conversion
|
2011-11-13 03:54:42 +01:00
|
|
|
* @short_description: convert strings between different character sets
|
2010-08-09 04:11:38 +02:00
|
|
|
*
|
2014-02-01 16:10:19 +01:00
|
|
|
* The g_convert() family of function wraps the functionality of iconv().
|
|
|
|
* In addition to pure character set conversions, GLib has functions to
|
|
|
|
* deal with the extra complications of encodings for file names.
|
2010-08-09 04:11:38 +02:00
|
|
|
*
|
2014-02-01 16:10:19 +01:00
|
|
|
* ## File Name Encodings
|
|
|
|
*
|
|
|
|
* Historically, UNIX has not had a defined encoding for file names:
|
|
|
|
* a file name is valid as long as it does not have path separators
|
|
|
|
* in it ("/"). However, displaying file names may require conversion:
|
|
|
|
* from the character set in which they were created, to the character
|
|
|
|
* set in which the application operates. Consider the Spanish file name
|
2018-01-22 13:47:42 +01:00
|
|
|
* "Presentación.sxi". If the application which created it uses
|
2014-02-01 16:10:19 +01:00
|
|
|
* ISO-8859-1 for its encoding,
|
2014-02-08 21:19:13 +01:00
|
|
|
* |[
|
2015-11-16 13:16:08 +01:00
|
|
|
* Character: P r e s e n t a c i ó n . s x i
|
2010-08-09 04:11:38 +02:00
|
|
|
* Hex code: 50 72 65 73 65 6e 74 61 63 69 f3 6e 2e 73 78 69
|
2014-02-08 21:19:13 +01:00
|
|
|
* ]|
|
2010-08-09 04:11:38 +02:00
|
|
|
* However, if the application use UTF-8, the actual file name on
|
|
|
|
* disk would look like this:
|
2014-02-08 21:19:13 +01:00
|
|
|
* |[
|
2015-11-16 13:16:08 +01:00
|
|
|
* Character: P r e s e n t a c i ó n . s x i
|
2010-08-09 04:11:38 +02:00
|
|
|
* Hex code: 50 72 65 73 65 6e 74 61 63 69 c3 b3 6e 2e 73 78 69
|
2014-02-08 21:19:13 +01:00
|
|
|
* ]|
|
2014-02-01 16:10:19 +01:00
|
|
|
* Glib uses UTF-8 for its strings, and GUI toolkits like GTK+ that use
|
2018-01-22 13:48:43 +01:00
|
|
|
* GLib do the same thing. If you get a file name from the file system,
|
2014-02-01 16:10:19 +01:00
|
|
|
* for example, from readdir() or from g_dir_read_name(), and you wish
|
|
|
|
* to display the file name to the user, you will need to convert it
|
|
|
|
* into UTF-8. The opposite case is when the user types the name of a
|
2018-01-22 13:48:43 +01:00
|
|
|
* file they wish to save: the toolkit will give you that string in
|
2014-02-01 16:10:19 +01:00
|
|
|
* UTF-8 encoding, and you will need to convert it to the character
|
|
|
|
* set used for file names before you can create the file with open()
|
|
|
|
* or fopen().
|
|
|
|
*
|
2018-01-22 13:48:43 +01:00
|
|
|
* By default, GLib assumes that file names on disk are in UTF-8
|
2014-02-01 16:10:19 +01:00
|
|
|
* encoding. This is a valid assumption for file systems which
|
|
|
|
* were created relatively recently: most applications use UTF-8
|
2010-08-09 04:11:38 +02:00
|
|
|
* encoding for their strings, and that is also what they use for
|
2014-02-01 16:10:19 +01:00
|
|
|
* the file names they create. However, older file systems may
|
2010-08-09 04:11:38 +02:00
|
|
|
* still contain file names created in "older" encodings, such as
|
2014-02-08 18:26:56 +01:00
|
|
|
* ISO-8859-1. In this case, for compatibility reasons, you may want
|
2018-01-22 13:48:43 +01:00
|
|
|
* to instruct GLib to use that particular encoding for file names
|
2014-02-08 18:26:56 +01:00
|
|
|
* rather than UTF-8. You can do this by specifying the encoding for
|
|
|
|
* file names in the [`G_FILENAME_ENCODING`][G_FILENAME_ENCODING]
|
2014-02-01 16:10:19 +01:00
|
|
|
* environment variable. For example, if your installation uses
|
2018-01-22 13:48:43 +01:00
|
|
|
* ISO-8859-1 for file names, you can put this in your `~/.profile`:
|
2014-02-08 18:26:56 +01:00
|
|
|
* |[
|
2010-08-09 04:11:38 +02:00
|
|
|
* export G_FILENAME_ENCODING=ISO-8859-1
|
2014-02-08 18:26:56 +01:00
|
|
|
* ]|
|
2018-01-22 13:48:43 +01:00
|
|
|
* GLib provides the functions g_filename_to_utf8() and
|
2014-02-01 16:10:19 +01:00
|
|
|
* g_filename_from_utf8() to perform the necessary conversions.
|
|
|
|
* These functions convert file names from the encoding specified
|
2014-02-08 18:26:56 +01:00
|
|
|
* in `G_FILENAME_ENCODING` to UTF-8 and vice-versa. This
|
|
|
|
* [diagram][file-name-encodings-diagram] illustrates how
|
2010-08-09 04:11:38 +02:00
|
|
|
* these functions are used to convert between UTF-8 and the
|
|
|
|
* encoding for file names in the file system.
|
2014-02-01 16:10:19 +01:00
|
|
|
*
|
2014-02-06 04:02:24 +01:00
|
|
|
* ## Conversion between file name encodings # {#file-name-encodings-diagram)
|
|
|
|
*
|
|
|
|
* ![](file-name-encodings.png)
|
2014-02-01 16:10:19 +01:00
|
|
|
*
|
|
|
|
* ## Checklist for Application Writers
|
|
|
|
*
|
2010-08-09 04:11:38 +02:00
|
|
|
* This section is a practical summary of the detailed
|
|
|
|
* things to do to make sure your applications process file
|
|
|
|
* name encodings correctly.
|
2014-02-01 16:10:19 +01:00
|
|
|
*
|
|
|
|
* 1. If you get a file name from the file system from a function
|
|
|
|
* such as readdir() or gtk_file_chooser_get_filename(), you do
|
|
|
|
* not need to do any conversion to pass that file name to
|
|
|
|
* functions like open(), rename(), or fopen() -- those are "raw"
|
|
|
|
* file names which the file system understands.
|
|
|
|
*
|
|
|
|
* 2. If you need to display a file name, convert it to UTF-8 first
|
|
|
|
* by using g_filename_to_utf8(). If conversion fails, display a
|
|
|
|
* string like "Unknown file name". Do not convert this string back
|
|
|
|
* into the encoding used for file names if you wish to pass it to
|
|
|
|
* the file system; use the original file name instead.
|
|
|
|
*
|
|
|
|
* For example, the document window of a word processor could display
|
|
|
|
* "Unknown file name" in its title bar but still let the user save
|
2014-02-06 01:32:41 +01:00
|
|
|
* the file, as it would keep the raw file name internally. This
|
|
|
|
* can happen if the user has not set the `G_FILENAME_ENCODING`
|
2021-11-13 03:32:21 +01:00
|
|
|
* environment variable even though they have files whose names are
|
2014-02-06 01:32:41 +01:00
|
|
|
* not encoded in UTF-8.
|
2014-02-01 16:10:19 +01:00
|
|
|
*
|
|
|
|
* 3. If your user interface lets the user type a file name for saving
|
|
|
|
* or renaming, convert it to the encoding used for file names in
|
|
|
|
* the file system by using g_filename_from_utf8(). Pass the converted
|
|
|
|
* file name to functions like fopen(). If conversion fails, ask the
|
|
|
|
* user to enter a different file name. This can happen if the user
|
2014-02-06 14:04:52 +01:00
|
|
|
* types Japanese characters when `G_FILENAME_ENCODING` is set to
|
|
|
|
* `ISO-8859-1`, for example.
|
2010-08-09 04:11:38 +02:00
|
|
|
*/
|
|
|
|
|
2009-10-28 19:18:20 +01:00
|
|
|
/* We try to terminate strings in unknown charsets with this many zero bytes
|
|
|
|
* to ensure that multibyte strings really are nul-terminated when we return
|
|
|
|
* them from g_convert() and friends.
|
|
|
|
*/
|
|
|
|
#define NUL_TERMINATOR_LENGTH 4
|
|
|
|
|
2012-08-28 19:15:56 +02:00
|
|
|
G_DEFINE_QUARK (g_convert_error, g_convert_error)
|
2000-09-11 05:03:18 +02:00
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
static gboolean
|
|
|
|
try_conversion (const char *to_codeset,
|
|
|
|
const char *from_codeset,
|
|
|
|
iconv_t *cd)
|
|
|
|
{
|
|
|
|
*cd = iconv_open (to_codeset, from_codeset);
|
|
|
|
|
|
|
|
if (*cd == (iconv_t)-1 && errno == EINVAL)
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
try_to_aliases (const char **to_aliases,
|
|
|
|
const char *from_codeset,
|
|
|
|
iconv_t *cd)
|
|
|
|
{
|
|
|
|
if (to_aliases)
|
|
|
|
{
|
|
|
|
const char **p = to_aliases;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (try_conversion (*p, from_codeset, cd))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-04-16 22:05:25 +02:00
|
|
|
/**
|
2015-10-06 14:33:03 +02:00
|
|
|
* g_iconv_open: (skip)
|
2001-04-16 22:05:25 +02:00
|
|
|
* @to_codeset: destination codeset
|
|
|
|
* @from_codeset: source codeset
|
|
|
|
*
|
2003-07-25 23:32:47 +02:00
|
|
|
* Same as the standard UNIX routine iconv_open(), but
|
Revert mistaken change: it is UNIX, not Unix.
* glib/tmpl/completion.sgml, glib/tmpl/date.sgml,
glib/tmpl/fileutils.sgml, glib/tmpl/iochannels.sgml,
glib/tmpl/macros.sgml, glib/tmpl/memory.sgml,
glib/tmpl/misc_utils.sgml, glib/tmpl/warnings.sgml,
glib/tmpl/windows.sgml: Revert mistaken change: it is UNIX, not Unix.
* glib/tmpl/memory_chunks.sgml,
glib/tmpl/date.sgml, glib/tmpl/threads.sgml,
glib/tmpl/gtype.sgml: Trivial markup fixes.
* glib/gconvert.c, glib/giochannel.c, glib/gmain.c,
glib/gwin32.c: Revert mistaken change: it is UNIX, not Unix.
2001-12-13 23:24:18 +01:00
|
|
|
* may be implemented via libiconv on UNIX flavors that lack
|
2001-04-16 22:05:25 +02:00
|
|
|
* a native implementation.
|
|
|
|
*
|
2001-12-20 05:49:44 +01:00
|
|
|
* GLib provides g_convert() and g_locale_to_utf8() which are likely
|
2001-04-16 22:05:25 +02:00
|
|
|
* more convenient than the raw iconv wrappers.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a "conversion descriptor", or (GIConv)-1 if
|
2002-07-26 00:35:17 +02:00
|
|
|
* opening the converter failed.
|
2001-04-16 22:05:25 +02:00
|
|
|
**/
|
2000-11-13 00:44:28 +01:00
|
|
|
GIConv
|
|
|
|
g_iconv_open (const gchar *to_codeset,
|
|
|
|
const gchar *from_codeset)
|
|
|
|
{
|
2001-09-27 04:49:05 +02:00
|
|
|
iconv_t cd;
|
2000-11-13 00:44:28 +01:00
|
|
|
|
2001-09-27 04:49:05 +02:00
|
|
|
if (!try_conversion (to_codeset, from_codeset, &cd))
|
|
|
|
{
|
|
|
|
const char **to_aliases = _g_charset_get_aliases (to_codeset);
|
2001-10-17 18:58:26 +02:00
|
|
|
const char **from_aliases = _g_charset_get_aliases (from_codeset);
|
2001-09-27 04:49:05 +02:00
|
|
|
|
|
|
|
if (from_aliases)
|
|
|
|
{
|
|
|
|
const char **p = from_aliases;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (try_conversion (to_codeset, *p, &cd))
|
2002-07-26 00:35:17 +02:00
|
|
|
goto out;
|
2001-09-27 04:49:05 +02:00
|
|
|
|
|
|
|
if (try_to_aliases (to_aliases, *p, &cd))
|
2002-07-26 00:35:17 +02:00
|
|
|
goto out;
|
2001-09-27 04:49:05 +02:00
|
|
|
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (try_to_aliases (to_aliases, from_codeset, &cd))
|
2002-07-26 00:35:17 +02:00
|
|
|
goto out;
|
2001-09-27 04:49:05 +02:00
|
|
|
}
|
|
|
|
|
2002-07-26 01:58:00 +02:00
|
|
|
out:
|
2002-07-26 00:35:17 +02:00
|
|
|
return (cd == (iconv_t)-1) ? (GIConv)-1 : (GIConv)cd;
|
2000-11-13 00:44:28 +01:00
|
|
|
}
|
|
|
|
|
2001-04-16 22:05:25 +02:00
|
|
|
/**
|
2015-10-06 14:33:03 +02:00
|
|
|
* g_iconv: (skip)
|
2001-04-16 22:05:25 +02:00
|
|
|
* @converter: conversion descriptor from g_iconv_open()
|
|
|
|
* @inbuf: bytes to convert
|
2022-04-24 21:15:53 +02:00
|
|
|
* @inbytes_left: (inout): inout parameter, bytes remaining to convert in @inbuf
|
2001-04-16 22:05:25 +02:00
|
|
|
* @outbuf: converted output bytes
|
2022-04-24 21:15:53 +02:00
|
|
|
* @outbytes_left: (inout): inout parameter, bytes available to fill in @outbuf
|
2001-04-16 22:05:25 +02:00
|
|
|
*
|
2003-07-25 23:32:47 +02:00
|
|
|
* Same as the standard UNIX routine iconv(), but
|
Revert mistaken change: it is UNIX, not Unix.
* glib/tmpl/completion.sgml, glib/tmpl/date.sgml,
glib/tmpl/fileutils.sgml, glib/tmpl/iochannels.sgml,
glib/tmpl/macros.sgml, glib/tmpl/memory.sgml,
glib/tmpl/misc_utils.sgml, glib/tmpl/warnings.sgml,
glib/tmpl/windows.sgml: Revert mistaken change: it is UNIX, not Unix.
* glib/tmpl/memory_chunks.sgml,
glib/tmpl/date.sgml, glib/tmpl/threads.sgml,
glib/tmpl/gtype.sgml: Trivial markup fixes.
* glib/gconvert.c, glib/giochannel.c, glib/gmain.c,
glib/gwin32.c: Revert mistaken change: it is UNIX, not Unix.
2001-12-13 23:24:18 +01:00
|
|
|
* may be implemented via libiconv on UNIX flavors that lack
|
2001-04-16 22:05:25 +02:00
|
|
|
* a native implementation.
|
|
|
|
*
|
2001-12-20 05:49:44 +01:00
|
|
|
* GLib provides g_convert() and g_locale_to_utf8() which are likely
|
2001-04-16 22:05:25 +02:00
|
|
|
* more convenient than the raw iconv wrappers.
|
|
|
|
*
|
gconvert: Fix error handling for g_iconv() with unrepresentable chars
The behaviour of upstream iconv() when faced with a character which is
valid in the input encoding, but not representable in the output
encoding, is implementation defined:
http://pubs.opengroup.org/onlinepubs/9699919799/
Specifically:
If iconv() encounters a character in the input buffer that is valid,
but for which an identical character does not exist in the target
codeset, iconv() shall perform an implementation-defined conversion
on this character.
This behaviour was being exposed in our g_iconv() wrapper and also in
g_convert_with_iconv() — but users of g_convert_with_iconv() (both the
GLib unit tests, and the implementation of g_convert_with_fallback())
were assuming that iconv() would return EILSEQ if faced with an
unrepresentable character.
On platforms like NetBSD, this is not the case: NetBSD’s iconv()
finishes the conversion successfully, and outputs a string containing
replacement characters. It signals those replacements in its return
value from iconv(), which is positive (specifically, non-zero) in such a
case.
Let’s codify the existing assumed behaviour of g_convert_with_iconv(),
documenting that it will return G_CONVERT_ERROR_INVALID_SEQUENCE if
faced with an unrepresentable character. As g_iconv() is a thin wrapper
around iconv(), leave the behaviour there implementation-defined (but
document it as such).
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://bugzilla.gnome.org/show_bug.cgi?id=790698
2018-01-22 13:50:15 +01:00
|
|
|
* Note that the behaviour of iconv() for characters which are valid in the
|
|
|
|
* input character set, but which have no representation in the output character
|
|
|
|
* set, is implementation defined. This function may return success (with a
|
|
|
|
* positive number of non-reversible conversions as replacement characters were
|
|
|
|
* used), or it may return -1 and set an error such as %EILSEQ, in such a
|
|
|
|
* situation.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: count of non-reversible conversions, or -1 on error
|
2001-04-16 22:05:25 +02:00
|
|
|
**/
|
2006-12-28 05:48:06 +01:00
|
|
|
gsize
|
2000-11-13 00:44:28 +01:00
|
|
|
g_iconv (GIConv converter,
|
|
|
|
gchar **inbuf,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gsize *inbytes_left,
|
2000-11-13 00:44:28 +01:00
|
|
|
gchar **outbuf,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gsize *outbytes_left)
|
2000-11-13 00:44:28 +01:00
|
|
|
{
|
|
|
|
iconv_t cd = (iconv_t)converter;
|
|
|
|
|
|
|
|
return iconv (cd, inbuf, inbytes_left, outbuf, outbytes_left);
|
|
|
|
}
|
|
|
|
|
2001-04-16 22:05:25 +02:00
|
|
|
/**
|
2015-10-06 14:33:03 +02:00
|
|
|
* g_iconv_close: (skip)
|
2001-04-16 22:05:25 +02:00
|
|
|
* @converter: a conversion descriptor from g_iconv_open()
|
|
|
|
*
|
2003-07-25 23:32:47 +02:00
|
|
|
* Same as the standard UNIX routine iconv_close(), but
|
Revert mistaken change: it is UNIX, not Unix.
* glib/tmpl/completion.sgml, glib/tmpl/date.sgml,
glib/tmpl/fileutils.sgml, glib/tmpl/iochannels.sgml,
glib/tmpl/macros.sgml, glib/tmpl/memory.sgml,
glib/tmpl/misc_utils.sgml, glib/tmpl/warnings.sgml,
glib/tmpl/windows.sgml: Revert mistaken change: it is UNIX, not Unix.
* glib/tmpl/memory_chunks.sgml,
glib/tmpl/date.sgml, glib/tmpl/threads.sgml,
glib/tmpl/gtype.sgml: Trivial markup fixes.
* glib/gconvert.c, glib/giochannel.c, glib/gmain.c,
glib/gwin32.c: Revert mistaken change: it is UNIX, not Unix.
2001-12-13 23:24:18 +01:00
|
|
|
* may be implemented via libiconv on UNIX flavors that lack
|
2001-04-16 22:05:25 +02:00
|
|
|
* a native implementation. Should be called to clean up
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* the conversion descriptor from g_iconv_open() when
|
2001-04-16 22:05:25 +02:00
|
|
|
* you are done converting things.
|
|
|
|
*
|
2001-12-20 05:49:44 +01:00
|
|
|
* GLib provides g_convert() and g_locale_to_utf8() which are likely
|
2001-04-16 22:05:25 +02:00
|
|
|
* more convenient than the raw iconv wrappers.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: -1 on error, 0 on success
|
2001-04-16 22:05:25 +02:00
|
|
|
**/
|
2000-11-13 00:44:28 +01:00
|
|
|
gint
|
|
|
|
g_iconv_close (GIConv converter)
|
|
|
|
{
|
|
|
|
iconv_t cd = (iconv_t)converter;
|
|
|
|
|
|
|
|
return iconv_close (cd);
|
|
|
|
}
|
|
|
|
|
2005-08-08 21:50:38 +02:00
|
|
|
static GIConv
|
|
|
|
open_converter (const gchar *to_codeset,
|
|
|
|
const gchar *from_codeset,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GIConv cd;
|
|
|
|
|
|
|
|
cd = g_iconv_open (to_codeset, from_codeset);
|
|
|
|
|
|
|
|
if (cd == (GIConv) -1)
|
|
|
|
{
|
|
|
|
/* Something went wrong. */
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
if (errno == EINVAL)
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("Conversion from character set “%s” to “%s” is not supported"),
|
2005-08-08 21:50:38 +02:00
|
|
|
from_codeset, to_codeset);
|
|
|
|
else
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("Could not open converter from “%s” to “%s”"),
|
2005-08-08 21:50:38 +02:00
|
|
|
from_codeset, to_codeset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
close_converter (GIConv cd)
|
|
|
|
{
|
|
|
|
if (cd == (GIConv) -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return g_iconv_close (cd);
|
|
|
|
}
|
|
|
|
|
2001-08-25 05:22:16 +02:00
|
|
|
/**
|
2015-10-06 14:33:03 +02:00
|
|
|
* g_convert_with_iconv: (skip)
|
2018-01-05 23:40:12 +01:00
|
|
|
* @str: (array length=len) (element-type guint8):
|
|
|
|
* the string to convert.
|
2014-05-30 00:13:37 +02:00
|
|
|
* @len: the length of the string in bytes, or -1 if the string is
|
2014-02-01 16:47:09 +01:00
|
|
|
* nul-terminated (Note that some encodings may allow nul
|
|
|
|
* bytes to occur inside strings. In that case, using -1
|
|
|
|
* for the @len parameter is unsafe)
|
2001-08-25 05:22:16 +02:00
|
|
|
* @converter: conversion descriptor from g_iconv_open()
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in
|
|
|
|
* the input string that were successfully converted, or %NULL.
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Even if the conversion was successful, this may be
|
2001-10-01 19:35:18 +02:00
|
|
|
* less than @len if there were partial characters
|
2001-08-25 05:22:16 +02:00
|
|
|
* at the end of the input. If the error
|
2022-03-04 17:21:55 +01:00
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
|
2018-02-13 15:49:20 +01:00
|
|
|
* stored will be the byte offset after the last valid
|
2001-08-25 05:22:16 +02:00
|
|
|
* input sequence.
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in
|
|
|
|
* the output buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-08-25 05:22:16 +02:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
2005-08-02 16:58:20 +02:00
|
|
|
* Converts a string from one character set to another.
|
|
|
|
*
|
2014-02-01 16:47:09 +01:00
|
|
|
* Note that you should use g_iconv() for streaming conversions.
|
2018-01-11 12:54:14 +01:00
|
|
|
* Despite the fact that @bytes_read can return information about partial
|
2014-02-01 16:47:09 +01:00
|
|
|
* characters, the g_convert_... functions are not generally suitable
|
|
|
|
* for streaming. If the underlying converter maintains internal state,
|
|
|
|
* then this won't be preserved across successive calls to g_convert(),
|
|
|
|
* g_convert_with_iconv() or g_convert_with_fallback(). (An example of
|
|
|
|
* this is the GNU C converter for CP1255 which does not emit a base
|
|
|
|
* character until it knows that the next character is not a mark that
|
|
|
|
* could combine with the base character.)
|
2001-08-25 05:22:16 +02:00
|
|
|
*
|
gconvert: Fix error handling for g_iconv() with unrepresentable chars
The behaviour of upstream iconv() when faced with a character which is
valid in the input encoding, but not representable in the output
encoding, is implementation defined:
http://pubs.opengroup.org/onlinepubs/9699919799/
Specifically:
If iconv() encounters a character in the input buffer that is valid,
but for which an identical character does not exist in the target
codeset, iconv() shall perform an implementation-defined conversion
on this character.
This behaviour was being exposed in our g_iconv() wrapper and also in
g_convert_with_iconv() — but users of g_convert_with_iconv() (both the
GLib unit tests, and the implementation of g_convert_with_fallback())
were assuming that iconv() would return EILSEQ if faced with an
unrepresentable character.
On platforms like NetBSD, this is not the case: NetBSD’s iconv()
finishes the conversion successfully, and outputs a string containing
replacement characters. It signals those replacements in its return
value from iconv(), which is positive (specifically, non-zero) in such a
case.
Let’s codify the existing assumed behaviour of g_convert_with_iconv(),
documenting that it will return G_CONVERT_ERROR_INVALID_SEQUENCE if
faced with an unrepresentable character. As g_iconv() is a thin wrapper
around iconv(), leave the behaviour there implementation-defined (but
document it as such).
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://bugzilla.gnome.org/show_bug.cgi?id=790698
2018-01-22 13:50:15 +01:00
|
|
|
* Characters which are valid in the input character set, but which have no
|
|
|
|
* representation in the output character set will result in a
|
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE error. This is in contrast to the iconv()
|
|
|
|
* specification, which leaves this behaviour implementation defined. Note that
|
|
|
|
* this is the same error code as is returned for an invalid byte sequence in
|
|
|
|
* the input character set. To get defined behaviour for conversion of
|
|
|
|
* unrepresentable characters, use g_convert_with_fallback().
|
|
|
|
*
|
2018-01-05 23:40:12 +01:00
|
|
|
* Returns: (array length=bytes_written) (element-type guint8) (transfer full):
|
|
|
|
* If the conversion was successful, a newly allocated buffer
|
|
|
|
* containing the converted string, which must be freed with
|
2009-10-28 19:18:20 +01:00
|
|
|
* g_free(). Otherwise %NULL and @error will be set.
|
2001-08-25 05:22:16 +02:00
|
|
|
**/
|
|
|
|
gchar*
|
|
|
|
g_convert_with_iconv (const gchar *str,
|
|
|
|
gssize len,
|
|
|
|
GIConv converter,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar *dest;
|
|
|
|
gchar *outp;
|
|
|
|
const gchar *p;
|
|
|
|
gsize inbytes_remaining;
|
|
|
|
gsize outbytes_remaining;
|
|
|
|
gsize err;
|
|
|
|
gsize outbuf_size;
|
|
|
|
gboolean have_error = FALSE;
|
2005-08-02 16:58:20 +02:00
|
|
|
gboolean done = FALSE;
|
2007-11-08 07:04:00 +01:00
|
|
|
gboolean reset = FALSE;
|
2001-08-25 05:22:16 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (converter != (GIConv) -1, NULL);
|
|
|
|
|
2000-09-11 05:03:18 +02:00
|
|
|
if (len < 0)
|
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
inbytes_remaining = len;
|
2009-10-28 19:18:20 +01:00
|
|
|
outbuf_size = len + NUL_TERMINATOR_LENGTH;
|
2001-01-05 22:22:47 +01:00
|
|
|
|
2009-10-28 19:18:20 +01:00
|
|
|
outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
|
2000-09-11 05:03:18 +02:00
|
|
|
outp = dest = g_malloc (outbuf_size);
|
|
|
|
|
2005-08-02 16:58:20 +02:00
|
|
|
while (!done && !have_error)
|
2000-09-11 05:03:18 +02:00
|
|
|
{
|
2007-11-08 07:04:00 +01:00
|
|
|
if (reset)
|
|
|
|
err = g_iconv (converter, NULL, &inbytes_remaining, &outp, &outbytes_remaining);
|
|
|
|
else
|
|
|
|
err = g_iconv (converter, (char **)&p, &inbytes_remaining, &outp, &outbytes_remaining);
|
2005-08-02 16:58:20 +02:00
|
|
|
|
2006-12-28 05:48:06 +01:00
|
|
|
if (err == (gsize) -1)
|
2000-09-11 05:03:18 +02:00
|
|
|
{
|
2005-08-02 16:58:20 +02:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
/* Incomplete text, do not report an error */
|
2005-08-02 23:10:16 +02:00
|
|
|
done = TRUE;
|
2005-08-02 16:58:20 +02:00
|
|
|
break;
|
|
|
|
case E2BIG:
|
|
|
|
{
|
2006-12-28 05:48:06 +01:00
|
|
|
gsize used = outp - dest;
|
2001-01-05 22:22:47 +01:00
|
|
|
|
2005-08-02 16:58:20 +02:00
|
|
|
outbuf_size *= 2;
|
|
|
|
dest = g_realloc (dest, outbuf_size);
|
|
|
|
|
|
|
|
outp = dest + used;
|
2009-10-28 19:18:20 +01:00
|
|
|
outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
|
2005-08-02 16:58:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EILSEQ:
|
2011-09-18 02:27:16 +02:00
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid byte sequence in conversion input"));
|
2005-08-02 16:58:20 +02:00
|
|
|
have_error = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
2011-09-18 02:27:16 +02:00
|
|
|
{
|
|
|
|
int errsv = errno;
|
|
|
|
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
|
|
|
|
_("Error during conversion: %s"),
|
|
|
|
g_strerror (errsv));
|
|
|
|
}
|
2005-08-02 16:58:20 +02:00
|
|
|
have_error = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
gconvert: Fix error handling for g_iconv() with unrepresentable chars
The behaviour of upstream iconv() when faced with a character which is
valid in the input encoding, but not representable in the output
encoding, is implementation defined:
http://pubs.opengroup.org/onlinepubs/9699919799/
Specifically:
If iconv() encounters a character in the input buffer that is valid,
but for which an identical character does not exist in the target
codeset, iconv() shall perform an implementation-defined conversion
on this character.
This behaviour was being exposed in our g_iconv() wrapper and also in
g_convert_with_iconv() — but users of g_convert_with_iconv() (both the
GLib unit tests, and the implementation of g_convert_with_fallback())
were assuming that iconv() would return EILSEQ if faced with an
unrepresentable character.
On platforms like NetBSD, this is not the case: NetBSD’s iconv()
finishes the conversion successfully, and outputs a string containing
replacement characters. It signals those replacements in its return
value from iconv(), which is positive (specifically, non-zero) in such a
case.
Let’s codify the existing assumed behaviour of g_convert_with_iconv(),
documenting that it will return G_CONVERT_ERROR_INVALID_SEQUENCE if
faced with an unrepresentable character. As g_iconv() is a thin wrapper
around iconv(), leave the behaviour there implementation-defined (but
document it as such).
Signed-off-by: Philip Withnall <withnall@endlessm.com>
https://bugzilla.gnome.org/show_bug.cgi?id=790698
2018-01-22 13:50:15 +01:00
|
|
|
else if (err > 0)
|
|
|
|
{
|
|
|
|
/* @err gives the number of replacement characters used. */
|
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Unrepresentable character in conversion input"));
|
|
|
|
have_error = TRUE;
|
|
|
|
}
|
2005-08-02 16:58:20 +02:00
|
|
|
else
|
|
|
|
{
|
2007-11-08 07:04:00 +01:00
|
|
|
if (!reset)
|
2005-08-02 16:58:20 +02:00
|
|
|
{
|
|
|
|
/* call g_iconv with NULL inbuf to cleanup shift state */
|
2007-11-08 07:04:00 +01:00
|
|
|
reset = TRUE;
|
2005-08-02 16:58:20 +02:00
|
|
|
inbytes_remaining = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
done = TRUE;
|
2000-09-11 05:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-28 19:18:20 +01:00
|
|
|
memset (outp, 0, NUL_TERMINATOR_LENGTH);
|
2000-09-11 05:03:18 +02:00
|
|
|
|
|
|
|
if (bytes_read)
|
|
|
|
*bytes_read = p - str;
|
2000-11-12 22:23:55 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((p - str) != len)
|
|
|
|
{
|
2001-02-13 19:20:13 +01:00
|
|
|
if (!have_error)
|
|
|
|
{
|
2011-09-18 02:27:16 +02:00
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
|
|
|
|
_("Partial character sequence at end of input"));
|
2001-02-13 19:20:13 +01:00
|
|
|
have_error = TRUE;
|
|
|
|
}
|
2000-11-12 22:23:55 +01:00
|
|
|
}
|
|
|
|
}
|
2000-09-11 05:03:18 +02:00
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = outp - dest; /* Doesn't include '\0' */
|
|
|
|
|
|
|
|
if (have_error)
|
|
|
|
{
|
|
|
|
g_free (dest);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2005-08-02 16:58:20 +02:00
|
|
|
/**
|
|
|
|
* g_convert:
|
2018-01-05 23:40:12 +01:00
|
|
|
* @str: (array length=len) (element-type guint8):
|
|
|
|
* the string to convert.
|
2014-05-30 00:13:37 +02:00
|
|
|
* @len: the length of the string in bytes, or -1 if the string is
|
2014-02-01 16:47:09 +01:00
|
|
|
* nul-terminated (Note that some encodings may allow nul
|
|
|
|
* bytes to occur inside strings. In that case, using -1
|
|
|
|
* for the @len parameter is unsafe)
|
2005-08-02 16:58:20 +02:00
|
|
|
* @to_codeset: name of character set into which to convert @str
|
|
|
|
* @from_codeset: character set of @str.
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in
|
|
|
|
* the input string that were successfully converted, or %NULL.
|
2005-08-02 16:58:20 +02:00
|
|
|
* Even if the conversion was successful, this may be
|
|
|
|
* less than @len if there were partial characters
|
|
|
|
* at the end of the input. If the error
|
2022-03-04 17:21:55 +01:00
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
|
2018-02-13 15:49:20 +01:00
|
|
|
* stored will be the byte offset after the last valid
|
2005-08-02 16:58:20 +02:00
|
|
|
* input sequence.
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in
|
|
|
|
* the output buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2005-08-02 16:58:20 +02:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
|
|
|
* Converts a string from one character set to another.
|
|
|
|
*
|
2014-02-01 16:47:09 +01:00
|
|
|
* Note that you should use g_iconv() for streaming conversions.
|
2018-01-11 12:54:14 +01:00
|
|
|
* Despite the fact that @bytes_read can return information about partial
|
2014-02-01 16:47:09 +01:00
|
|
|
* characters, the g_convert_... functions are not generally suitable
|
|
|
|
* for streaming. If the underlying converter maintains internal state,
|
|
|
|
* then this won't be preserved across successive calls to g_convert(),
|
|
|
|
* g_convert_with_iconv() or g_convert_with_fallback(). (An example of
|
|
|
|
* this is the GNU C converter for CP1255 which does not emit a base
|
|
|
|
* character until it knows that the next character is not a mark that
|
|
|
|
* could combine with the base character.)
|
2005-08-02 16:58:20 +02:00
|
|
|
*
|
2014-02-21 00:49:37 +01:00
|
|
|
* Using extensions such as "//TRANSLIT" may not work (or may not work
|
|
|
|
* well) on many platforms. Consider using g_str_to_ascii() instead.
|
|
|
|
*
|
2018-01-05 23:40:12 +01:00
|
|
|
* Returns: (array length=bytes_written) (element-type guint8) (transfer full):
|
|
|
|
* If the conversion was successful, a newly allocated buffer
|
|
|
|
* containing the converted string, which must be freed with g_free().
|
|
|
|
* Otherwise %NULL and @error will be set.
|
2005-08-02 16:58:20 +02:00
|
|
|
**/
|
|
|
|
gchar*
|
|
|
|
g_convert (const gchar *str,
|
|
|
|
gssize len,
|
|
|
|
const gchar *to_codeset,
|
|
|
|
const gchar *from_codeset,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar *res;
|
|
|
|
GIConv cd;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str != NULL, NULL);
|
|
|
|
g_return_val_if_fail (to_codeset != NULL, NULL);
|
|
|
|
g_return_val_if_fail (from_codeset != NULL, NULL);
|
|
|
|
|
|
|
|
cd = open_converter (to_codeset, from_codeset, error);
|
|
|
|
|
|
|
|
if (cd == (GIConv) -1)
|
|
|
|
{
|
|
|
|
if (bytes_read)
|
|
|
|
*bytes_read = 0;
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = g_convert_with_iconv (str, len, cd,
|
|
|
|
bytes_read, bytes_written,
|
|
|
|
error);
|
|
|
|
|
|
|
|
close_converter (cd);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:03:18 +02:00
|
|
|
/**
|
|
|
|
* g_convert_with_fallback:
|
2018-01-05 23:40:12 +01:00
|
|
|
* @str: (array length=len) (element-type guint8):
|
|
|
|
* the string to convert.
|
2014-05-30 00:13:37 +02:00
|
|
|
* @len: the length of the string in bytes, or -1 if the string is
|
2014-02-01 16:47:09 +01:00
|
|
|
* nul-terminated (Note that some encodings may allow nul
|
|
|
|
* bytes to occur inside strings. In that case, using -1
|
|
|
|
* for the @len parameter is unsafe)
|
2000-09-11 05:03:18 +02:00
|
|
|
* @to_codeset: name of character set into which to convert @str
|
|
|
|
* @from_codeset: character set of @str.
|
2018-01-22 13:48:43 +01:00
|
|
|
* @fallback: UTF-8 string to use in place of characters not
|
2005-04-05 05:56:40 +02:00
|
|
|
* present in the target encoding. (The string must be
|
|
|
|
* representable in the target encoding).
|
2017-11-10 22:18:20 +01:00
|
|
|
* If %NULL, characters not in the target encoding will
|
|
|
|
* be represented as Unicode escapes \uxxxx or \Uxxxxyyyy.
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in
|
|
|
|
* the input string that were successfully converted, or %NULL.
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Even if the conversion was successful, this may be
|
2001-10-01 19:35:18 +02:00
|
|
|
* less than @len if there were partial characters
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
* at the end of the input.
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in
|
|
|
|
* the output buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2000-09-11 05:03:18 +02:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Converts a string from one character set to another, possibly
|
2000-09-11 05:03:18 +02:00
|
|
|
* including fallback sequences for characters not representable
|
|
|
|
* in the output. Note that it is not guaranteed that the specification
|
|
|
|
* for the fallback sequences in @fallback will be honored. Some
|
2008-02-06 17:02:20 +01:00
|
|
|
* systems may do an approximate conversion from @from_codeset
|
2003-07-25 23:32:47 +02:00
|
|
|
* to @to_codeset in their iconv() functions,
|
2001-12-20 05:49:44 +01:00
|
|
|
* in which case GLib will simply return that approximate conversion.
|
2000-09-11 05:03:18 +02:00
|
|
|
*
|
2014-02-01 16:47:09 +01:00
|
|
|
* Note that you should use g_iconv() for streaming conversions.
|
2018-01-11 12:54:14 +01:00
|
|
|
* Despite the fact that @bytes_read can return information about partial
|
2014-02-01 16:47:09 +01:00
|
|
|
* characters, the g_convert_... functions are not generally suitable
|
|
|
|
* for streaming. If the underlying converter maintains internal state,
|
|
|
|
* then this won't be preserved across successive calls to g_convert(),
|
|
|
|
* g_convert_with_iconv() or g_convert_with_fallback(). (An example of
|
|
|
|
* this is the GNU C converter for CP1255 which does not emit a base
|
|
|
|
* character until it knows that the next character is not a mark that
|
|
|
|
* could combine with the base character.)
|
2005-08-02 16:58:20 +02:00
|
|
|
*
|
2018-01-05 23:40:12 +01:00
|
|
|
* Returns: (array length=bytes_written) (element-type guint8) (transfer full):
|
|
|
|
* If the conversion was successful, a newly allocated buffer
|
|
|
|
* containing the converted string, which must be freed with g_free().
|
|
|
|
* Otherwise %NULL and @error will be set.
|
2000-09-11 05:03:18 +02:00
|
|
|
**/
|
|
|
|
gchar*
|
|
|
|
g_convert_with_fallback (const gchar *str,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize len,
|
2000-09-11 05:03:18 +02:00
|
|
|
const gchar *to_codeset,
|
|
|
|
const gchar *from_codeset,
|
2010-03-16 16:34:50 +01:00
|
|
|
const gchar *fallback,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
2000-09-11 05:03:18 +02:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar *utf8;
|
|
|
|
gchar *dest;
|
|
|
|
gchar *outp;
|
|
|
|
const gchar *insert_str = NULL;
|
|
|
|
const gchar *p;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gsize inbytes_remaining;
|
2000-09-11 05:03:18 +02:00
|
|
|
const gchar *save_p = NULL;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gsize save_inbytes = 0;
|
|
|
|
gsize outbytes_remaining;
|
|
|
|
gsize err;
|
2000-11-13 00:44:28 +01:00
|
|
|
GIConv cd;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gsize outbuf_size;
|
2000-09-11 05:03:18 +02:00
|
|
|
gboolean have_error = FALSE;
|
|
|
|
gboolean done = FALSE;
|
|
|
|
|
|
|
|
GError *local_error = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str != NULL, NULL);
|
|
|
|
g_return_val_if_fail (to_codeset != NULL, NULL);
|
|
|
|
g_return_val_if_fail (from_codeset != NULL, NULL);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
/* Try an exact conversion; we only proceed if this fails
|
|
|
|
* due to an illegal sequence in the input string.
|
|
|
|
*/
|
|
|
|
dest = g_convert (str, len, to_codeset, from_codeset,
|
|
|
|
bytes_read, bytes_written, &local_error);
|
|
|
|
if (!local_error)
|
|
|
|
return dest;
|
|
|
|
|
2022-05-05 12:19:50 +02:00
|
|
|
g_assert (dest == NULL);
|
|
|
|
|
2000-09-11 05:03:18 +02:00
|
|
|
if (!g_error_matches (local_error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
|
|
|
|
{
|
|
|
|
g_propagate_error (error, local_error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_error_free (local_error);
|
|
|
|
|
2001-02-13 19:20:13 +01:00
|
|
|
local_error = NULL;
|
|
|
|
|
2000-09-11 05:03:18 +02:00
|
|
|
/* No go; to proceed, we need a converter from "UTF-8" to
|
|
|
|
* to_codeset, and the string as UTF-8.
|
|
|
|
*/
|
|
|
|
cd = open_converter (to_codeset, "UTF-8", error);
|
2000-11-13 00:44:28 +01:00
|
|
|
if (cd == (GIConv) -1)
|
2000-09-11 05:03:18 +02:00
|
|
|
{
|
|
|
|
if (bytes_read)
|
|
|
|
*bytes_read = 0;
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
utf8 = g_convert (str, len, "UTF-8", from_codeset,
|
|
|
|
bytes_read, &inbytes_remaining, error);
|
|
|
|
if (!utf8)
|
2002-01-21 21:53:18 +01:00
|
|
|
{
|
2002-01-29 19:25:45 +01:00
|
|
|
close_converter (cd);
|
2002-01-21 21:53:18 +01:00
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-11 05:03:18 +02:00
|
|
|
|
|
|
|
/* Now the heart of the code. We loop through the UTF-8 string, and
|
|
|
|
* whenever we hit an offending character, we form fallback, convert
|
|
|
|
* the fallback to the target codeset, and then go back to
|
|
|
|
* converting the original string after finishing with the fallback.
|
|
|
|
*
|
|
|
|
* The variables save_p and save_inbytes store the input state
|
|
|
|
* for the original string while we are converting the fallback
|
|
|
|
*/
|
|
|
|
p = utf8;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
|
2009-10-28 19:18:20 +01:00
|
|
|
outbuf_size = len + NUL_TERMINATOR_LENGTH;
|
|
|
|
outbytes_remaining = outbuf_size - NUL_TERMINATOR_LENGTH;
|
2000-09-11 05:03:18 +02:00
|
|
|
outp = dest = g_malloc (outbuf_size);
|
|
|
|
|
|
|
|
while (!done && !have_error)
|
|
|
|
{
|
2006-12-28 05:48:06 +01:00
|
|
|
gsize inbytes_tmp = inbytes_remaining;
|
2000-11-13 00:44:28 +01:00
|
|
|
err = g_iconv (cd, (char **)&p, &inbytes_tmp, &outp, &outbytes_remaining);
|
2000-09-18 00:18:48 +02:00
|
|
|
inbytes_remaining = inbytes_tmp;
|
2000-09-11 05:03:18 +02:00
|
|
|
|
2006-12-28 05:48:06 +01:00
|
|
|
if (err == (gsize) -1)
|
2000-09-11 05:03:18 +02:00
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
g_assert_not_reached();
|
|
|
|
break;
|
|
|
|
case E2BIG:
|
|
|
|
{
|
2006-12-28 05:48:06 +01:00
|
|
|
gsize used = outp - dest;
|
2001-01-05 22:22:47 +01:00
|
|
|
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
outbuf_size *= 2;
|
|
|
|
dest = g_realloc (dest, outbuf_size);
|
|
|
|
|
|
|
|
outp = dest + used;
|
2009-10-28 19:18:20 +01:00
|
|
|
outbytes_remaining = outbuf_size - used - NUL_TERMINATOR_LENGTH;
|
2000-09-11 05:03:18 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EILSEQ:
|
|
|
|
if (save_p)
|
|
|
|
{
|
|
|
|
/* Error converting fallback string - fatal
|
|
|
|
*/
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("Cannot convert fallback “%s” to codeset “%s”"),
|
2000-09-11 05:03:18 +02:00
|
|
|
insert_str, to_codeset);
|
|
|
|
have_error = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2005-08-02 16:58:20 +02:00
|
|
|
else if (p)
|
2000-09-11 05:03:18 +02:00
|
|
|
{
|
|
|
|
if (!fallback)
|
|
|
|
{
|
|
|
|
gunichar ch = g_utf8_get_char (p);
|
2003-06-25 20:03:38 +02:00
|
|
|
insert_str = g_strdup_printf (ch < 0x10000 ? "\\u%04x" : "\\U%08x",
|
2000-09-11 05:03:18 +02:00
|
|
|
ch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
insert_str = fallback;
|
|
|
|
|
|
|
|
save_p = g_utf8_next_char (p);
|
|
|
|
save_inbytes = inbytes_remaining - (save_p - p);
|
|
|
|
p = insert_str;
|
|
|
|
inbytes_remaining = strlen (p);
|
2005-08-02 16:58:20 +02:00
|
|
|
break;
|
2000-09-11 05:03:18 +02:00
|
|
|
}
|
2018-09-02 20:47:32 +02:00
|
|
|
/* if p is null */
|
|
|
|
G_GNUC_FALLTHROUGH;
|
2000-09-11 05:03:18 +02:00
|
|
|
default:
|
2009-08-20 15:13:43 +02:00
|
|
|
{
|
|
|
|
int errsv = errno;
|
|
|
|
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
|
|
|
|
_("Error during conversion: %s"),
|
|
|
|
g_strerror (errsv));
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:03:18 +02:00
|
|
|
have_error = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (save_p)
|
|
|
|
{
|
|
|
|
if (!fallback)
|
|
|
|
g_free ((gchar *)insert_str);
|
|
|
|
p = save_p;
|
|
|
|
inbytes_remaining = save_inbytes;
|
|
|
|
save_p = NULL;
|
|
|
|
}
|
2005-08-02 16:58:20 +02:00
|
|
|
else if (p)
|
|
|
|
{
|
|
|
|
/* call g_iconv with NULL inbuf to cleanup shift state */
|
|
|
|
p = NULL;
|
|
|
|
inbytes_remaining = 0;
|
|
|
|
}
|
2000-09-11 05:03:18 +02:00
|
|
|
else
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup
|
|
|
|
*/
|
2009-10-28 19:18:20 +01:00
|
|
|
memset (outp, 0, NUL_TERMINATOR_LENGTH);
|
2000-09-11 05:03:18 +02:00
|
|
|
|
2002-01-29 19:25:45 +01:00
|
|
|
close_converter (cd);
|
2000-09-11 05:03:18 +02:00
|
|
|
|
|
|
|
if (bytes_written)
|
2002-01-21 21:53:18 +01:00
|
|
|
*bytes_written = outp - dest; /* Doesn't include '\0' */
|
2000-09-11 05:03:18 +02:00
|
|
|
|
|
|
|
g_free (utf8);
|
|
|
|
|
|
|
|
if (have_error)
|
|
|
|
{
|
|
|
|
if (save_p && !fallback)
|
|
|
|
g_free ((gchar *)insert_str);
|
|
|
|
g_free (dest);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return dest;
|
|
|
|
}
|
2000-11-12 22:23:55 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* g_locale_to_utf8
|
|
|
|
*
|
2001-01-05 22:22:47 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-01-13 11:40:22 +01:00
|
|
|
/*
|
|
|
|
* Validate @string as UTF-8. @len can be negative if @string is
|
|
|
|
* nul-terminated, or a non-negative value in bytes. If @string ends in an
|
|
|
|
* incomplete sequence, or contains any illegal sequences or nul codepoints,
|
|
|
|
* %NULL will be returned and the error set to
|
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
|
|
|
|
* On success, @bytes_read and @bytes_written, if provided, will be set to
|
|
|
|
* the number of bytes in @string up to @len or the terminating nul byte.
|
|
|
|
* On error, @bytes_read will be set to the byte offset after the last valid
|
|
|
|
* and non-nul UTF-8 sequence in @string, and @bytes_written will be set to 0.
|
|
|
|
*/
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
static gchar *
|
|
|
|
strdup_len (const gchar *string,
|
|
|
|
gssize len,
|
2001-08-26 23:39:47 +02:00
|
|
|
gsize *bytes_read,
|
2018-01-13 11:40:22 +01:00
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
{
|
|
|
|
gsize real_len;
|
2018-01-13 11:40:22 +01:00
|
|
|
const gchar *end_valid;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
|
2018-01-13 11:40:22 +01:00
|
|
|
if (!g_utf8_validate (string, len, &end_valid))
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
|
|
|
if (bytes_read)
|
2018-01-13 11:40:22 +01:00
|
|
|
*bytes_read = end_valid - string;
|
2001-08-26 23:39:47 +02:00
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
2008-06-16 18:53:58 +02:00
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid byte sequence in conversion input"));
|
2001-08-26 23:39:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-13 11:40:22 +01:00
|
|
|
|
|
|
|
real_len = end_valid - string;
|
|
|
|
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
if (bytes_read)
|
|
|
|
*bytes_read = real_len;
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = real_len;
|
|
|
|
|
|
|
|
return g_strndup (string, real_len);
|
|
|
|
}
|
|
|
|
|
2018-01-17 08:25:23 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_INPUT = 1 << 0,
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_OUTPUT = 1 << 1
|
|
|
|
} ConvertCheckFlags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from @string in the encoding identified by @from_codeset,
|
|
|
|
* returning a string in the encoding identifed by @to_codeset.
|
|
|
|
* @len can be negative if @string is nul-terminated, or a non-negative
|
|
|
|
* value in bytes. Flags defined in #ConvertCheckFlags can be set in @flags
|
|
|
|
* to check the input, the output, or both, for embedded nul bytes.
|
|
|
|
* On success, @bytes_read, if provided, will be set to the number of bytes
|
|
|
|
* in @string up to @len or the terminating nul byte, and @bytes_written, if
|
|
|
|
* provided, will be set to the number of output bytes written into the
|
|
|
|
* returned buffer, excluding the terminating nul sequence.
|
|
|
|
* On error, @bytes_read will be set to the byte offset after the last valid
|
|
|
|
* sequence in @string, and @bytes_written will be set to 0.
|
|
|
|
*/
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
static gchar *
|
2018-01-17 08:25:23 +01:00
|
|
|
convert_checked (const gchar *string,
|
|
|
|
gssize len,
|
|
|
|
const gchar *to_codeset,
|
|
|
|
const gchar *from_codeset,
|
|
|
|
ConvertCheckFlags flags,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
{
|
2018-01-17 08:25:23 +01:00
|
|
|
gchar *out;
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
gsize outbytes;
|
|
|
|
|
2018-01-17 08:25:23 +01:00
|
|
|
if ((flags & CONVERT_CHECK_NO_NULS_IN_INPUT) && len > 0)
|
|
|
|
{
|
|
|
|
const gchar *early_nul = memchr (string, '\0', len);
|
|
|
|
if (early_nul != NULL)
|
|
|
|
{
|
|
|
|
if (bytes_read)
|
|
|
|
*bytes_read = early_nul - string;
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
|
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Embedded NUL byte in conversion input"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = g_convert (string, len, to_codeset, from_codeset,
|
|
|
|
bytes_read, &outbytes, error);
|
|
|
|
if (out == NULL)
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
{
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-17 08:25:23 +01:00
|
|
|
|
|
|
|
if ((flags & CONVERT_CHECK_NO_NULS_IN_OUTPUT)
|
|
|
|
&& memchr (out, '\0', outbytes) != NULL)
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
{
|
2018-01-17 08:25:23 +01:00
|
|
|
g_free (out);
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = 0;
|
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_EMBEDDED_NUL,
|
|
|
|
_("Embedded NUL byte in conversion output"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_written)
|
|
|
|
*bytes_written = outbytes;
|
2018-01-17 08:25:23 +01:00
|
|
|
return out;
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
}
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
/**
|
|
|
|
* g_locale_to_utf8:
|
2018-01-05 23:40:12 +01:00
|
|
|
* @opsysstring: (array length=len) (element-type guint8): a string in the
|
|
|
|
* encoding of the current locale. On Windows
|
2004-12-20 23:33:11 +01:00
|
|
|
* this means the system codepage.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @len: the length of the string, or -1 if the string is
|
2014-02-01 16:47:09 +01:00
|
|
|
* nul-terminated (Note that some encodings may allow nul
|
|
|
|
* bytes to occur inside strings. In that case, using -1
|
|
|
|
* for the @len parameter is unsafe)
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in the
|
2001-01-05 22:22:47 +01:00
|
|
|
* input string that were successfully converted, or %NULL.
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Even if the conversion was successful, this may be
|
2001-10-01 19:35:18 +02:00
|
|
|
* less than @len if there were partial characters
|
2001-01-05 22:22:47 +01:00
|
|
|
* at the end of the input. If the error
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
|
2018-02-13 15:49:20 +01:00
|
|
|
* stored will be the byte offset after the last valid
|
2001-01-05 22:22:47 +01:00
|
|
|
* input sequence.
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in the output
|
|
|
|
* buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-01-05 22:22:47 +01:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
2000-11-12 22:23:55 +01:00
|
|
|
* Converts a string which is in the encoding used for strings by
|
|
|
|
* the C runtime (usually the same as that used by the operating
|
2014-02-08 18:26:56 +01:00
|
|
|
* system) in the [current locale][setlocale] into a UTF-8 string.
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
*
|
|
|
|
* If the source encoding is not UTF-8 and the conversion output contains a
|
|
|
|
* nul character, the error %G_CONVERT_ERROR_EMBEDDED_NUL is set and the
|
|
|
|
* function returns %NULL.
|
|
|
|
* If the source encoding is UTF-8, an embedded nul character is treated with
|
|
|
|
* the %G_CONVERT_ERROR_ILLEGAL_SEQUENCE error for backward compatibility with
|
|
|
|
* earlier versions of this library. Use g_convert() to produce output that
|
|
|
|
* may contain embedded nul characters.
|
2001-01-05 22:22:47 +01:00
|
|
|
*
|
2018-01-05 23:40:12 +01:00
|
|
|
* Returns: (type utf8): The converted string, or %NULL on an error.
|
2001-01-05 22:22:47 +01:00
|
|
|
**/
|
2000-11-12 22:23:55 +01:00
|
|
|
gchar *
|
2001-01-05 22:22:47 +01:00
|
|
|
g_locale_to_utf8 (const gchar *opsysstring,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
2000-11-12 22:23:55 +01:00
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
const char *charset;
|
2000-11-12 22:23:55 +01:00
|
|
|
|
|
|
|
if (g_get_charset (&charset))
|
2001-08-26 23:39:47 +02:00
|
|
|
return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
else
|
2018-01-17 08:25:23 +01:00
|
|
|
return convert_checked (opsysstring, len, "UTF-8", charset,
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_OUTPUT,
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
bytes_read, bytes_written, error);
|
2000-11-12 22:23:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:47:27 +01:00
|
|
|
/*
|
|
|
|
* Do the exact same as g_locale_to_utf8 except that the charset would
|
|
|
|
* be retrieved from _g_get_time_charset (which uses LC_TIME)
|
|
|
|
*
|
|
|
|
* Returns: The converted string, or %NULL on an error.
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
_g_time_locale_to_utf8 (const gchar *opsysstring,
|
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
const char *charset;
|
|
|
|
|
|
|
|
if (_g_get_time_charset (&charset))
|
|
|
|
return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
|
|
|
|
else
|
|
|
|
return convert_checked (opsysstring, len, "UTF-8", charset,
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_OUTPUT,
|
|
|
|
bytes_read, bytes_written, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the exact same as g_locale_to_utf8 except that the charset would
|
|
|
|
* be retrieved from _g_get_ctype_charset (which uses LC_CTYPE)
|
|
|
|
*
|
|
|
|
* Returns: The converted string, or %NULL on an error.
|
|
|
|
*/
|
|
|
|
gchar *
|
|
|
|
_g_ctype_locale_to_utf8 (const gchar *opsysstring,
|
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
const char *charset;
|
|
|
|
|
|
|
|
if (_g_get_ctype_charset (&charset))
|
|
|
|
return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
|
|
|
|
else
|
|
|
|
return convert_checked (opsysstring, len, "UTF-8", charset,
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_OUTPUT,
|
|
|
|
bytes_read, bytes_written, error);
|
|
|
|
}
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
/**
|
|
|
|
* g_locale_from_utf8:
|
|
|
|
* @utf8string: a UTF-8 encoded string
|
|
|
|
* @len: the length of the string, or -1 if the string is
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* nul-terminated.
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in the
|
2001-01-05 22:22:47 +01:00
|
|
|
* input string that were successfully converted, or %NULL.
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Even if the conversion was successful, this may be
|
2001-10-01 19:35:18 +02:00
|
|
|
* less than @len if there were partial characters
|
2001-01-05 22:22:47 +01:00
|
|
|
* at the end of the input. If the error
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
|
2018-02-13 15:49:20 +01:00
|
|
|
* stored will be the byte offset after the last valid
|
2001-01-05 22:22:47 +01:00
|
|
|
* input sequence.
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in the output
|
|
|
|
* buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-01-05 22:22:47 +01:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
|
|
|
* Converts a string from UTF-8 to the encoding used for strings by
|
|
|
|
* the C runtime (usually the same as that used by the operating
|
2014-02-08 18:26:56 +01:00
|
|
|
* system) in the [current locale][setlocale]. On Windows this means
|
|
|
|
* the system codepage.
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
*
|
2018-01-17 08:25:23 +01:00
|
|
|
* The input string shall not contain nul characters even if the @len
|
|
|
|
* argument is positive. A nul character found inside the string will result
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* in error %G_CONVERT_ERROR_ILLEGAL_SEQUENCE. Use g_convert() to convert
|
|
|
|
* input that may contain embedded nul characters.
|
|
|
|
*
|
2018-01-05 23:40:12 +01:00
|
|
|
* Returns: (array length=bytes_written) (element-type guint8) (transfer full):
|
|
|
|
* A newly-allocated buffer containing the converted string,
|
|
|
|
* or %NULL on an error, and error will be set.
|
2001-01-05 22:22:47 +01:00
|
|
|
**/
|
2000-11-12 22:23:55 +01:00
|
|
|
gchar *
|
2001-01-05 22:22:47 +01:00
|
|
|
g_locale_from_utf8 (const gchar *utf8string,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
2000-11-12 22:23:55 +01:00
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
const gchar *charset;
|
2000-11-12 22:23:55 +01:00
|
|
|
|
|
|
|
if (g_get_charset (&charset))
|
2001-08-26 23:39:47 +02:00
|
|
|
return strdup_len (utf8string, len, bytes_read, bytes_written, error);
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
else
|
2018-01-17 08:25:23 +01:00
|
|
|
return convert_checked (utf8string, len, charset, "UTF-8",
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_INPUT,
|
|
|
|
bytes_read, bytes_written, error);
|
2000-11-12 22:23:55 +01:00
|
|
|
}
|
|
|
|
|
2002-03-13 06:01:58 +01:00
|
|
|
#ifndef G_PLATFORM_WIN32
|
2003-08-13 02:13:02 +02:00
|
|
|
|
2003-11-09 14:17:23 +01:00
|
|
|
typedef struct _GFilenameCharsetCache GFilenameCharsetCache;
|
|
|
|
|
|
|
|
struct _GFilenameCharsetCache {
|
|
|
|
gboolean is_utf8;
|
|
|
|
gchar *charset;
|
2004-11-02 22:29:33 +01:00
|
|
|
gchar **filename_charsets;
|
2003-11-09 14:17:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
filename_charset_cache_free (gpointer data)
|
|
|
|
{
|
|
|
|
GFilenameCharsetCache *cache = data;
|
|
|
|
g_free (cache->charset);
|
2004-11-02 22:29:33 +01:00
|
|
|
g_strfreev (cache->filename_charsets);
|
2003-11-09 14:17:23 +01:00
|
|
|
g_free (cache);
|
|
|
|
}
|
|
|
|
|
2004-11-02 22:53:23 +01:00
|
|
|
/**
|
2004-11-02 22:29:33 +01:00
|
|
|
* g_get_filename_charsets:
|
2018-01-05 23:40:12 +01:00
|
|
|
* @filename_charsets: (out) (transfer none) (array zero-terminated=1):
|
|
|
|
* return location for the %NULL-terminated list of encoding names
|
2003-11-06 00:05:49 +01:00
|
|
|
*
|
2004-11-02 22:29:33 +01:00
|
|
|
* Determines the preferred character sets used for filenames.
|
|
|
|
* The first character set from the @charsets is the filename encoding, the
|
|
|
|
* subsequent character sets are used when trying to generate a displayable
|
2004-11-10 15:26:27 +01:00
|
|
|
* representation of a filename, see g_filename_display_name().
|
2003-11-06 00:05:49 +01:00
|
|
|
*
|
2004-12-12 21:53:02 +01:00
|
|
|
* On Unix, the character sets are determined by consulting the
|
2014-02-06 01:32:41 +01:00
|
|
|
* environment variables `G_FILENAME_ENCODING` and `G_BROKEN_FILENAMES`.
|
|
|
|
* On Windows, the character set used in the GLib API is always UTF-8
|
|
|
|
* and said environment variables have no effect.
|
|
|
|
*
|
|
|
|
* `G_FILENAME_ENCODING` may be set to a comma-separated list of
|
2015-11-16 13:16:08 +01:00
|
|
|
* character set names. The special token "\@locale" is taken
|
2014-02-08 18:26:56 +01:00
|
|
|
* to mean the character set for the [current locale][setlocale].
|
|
|
|
* If `G_FILENAME_ENCODING` is not set, but `G_BROKEN_FILENAMES` is,
|
|
|
|
* the character set of the current locale is taken as the filename
|
|
|
|
* encoding. If neither environment variable is set, UTF-8 is taken
|
|
|
|
* as the filename encoding, but the character set of the current locale
|
|
|
|
* is also put in the list of encodings.
|
2004-11-02 22:29:33 +01:00
|
|
|
*
|
|
|
|
* The returned @charsets belong to GLib and must not be freed.
|
2004-10-27 18:46:29 +02:00
|
|
|
*
|
|
|
|
* Note that on Unix, regardless of the locale character set or
|
2014-02-06 01:32:41 +01:00
|
|
|
* `G_FILENAME_ENCODING` value, the actual file names present
|
2006-12-27 06:09:48 +01:00
|
|
|
* on a system might be in any random encoding or just gibberish.
|
2004-10-27 18:46:29 +02:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: %TRUE if the filename encoding is UTF-8.
|
2004-11-02 22:29:33 +01:00
|
|
|
*
|
|
|
|
* Since: 2.6
|
2003-11-06 00:05:49 +01:00
|
|
|
*/
|
2004-11-02 22:29:33 +01:00
|
|
|
gboolean
|
2011-03-13 03:50:45 +01:00
|
|
|
g_get_filename_charsets (const gchar ***filename_charsets)
|
2002-03-13 06:01:58 +01:00
|
|
|
{
|
2011-10-13 00:27:06 +02:00
|
|
|
static GPrivate cache_private = G_PRIVATE_INIT (filename_charset_cache_free);
|
|
|
|
GFilenameCharsetCache *cache = g_private_get (&cache_private);
|
2003-11-09 14:17:23 +01:00
|
|
|
const gchar *charset;
|
2004-11-02 22:29:33 +01:00
|
|
|
|
2003-11-09 14:17:23 +01:00
|
|
|
if (!cache)
|
2018-12-06 16:16:33 +01:00
|
|
|
cache = g_private_set_alloc0 (&cache_private, sizeof (GFilenameCharsetCache));
|
2003-11-09 14:17:23 +01:00
|
|
|
|
|
|
|
g_get_charset (&charset);
|
|
|
|
|
|
|
|
if (!(cache->charset && strcmp (cache->charset, charset) == 0))
|
|
|
|
{
|
|
|
|
const gchar *new_charset;
|
2020-07-23 09:50:39 +02:00
|
|
|
const gchar *p;
|
2004-11-02 22:29:33 +01:00
|
|
|
gint i;
|
2002-03-13 06:01:58 +01:00
|
|
|
|
2003-11-09 14:17:23 +01:00
|
|
|
g_free (cache->charset);
|
2004-11-02 22:29:33 +01:00
|
|
|
g_strfreev (cache->filename_charsets);
|
2003-11-09 14:17:23 +01:00
|
|
|
cache->charset = g_strdup (charset);
|
2003-11-06 00:05:49 +01:00
|
|
|
|
2020-07-23 09:50:39 +02:00
|
|
|
p = g_getenv ("G_FILENAME_ENCODING");
|
2005-01-17 16:28:59 +01:00
|
|
|
if (p != NULL && p[0] != '\0')
|
2003-11-06 00:05:49 +01:00
|
|
|
{
|
2004-11-02 22:29:33 +01:00
|
|
|
cache->filename_charsets = g_strsplit (p, ",", 0);
|
|
|
|
cache->is_utf8 = (strcmp (cache->filename_charsets[0], "UTF-8") == 0);
|
2003-08-13 02:13:02 +02:00
|
|
|
|
2004-11-02 22:29:33 +01:00
|
|
|
for (i = 0; cache->filename_charsets[i]; i++)
|
2003-11-06 00:05:49 +01:00
|
|
|
{
|
2004-11-02 22:29:33 +01:00
|
|
|
if (strcmp ("@locale", cache->filename_charsets[i]) == 0)
|
|
|
|
{
|
|
|
|
g_get_charset (&new_charset);
|
|
|
|
g_free (cache->filename_charsets[i]);
|
|
|
|
cache->filename_charsets[i] = g_strdup (new_charset);
|
|
|
|
}
|
2003-11-06 00:05:49 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 09:50:39 +02:00
|
|
|
else if (g_getenv ("G_BROKEN_FILENAMES") != NULL)
|
2003-11-09 14:17:23 +01:00
|
|
|
{
|
2004-11-02 22:29:33 +01:00
|
|
|
cache->filename_charsets = g_new0 (gchar *, 2);
|
2003-11-09 14:17:23 +01:00
|
|
|
cache->is_utf8 = g_get_charset (&new_charset);
|
2004-11-02 22:29:33 +01:00
|
|
|
cache->filename_charsets[0] = g_strdup (new_charset);
|
2003-11-09 14:17:23 +01:00
|
|
|
}
|
2003-11-06 00:05:49 +01:00
|
|
|
else
|
|
|
|
{
|
2004-11-02 22:29:33 +01:00
|
|
|
cache->filename_charsets = g_new0 (gchar *, 3);
|
2003-11-09 14:17:23 +01:00
|
|
|
cache->is_utf8 = TRUE;
|
2004-11-02 22:29:33 +01:00
|
|
|
cache->filename_charsets[0] = g_strdup ("UTF-8");
|
|
|
|
if (!g_get_charset (&new_charset))
|
|
|
|
cache->filename_charsets[1] = g_strdup (new_charset);
|
2003-11-06 00:05:49 +01:00
|
|
|
}
|
|
|
|
}
|
2003-08-13 02:13:02 +02:00
|
|
|
|
2004-11-02 22:29:33 +01:00
|
|
|
if (filename_charsets)
|
|
|
|
*filename_charsets = (const gchar **)cache->filename_charsets;
|
2003-11-06 00:05:49 +01:00
|
|
|
|
2003-11-09 14:17:23 +01:00
|
|
|
return cache->is_utf8;
|
2003-11-06 00:05:49 +01:00
|
|
|
}
|
2003-11-09 14:17:23 +01:00
|
|
|
|
2003-11-06 00:05:49 +01:00
|
|
|
#else /* G_PLATFORM_WIN32 */
|
2004-10-27 18:46:29 +02:00
|
|
|
|
2004-11-02 22:29:33 +01:00
|
|
|
gboolean
|
2011-03-13 03:50:45 +01:00
|
|
|
g_get_filename_charsets (const gchar ***filename_charsets)
|
2004-10-27 18:46:29 +02:00
|
|
|
{
|
2005-04-27 11:50:09 +02:00
|
|
|
static const gchar *charsets[] = {
|
2004-11-02 22:29:33 +01:00
|
|
|
"UTF-8",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2004-10-27 18:46:29 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* On Windows GLib pretends that the filename charset is UTF-8 */
|
2004-11-02 22:29:33 +01:00
|
|
|
if (filename_charsets)
|
|
|
|
*filename_charsets = charsets;
|
|
|
|
|
2004-10-27 18:46:29 +02:00
|
|
|
return TRUE;
|
|
|
|
#else
|
2004-11-02 22:29:33 +01:00
|
|
|
gboolean result;
|
|
|
|
|
2004-10-27 18:46:29 +02:00
|
|
|
/* Cygwin works like before */
|
2004-11-02 22:29:33 +01:00
|
|
|
result = g_get_charset (&(charsets[0]));
|
|
|
|
|
|
|
|
if (filename_charsets)
|
|
|
|
*filename_charsets = charsets;
|
|
|
|
|
|
|
|
return result;
|
2004-10-27 18:46:29 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-11-02 22:29:33 +01:00
|
|
|
#endif /* G_PLATFORM_WIN32 */
|
2004-10-27 18:46:29 +02:00
|
|
|
|
|
|
|
static gboolean
|
2004-11-02 22:29:33 +01:00
|
|
|
get_filename_charset (const gchar **filename_charset)
|
2003-12-13 15:38:18 +01:00
|
|
|
{
|
2004-11-02 22:29:33 +01:00
|
|
|
const gchar **charsets;
|
|
|
|
gboolean is_utf8;
|
|
|
|
|
|
|
|
is_utf8 = g_get_filename_charsets (&charsets);
|
2004-10-27 18:46:29 +02:00
|
|
|
|
2004-11-02 22:29:33 +01:00
|
|
|
if (filename_charset)
|
|
|
|
*filename_charset = charsets[0];
|
|
|
|
|
|
|
|
return is_utf8;
|
|
|
|
}
|
2002-03-13 06:01:58 +01:00
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
/**
|
|
|
|
* g_filename_to_utf8:
|
2016-06-04 15:46:12 +02:00
|
|
|
* @opsysstring: (type filename): a string in the encoding for filenames
|
2001-01-05 22:22:47 +01:00
|
|
|
* @len: the length of the string, or -1 if the string is
|
2014-02-01 16:47:09 +01:00
|
|
|
* nul-terminated (Note that some encodings may allow nul
|
|
|
|
* bytes to occur inside strings. In that case, using -1
|
|
|
|
* for the @len parameter is unsafe)
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in the
|
2001-01-05 22:22:47 +01:00
|
|
|
* input string that were successfully converted, or %NULL.
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Even if the conversion was successful, this may be
|
2001-10-01 19:35:18 +02:00
|
|
|
* less than @len if there were partial characters
|
2001-01-05 22:22:47 +01:00
|
|
|
* at the end of the input. If the error
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
|
2018-02-13 15:49:20 +01:00
|
|
|
* stored will be the byte offset after the last valid
|
2001-01-05 22:22:47 +01:00
|
|
|
* input sequence.
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in the output
|
|
|
|
* buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-01-05 22:22:47 +01:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
2005-04-27 11:50:09 +02:00
|
|
|
* Converts a string which is in the encoding used by GLib for
|
|
|
|
* filenames into a UTF-8 string. Note that on Windows GLib uses UTF-8
|
2006-12-27 06:09:48 +01:00
|
|
|
* for filenames; on other platforms, this function indirectly depends on
|
2014-02-08 18:26:56 +01:00
|
|
|
* the [current locale][setlocale].
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
*
|
2018-01-17 08:25:23 +01:00
|
|
|
* The input string shall not contain nul characters even if the @len
|
|
|
|
* argument is positive. A nul character found inside the string will result
|
|
|
|
* in error %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* If the source encoding is not UTF-8 and the conversion output contains a
|
|
|
|
* nul character, the error %G_CONVERT_ERROR_EMBEDDED_NUL is set and the
|
2018-01-17 08:25:23 +01:00
|
|
|
* function returns %NULL. Use g_convert() to produce output that
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* may contain embedded nul characters.
|
2001-01-05 22:22:47 +01:00
|
|
|
*
|
2018-01-05 23:40:12 +01:00
|
|
|
* Returns: (type utf8): The converted string, or %NULL on an error.
|
2001-01-05 22:22:47 +01:00
|
|
|
**/
|
2000-11-12 22:23:55 +01:00
|
|
|
gchar*
|
2001-01-05 22:22:47 +01:00
|
|
|
g_filename_to_utf8 (const gchar *opsysstring,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
2000-11-12 22:23:55 +01:00
|
|
|
{
|
2003-11-06 00:05:49 +01:00
|
|
|
const gchar *charset;
|
|
|
|
|
2011-06-15 01:52:19 +02:00
|
|
|
g_return_val_if_fail (opsysstring != NULL, NULL);
|
|
|
|
|
2003-11-06 00:05:49 +01:00
|
|
|
if (get_filename_charset (&charset))
|
2001-08-26 23:39:47 +02:00
|
|
|
return strdup_len (opsysstring, len, bytes_read, bytes_written, error);
|
2003-11-06 00:05:49 +01:00
|
|
|
else
|
2018-01-17 08:25:23 +01:00
|
|
|
return convert_checked (opsysstring, len, "UTF-8", charset,
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_INPUT |
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_OUTPUT,
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
bytes_read, bytes_written, error);
|
2000-11-12 22:23:55 +01:00
|
|
|
}
|
|
|
|
|
2001-01-05 22:22:47 +01:00
|
|
|
/**
|
|
|
|
* g_filename_from_utf8:
|
2018-02-08 22:33:13 +01:00
|
|
|
* @utf8string: (type utf8): a UTF-8 encoded string.
|
2001-01-05 22:22:47 +01:00
|
|
|
* @len: the length of the string, or -1 if the string is
|
2001-10-01 19:35:18 +02:00
|
|
|
* nul-terminated.
|
2013-12-06 13:23:09 +01:00
|
|
|
* @bytes_read: (out) (optional): location to store the number of bytes in
|
2012-11-03 13:52:29 +01:00
|
|
|
* the input string that were successfully converted, or %NULL.
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
* Even if the conversion was successful, this may be
|
2001-10-01 19:35:18 +02:00
|
|
|
* less than @len if there were partial characters
|
2001-01-05 22:22:47 +01:00
|
|
|
* at the end of the input. If the error
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
* %G_CONVERT_ERROR_ILLEGAL_SEQUENCE occurs, the value
|
2018-02-13 15:49:20 +01:00
|
|
|
* stored will be the byte offset after the last valid
|
2001-01-05 22:22:47 +01:00
|
|
|
* input sequence.
|
2018-01-05 23:40:12 +01:00
|
|
|
* @bytes_written: (out) (optional): the number of bytes stored in
|
|
|
|
* the output buffer (not including the terminating nul).
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-01-05 22:22:47 +01:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
2005-04-27 11:50:09 +02:00
|
|
|
* Converts a string from UTF-8 to the encoding GLib uses for
|
2006-12-27 06:09:48 +01:00
|
|
|
* filenames. Note that on Windows GLib uses UTF-8 for filenames;
|
|
|
|
* on other platforms, this function indirectly depends on the
|
2014-02-08 18:26:56 +01:00
|
|
|
* [current locale][setlocale].
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
*
|
2018-01-17 08:25:23 +01:00
|
|
|
* The input string shall not contain nul characters even if the @len
|
|
|
|
* argument is positive. A nul character found inside the string will result
|
2018-02-08 22:33:13 +01:00
|
|
|
* in error %G_CONVERT_ERROR_ILLEGAL_SEQUENCE. If the filename encoding is
|
|
|
|
* not UTF-8 and the conversion output contains a nul character, the error
|
|
|
|
* %G_CONVERT_ERROR_EMBEDDED_NUL is set and the function returns %NULL.
|
gconvert: Tighten, document embedded NUL behavior of UTF-8 conversions
The character encoding conversion utility functions g_locale_to_utf8()
and g_filename_to_utf8() had inconsistent behavior on producing strings
with inner NUL bytes: in the all-UTF-8 strdup path, the input string
validation prohibits embedded NULs, while g_convert(), using iconv(),
can produce UTF-8 output with NUL bytes inside the output buffer.
This, while valid UTF-8 per the Unicode standard, is not valid for
the nul-terminated (type utf8) return value format that the *_to_utf8()
functions are annotated with (as per discussion in bug 756128).
Check the output of g_convert() for embedded NUL bytes, and if any
are found, set the newly introduced error
G_CONVERT_ERROR_EMBEDDED_NUL.
Also document the error set by g_{locale,filename}_{from,to}_utf8()
when the input string contains nul bytes.
https://bugzilla.gnome.org/show_bug.cgi?id=792516
2018-01-14 15:55:03 +01:00
|
|
|
*
|
2018-02-08 22:33:13 +01:00
|
|
|
* Returns: (type filename):
|
2012-11-03 13:52:29 +01:00
|
|
|
* The converted string, or %NULL on an error.
|
2001-01-05 22:22:47 +01:00
|
|
|
**/
|
2000-11-12 22:23:55 +01:00
|
|
|
gchar*
|
2001-01-05 22:22:47 +01:00
|
|
|
g_filename_from_utf8 (const gchar *utf8string,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 15:55:09 +02:00
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
2001-01-05 22:22:47 +01:00
|
|
|
GError **error)
|
2000-11-12 22:23:55 +01:00
|
|
|
{
|
2003-11-06 00:05:49 +01:00
|
|
|
const gchar *charset;
|
|
|
|
|
|
|
|
if (get_filename_charset (&charset))
|
2001-08-26 23:39:47 +02:00
|
|
|
return strdup_len (utf8string, len, bytes_read, bytes_written, error);
|
2003-11-06 00:05:49 +01:00
|
|
|
else
|
2018-01-17 08:25:23 +01:00
|
|
|
return convert_checked (utf8string, len, charset, "UTF-8",
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_INPUT |
|
|
|
|
CONVERT_CHECK_NO_NULS_IN_OUTPUT,
|
|
|
|
bytes_read, bytes_written, error);
|
2000-11-12 22:23:55 +01:00
|
|
|
}
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
/* Test of haystack has the needle prefix, comparing case
|
|
|
|
* insensitive. haystack may be UTF-8, but needle must
|
|
|
|
* contain only ascii. */
|
|
|
|
static gboolean
|
|
|
|
has_case_prefix (const gchar *haystack, const gchar *needle)
|
|
|
|
{
|
|
|
|
const gchar *h, *n;
|
|
|
|
|
|
|
|
/* Eat one character at a time. */
|
|
|
|
h = haystack;
|
|
|
|
n = needle;
|
|
|
|
|
|
|
|
while (*n && *h &&
|
|
|
|
g_ascii_tolower (*n) == g_ascii_tolower (*h))
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
h++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *n == '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
UNSAFE_ALL = 0x1, /* Escape all unsafe characters */
|
|
|
|
UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */
|
2003-03-30 23:24:58 +02:00
|
|
|
UNSAFE_PATH = 0x8, /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */
|
2001-08-26 23:39:47 +02:00
|
|
|
UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */
|
|
|
|
UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */
|
|
|
|
} UnsafeCharacterSet;
|
|
|
|
|
|
|
|
static const guchar acceptable[96] = {
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
/* A table of the ASCII chars from space (32) to DEL (127) */
|
|
|
|
/* ! " # $ % & ' ( ) * + , - . / */
|
2003-03-30 23:24:58 +02:00
|
|
|
0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
|
2003-03-30 23:24:58 +02:00
|
|
|
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
/* @ A B C D E F G H I J K L M N O */
|
2003-03-30 23:24:58 +02:00
|
|
|
0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
/* P Q R S T U V W X Y Z [ \ ] ^ _ */
|
|
|
|
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
|
|
|
|
/* ` a b c d e f g h i j k l m n o */
|
|
|
|
0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
|
|
|
|
/* p q r s t u v w x y z { | } ~ DEL */
|
|
|
|
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
|
2001-08-26 23:39:47 +02:00
|
|
|
};
|
|
|
|
|
2022-01-19 19:05:57 +01:00
|
|
|
static const gchar hex[] = "0123456789ABCDEF";
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
/* Note: This escape function works on file: URIs, but if you want to
|
|
|
|
* escape something else, please read RFC-2396 */
|
|
|
|
static gchar *
|
|
|
|
g_escape_uri_string (const gchar *string,
|
|
|
|
UnsafeCharacterSet mask)
|
|
|
|
{
|
|
|
|
#define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
|
|
|
|
|
|
|
|
const gchar *p;
|
|
|
|
gchar *q;
|
|
|
|
gchar *result;
|
|
|
|
int c;
|
|
|
|
gint unacceptable;
|
|
|
|
UnsafeCharacterSet use_mask;
|
|
|
|
|
|
|
|
g_return_val_if_fail (mask == UNSAFE_ALL
|
|
|
|
|| mask == UNSAFE_ALLOW_PLUS
|
|
|
|
|| mask == UNSAFE_PATH
|
|
|
|
|| mask == UNSAFE_HOST
|
|
|
|
|| mask == UNSAFE_SLASHES, NULL);
|
|
|
|
|
|
|
|
unacceptable = 0;
|
|
|
|
use_mask = mask;
|
|
|
|
for (p = string; *p != '\0'; p++)
|
|
|
|
{
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
c = (guchar) *p;
|
2001-08-26 23:39:47 +02:00
|
|
|
if (!ACCEPTABLE (c))
|
|
|
|
unacceptable++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = g_malloc (p - string + unacceptable * 2 + 1);
|
|
|
|
|
|
|
|
use_mask = mask;
|
|
|
|
for (q = result, p = string; *p != '\0'; p++)
|
|
|
|
{
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
c = (guchar) *p;
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
if (!ACCEPTABLE (c))
|
|
|
|
{
|
|
|
|
*q++ = '%'; /* means hex coming */
|
|
|
|
*q++ = hex[c >> 4];
|
|
|
|
*q++ = hex[c & 15];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*q++ = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*q = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
g_escape_file_uri (const gchar *hostname,
|
|
|
|
const gchar *pathname)
|
|
|
|
{
|
|
|
|
char *escaped_hostname = NULL;
|
|
|
|
char *escaped_path;
|
|
|
|
char *res;
|
|
|
|
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
char *p, *backslash;
|
|
|
|
|
|
|
|
/* Turn backslashes into forward slashes. That's what Netscape
|
|
|
|
* does, and they are actually more or less equivalent in Windows.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pathname = g_strdup (pathname);
|
|
|
|
p = (char *) pathname;
|
|
|
|
|
|
|
|
while ((backslash = strchr (p, '\\')) != NULL)
|
|
|
|
{
|
|
|
|
*backslash = '/';
|
|
|
|
p = backslash + 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
if (hostname && *hostname != '\0')
|
|
|
|
{
|
|
|
|
escaped_hostname = g_escape_uri_string (hostname, UNSAFE_HOST);
|
|
|
|
}
|
|
|
|
|
2003-03-30 23:24:58 +02:00
|
|
|
escaped_path = g_escape_uri_string (pathname, UNSAFE_PATH);
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
res = g_strconcat ("file://",
|
|
|
|
(escaped_hostname) ? escaped_hostname : "",
|
|
|
|
(*escaped_path != '/') ? "/" : "",
|
|
|
|
escaped_path,
|
|
|
|
NULL);
|
|
|
|
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
g_free ((char *) pathname);
|
|
|
|
#endif
|
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
g_free (escaped_hostname);
|
|
|
|
g_free (escaped_path);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
unescape_character (const char *scanner)
|
|
|
|
{
|
|
|
|
int first_digit;
|
|
|
|
int second_digit;
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
first_digit = g_ascii_xdigit_value (scanner[0]);
|
2001-08-26 23:39:47 +02:00
|
|
|
if (first_digit < 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
second_digit = g_ascii_xdigit_value (scanner[1]);
|
2001-08-26 23:39:47 +02:00
|
|
|
if (second_digit < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return (first_digit << 4) | second_digit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
2002-02-08 20:57:35 +01:00
|
|
|
g_unescape_uri_string (const char *escaped,
|
|
|
|
int len,
|
|
|
|
const char *illegal_escaped_characters,
|
|
|
|
gboolean ascii_must_not_be_escaped)
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
|
|
|
const gchar *in, *in_end;
|
|
|
|
gchar *out, *result;
|
2002-02-08 20:57:35 +01:00
|
|
|
int c;
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
if (escaped == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (escaped);
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
result = g_malloc (len + 1);
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
out = result;
|
2002-02-08 20:57:35 +01:00
|
|
|
for (in = escaped, in_end = escaped + len; in < in_end; in++)
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
2002-02-08 20:57:35 +01:00
|
|
|
c = *in;
|
|
|
|
|
|
|
|
if (c == '%')
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
2002-02-08 20:57:35 +01:00
|
|
|
/* catch partial escape sequences past the end of the substring */
|
|
|
|
if (in + 3 > in_end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
c = unescape_character (in + 1);
|
|
|
|
|
|
|
|
/* catch bad escape sequences and NUL characters */
|
|
|
|
if (c <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* catch escaped ASCII */
|
|
|
|
if (ascii_must_not_be_escaped && c <= 0x7F)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* catch other illegal escaped characters */
|
|
|
|
if (strchr (illegal_escaped_characters, c) != NULL)
|
|
|
|
break;
|
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
in += 2;
|
|
|
|
}
|
2002-02-08 20:57:35 +01:00
|
|
|
|
|
|
|
*out++ = c;
|
2001-08-26 23:39:47 +02:00
|
|
|
}
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
g_assert (out - result <= len);
|
2001-08-26 23:39:47 +02:00
|
|
|
*out = '\0';
|
|
|
|
|
2004-07-30 21:00:01 +02:00
|
|
|
if (in != in_end)
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
|
|
|
g_free (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-02-08 20:57:35 +01:00
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
static gboolean
|
2004-06-08 05:27:03 +02:00
|
|
|
is_asciialphanum (gunichar c)
|
2002-02-08 20:57:35 +01:00
|
|
|
{
|
2004-06-08 05:27:03 +02:00
|
|
|
return c <= 0x7F && g_ascii_isalnum (c);
|
2002-02-08 20:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2004-06-08 05:27:03 +02:00
|
|
|
is_asciialpha (gunichar c)
|
2002-02-08 20:57:35 +01:00
|
|
|
{
|
2004-06-08 05:27:03 +02:00
|
|
|
return c <= 0x7F && g_ascii_isalpha (c);
|
2002-02-08 20:57:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allows an empty string */
|
|
|
|
static gboolean
|
|
|
|
hostname_validate (const char *hostname)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
gunichar c, first_char, last_char;
|
|
|
|
|
|
|
|
p = hostname;
|
|
|
|
if (*p == '\0')
|
|
|
|
return TRUE;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* read in a label */
|
|
|
|
c = g_utf8_get_char (p);
|
|
|
|
p = g_utf8_next_char (p);
|
2004-06-08 05:27:03 +02:00
|
|
|
if (!is_asciialphanum (c))
|
2002-02-08 20:57:35 +01:00
|
|
|
return FALSE;
|
|
|
|
first_char = c;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
last_char = c;
|
|
|
|
c = g_utf8_get_char (p);
|
|
|
|
p = g_utf8_next_char (p);
|
|
|
|
}
|
2004-06-08 05:27:03 +02:00
|
|
|
while (is_asciialphanum (c) || c == '-');
|
2002-02-08 20:57:35 +01:00
|
|
|
if (last_char == '-')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* if that was the last label, check that it was a toplabel */
|
|
|
|
if (c == '\0' || (c == '.' && *p == '\0'))
|
2004-06-08 05:27:03 +02:00
|
|
|
return is_asciialpha (first_char);
|
2002-02-08 20:57:35 +01:00
|
|
|
}
|
|
|
|
while (c == '.');
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
/**
|
|
|
|
* g_filename_from_uri:
|
2004-06-08 05:27:03 +02:00
|
|
|
* @uri: a uri describing a filename (escaped, encoded in ASCII).
|
2017-04-28 13:29:44 +02:00
|
|
|
* @hostname: (out) (optional) (nullable): Location to store hostname for the URI.
|
2001-08-26 23:39:47 +02:00
|
|
|
* If there is no hostname in the URI, %NULL will be
|
|
|
|
* stored in this location.
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-08-26 23:39:47 +02:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
2004-06-08 05:27:03 +02:00
|
|
|
* Converts an escaped ASCII-encoded URI to a local filename in the
|
2001-08-26 23:39:47 +02:00
|
|
|
* encoding used for filenames.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: (type filename): a newly-allocated string holding
|
2012-03-27 16:52:24 +02:00
|
|
|
* the resulting filename, or %NULL on an error.
|
2001-08-26 23:39:47 +02:00
|
|
|
**/
|
|
|
|
gchar *
|
Use gchar, gint, gsize instead of char, int, size_t in the interface for
2003-07-29 Matthias Clasen <maclas@gmx.de>
* glib/gqsort.[hc] (g_qsort_with_data):
* glib/gconvert.[hc] (g_filename_to_uri, g_filename_from_uri):
* glib/gfileutils.[hc] (g_mkstemp, g_file_open_tmp): Use gchar, gint, gsize instead of char, int, size_t
in the interface for consistency. (#118567)
2003-07-29 20:59:23 +02:00
|
|
|
g_filename_from_uri (const gchar *uri,
|
|
|
|
gchar **hostname,
|
|
|
|
GError **error)
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
|
|
|
const char *path_part;
|
|
|
|
const char *host_part;
|
|
|
|
char *unescaped_hostname;
|
|
|
|
char *result;
|
|
|
|
char *filename;
|
|
|
|
int offs;
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
char *p, *slash;
|
|
|
|
#endif
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
if (hostname)
|
|
|
|
*hostname = NULL;
|
|
|
|
|
|
|
|
if (!has_case_prefix (uri, "file:/"))
|
|
|
|
{
|
2001-10-01 22:40:05 +02:00
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The URI “%s” is not an absolute URI using the “file” scheme"),
|
2001-08-26 23:39:47 +02:00
|
|
|
uri);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
path_part = uri + strlen ("file:");
|
|
|
|
|
|
|
|
if (strchr (path_part, '#') != NULL)
|
|
|
|
{
|
2001-10-01 22:40:05 +02:00
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The local file URI “%s” may not include a “#”"),
|
2001-08-26 23:39:47 +02:00
|
|
|
uri);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_case_prefix (path_part, "///"))
|
|
|
|
path_part += 2;
|
|
|
|
else if (has_case_prefix (path_part, "//"))
|
|
|
|
{
|
|
|
|
path_part += 2;
|
|
|
|
host_part = path_part;
|
|
|
|
|
|
|
|
path_part = strchr (path_part, '/');
|
|
|
|
|
|
|
|
if (path_part == NULL)
|
|
|
|
{
|
2001-10-01 22:40:05 +02:00
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The URI “%s” is invalid"),
|
2001-08-26 23:39:47 +02:00
|
|
|
uri);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
unescaped_hostname = g_unescape_uri_string (host_part, path_part - host_part, "", TRUE);
|
|
|
|
|
|
|
|
if (unescaped_hostname == NULL ||
|
|
|
|
!hostname_validate (unescaped_hostname))
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
2002-02-08 20:57:35 +01:00
|
|
|
g_free (unescaped_hostname);
|
2001-10-01 22:40:05 +02:00
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The hostname of the URI “%s” is invalid"),
|
2001-08-26 23:39:47 +02:00
|
|
|
uri);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hostname)
|
|
|
|
*hostname = unescaped_hostname;
|
|
|
|
else
|
|
|
|
g_free (unescaped_hostname);
|
|
|
|
}
|
|
|
|
|
2002-02-08 20:57:35 +01:00
|
|
|
filename = g_unescape_uri_string (path_part, -1, "/", FALSE);
|
2001-08-26 23:39:47 +02:00
|
|
|
|
|
|
|
if (filename == NULL)
|
|
|
|
{
|
2001-10-01 22:40:05 +02:00
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The URI “%s” contains invalidly escaped characters"),
|
2001-08-26 23:39:47 +02:00
|
|
|
uri);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
offs = 0;
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* Drop localhost */
|
|
|
|
if (hostname && *hostname != NULL &&
|
|
|
|
g_ascii_strcasecmp (*hostname, "localhost") == 0)
|
|
|
|
{
|
|
|
|
g_free (*hostname);
|
|
|
|
*hostname = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn slashes into backslashes, because that's the canonical spelling */
|
|
|
|
p = filename;
|
|
|
|
while ((slash = strchr (p, '/')) != NULL)
|
|
|
|
{
|
|
|
|
*slash = '\\';
|
|
|
|
p = slash + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Windows URIs with a drive letter can be like "file://host/c:/foo"
|
|
|
|
* or "file://host/c|/foo" (some Netscape versions). In those cases, start
|
|
|
|
* the filename from the drive letter.
|
|
|
|
*/
|
|
|
|
if (g_ascii_isalpha (filename[1]))
|
|
|
|
{
|
|
|
|
if (filename[2] == ':')
|
|
|
|
offs = 1;
|
|
|
|
else if (filename[2] == '|')
|
|
|
|
{
|
|
|
|
filename[2] = ':';
|
|
|
|
offs = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-06-08 05:27:03 +02:00
|
|
|
|
|
|
|
result = g_strdup (filename + offs);
|
2001-08-26 23:39:47 +02:00
|
|
|
g_free (filename);
|
2004-06-08 05:27:03 +02:00
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_filename_to_uri:
|
2016-06-04 15:46:12 +02:00
|
|
|
* @filename: (type filename): an absolute filename specified in the GLib file
|
|
|
|
* name encoding, which is the on-disk file name bytes on Unix, and UTF-8
|
|
|
|
* on Windows
|
2016-10-29 03:29:02 +02:00
|
|
|
* @hostname: (nullable): A UTF-8 encoded hostname, or %NULL for none.
|
2011-08-29 20:49:32 +02:00
|
|
|
* @error: location to store the error occurring, or %NULL to ignore
|
2001-08-26 23:39:47 +02:00
|
|
|
* errors. Any of the errors in #GConvertError may occur.
|
|
|
|
*
|
2006-04-13 00:50:33 +02:00
|
|
|
* Converts an absolute filename to an escaped ASCII-encoded URI, with the path
|
|
|
|
* component following Section 3.3. of RFC 2396.
|
2001-08-26 23:39:47 +02:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a newly-allocated string holding the resulting
|
2001-08-26 23:39:47 +02:00
|
|
|
* URI, or %NULL on an error.
|
|
|
|
**/
|
|
|
|
gchar *
|
2004-11-02 22:29:33 +01:00
|
|
|
g_filename_to_uri (const gchar *filename,
|
|
|
|
const gchar *hostname,
|
|
|
|
GError **error)
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
|
|
|
char *escaped_uri;
|
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
|
|
|
|
if (!g_path_is_absolute (filename))
|
|
|
|
{
|
|
|
|
g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH,
|
2016-09-30 05:47:15 +02:00
|
|
|
_("The pathname “%s” is not an absolute path"),
|
2001-08-26 23:39:47 +02:00
|
|
|
filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hostname &&
|
2002-02-08 20:57:35 +01:00
|
|
|
!(g_utf8_validate (hostname, -1, NULL)
|
|
|
|
&& hostname_validate (hostname)))
|
2001-08-26 23:39:47 +02:00
|
|
|
{
|
2008-06-16 18:53:58 +02:00
|
|
|
g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
|
|
|
|
_("Invalid hostname"));
|
2001-08-26 23:39:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Not used on Windows, don't even compile it then.
2002-01-23 Tor Lillqvist <tml@iki.fi>
* glib/gconvert.c: (strdup_len): Not used on Windows, don't even
compile it then.
(acceptable): Improve comments for the _acceptable_ table: put
each ASCII char above the hex number for it.
(g_escape_file_uri): On Windows, turn backslashes in the file name
into plain ("forward") slashes.
(g_filename_from_uri): On Windows, don't return the hostname
"localhost", because we can not be 100% sure it will be recognized
in all cases anyway, so simpler to turn it into a null
hostname.
Change slashes in the filename into backslashes, as that is the
more canonical separator.
Recognize drive letters (either followed by a colon or a vertical
bar, as used by some browsers), and if the filename part starts
with a such, don't include any (back)slash.
Don't drop any extra leading slash in the filename on Unix.
(g_filename_to_uri): On Windows, if the hostname is "localhost",
don't use it, for consistency with g_filename_from_uri().
tests/uri-test.c: Change accordingly, so that all tests pass on
Windows. Unix, too, I hope, though I couldn't check that now.
(main): Unset the G_BROKEN_FILENAMES environment variable on Unix,
as some tests require that filenames are in UTF-8. Is unsetenv()
portable?
These changes should fix bugs #59387, #59652, #59657 and #59658.
2002-01-23 01:05:30 +01:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* Don't use localhost unnecessarily */
|
|
|
|
if (hostname && g_ascii_strcasecmp (hostname, "localhost") == 0)
|
|
|
|
hostname = NULL;
|
|
|
|
#endif
|
|
|
|
|
2004-06-08 05:27:03 +02:00
|
|
|
escaped_uri = g_escape_file_uri (hostname, filename);
|
|
|
|
|
2001-08-26 23:39:47 +02:00
|
|
|
return escaped_uri;
|
|
|
|
}
|
2004-10-22 21:51:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_uri_list_extract_uris:
|
|
|
|
* @uri_list: an URI list
|
|
|
|
*
|
|
|
|
* Splits an URI list conforming to the text/uri-list
|
|
|
|
* mime type defined in RFC 2483 into individual URIs,
|
|
|
|
* discarding any comments. The URIs are not validated.
|
|
|
|
*
|
2012-07-12 16:09:57 +02:00
|
|
|
* Returns: (transfer full): a newly allocated %NULL-terminated list
|
|
|
|
* of strings holding the individual URIs. The array should be freed
|
|
|
|
* with g_strfreev().
|
2004-10-22 21:51:29 +02:00
|
|
|
*
|
|
|
|
* Since: 2.6
|
|
|
|
*/
|
|
|
|
gchar **
|
|
|
|
g_uri_list_extract_uris (const gchar *uri_list)
|
|
|
|
{
|
2020-07-03 21:10:40 +02:00
|
|
|
GPtrArray *uris;
|
2004-10-22 21:51:29 +02:00
|
|
|
const gchar *p, *q;
|
|
|
|
|
2020-07-03 21:10:40 +02:00
|
|
|
uris = g_ptr_array_new ();
|
2004-10-22 21:51:29 +02:00
|
|
|
|
|
|
|
p = uri_list;
|
|
|
|
|
|
|
|
/* We don't actually try to validate the URI according to RFC
|
|
|
|
* 2396, or even check for allowed characters - we just ignore
|
|
|
|
* comments and trim whitespace off the ends. We also
|
|
|
|
* allow LF delimination as well as the specified CRLF.
|
|
|
|
*
|
|
|
|
* We do allow comments like specified in RFC 2483.
|
|
|
|
*/
|
|
|
|
while (p)
|
|
|
|
{
|
|
|
|
if (*p != '#')
|
|
|
|
{
|
|
|
|
while (g_ascii_isspace (*p))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
q = p;
|
|
|
|
while (*q && (*q != '\n') && (*q != '\r'))
|
|
|
|
q++;
|
|
|
|
|
|
|
|
if (q > p)
|
|
|
|
{
|
|
|
|
q--;
|
|
|
|
while (q > p && g_ascii_isspace (*q))
|
|
|
|
q--;
|
|
|
|
|
|
|
|
if (q > p)
|
2020-07-03 21:10:40 +02:00
|
|
|
g_ptr_array_add (uris, g_strndup (p, q - p + 1));
|
|
|
|
}
|
|
|
|
}
|
2004-10-22 21:51:29 +02:00
|
|
|
p = strchr (p, '\n');
|
|
|
|
if (p)
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2020-07-03 21:10:40 +02:00
|
|
|
g_ptr_array_add (uris, NULL);
|
2004-10-22 21:51:29 +02:00
|
|
|
|
2020-07-03 21:10:40 +02:00
|
|
|
return (gchar **) g_ptr_array_free (uris, FALSE);
|
2004-10-22 21:51:29 +02:00
|
|
|
}
|
2004-11-02 22:29:33 +01:00
|
|
|
|
2004-12-15 17:34:49 +01:00
|
|
|
/**
|
|
|
|
* g_filename_display_basename:
|
2016-06-04 15:46:12 +02:00
|
|
|
* @filename: (type filename): an absolute pathname in the
|
|
|
|
* GLib file name encoding
|
2004-12-15 17:34:49 +01:00
|
|
|
*
|
|
|
|
* Returns the display basename for the particular filename, guaranteed
|
|
|
|
* to be valid UTF-8. The display name might not be identical to the filename,
|
|
|
|
* for instance there might be problems converting it to UTF-8, and some files
|
2006-01-16 15:54:45 +01:00
|
|
|
* can be translated in the display.
|
|
|
|
*
|
2011-03-24 09:33:55 +01:00
|
|
|
* If GLib cannot make sense of the encoding of @filename, as a last resort it
|
2006-01-16 15:54:45 +01:00
|
|
|
* replaces unknown characters with U+FFFD, the Unicode replacement character.
|
|
|
|
* You can search the result for the UTF-8 encoding of this character (which is
|
|
|
|
* "\357\277\275" in octal notation) to find out if @filename was in an invalid
|
|
|
|
* encoding.
|
2004-12-15 17:34:49 +01:00
|
|
|
*
|
|
|
|
* You must pass the whole absolute pathname to this functions so that
|
|
|
|
* translation of well known locations can be done.
|
|
|
|
*
|
2004-12-15 17:42:28 +01:00
|
|
|
* This function is preferred over g_filename_display_name() if you know the
|
2004-12-15 17:34:49 +01:00
|
|
|
* whole path, as it allows translation.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a newly allocated string containing
|
2004-12-15 17:34:49 +01:00
|
|
|
* a rendition of the basename of the filename in valid UTF-8
|
|
|
|
*
|
|
|
|
* Since: 2.6
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_filename_display_basename (const gchar *filename)
|
|
|
|
{
|
|
|
|
char *basename;
|
|
|
|
char *display_name;
|
2004-12-15 17:42:28 +01:00
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
2004-12-15 17:34:49 +01:00
|
|
|
|
|
|
|
basename = g_path_get_basename (filename);
|
|
|
|
display_name = g_filename_display_name (basename);
|
|
|
|
g_free (basename);
|
|
|
|
return display_name;
|
|
|
|
}
|
|
|
|
|
2004-11-02 22:29:33 +01:00
|
|
|
/**
|
|
|
|
* g_filename_display_name:
|
2016-06-04 15:46:12 +02:00
|
|
|
* @filename: (type filename): a pathname hopefully in the
|
|
|
|
* GLib file name encoding
|
2004-11-02 22:29:33 +01:00
|
|
|
*
|
2006-01-16 15:54:45 +01:00
|
|
|
* Converts a filename into a valid UTF-8 string. The conversion is
|
|
|
|
* not necessarily reversible, so you should keep the original around
|
|
|
|
* and use the return value of this function only for display purposes.
|
|
|
|
* Unlike g_filename_to_utf8(), the result is guaranteed to be non-%NULL
|
|
|
|
* even if the filename actually isn't in the GLib file name encoding.
|
|
|
|
*
|
2011-03-24 09:33:55 +01:00
|
|
|
* If GLib cannot make sense of the encoding of @filename, as a last resort it
|
2006-01-16 15:54:45 +01:00
|
|
|
* replaces unknown characters with U+FFFD, the Unicode replacement character.
|
|
|
|
* You can search the result for the UTF-8 encoding of this character (which is
|
|
|
|
* "\357\277\275" in octal notation) to find out if @filename was in an invalid
|
|
|
|
* encoding.
|
2004-11-02 22:29:33 +01:00
|
|
|
*
|
2004-12-15 17:34:49 +01:00
|
|
|
* If you know the whole pathname of the file you should use
|
|
|
|
* g_filename_display_basename(), since that allows location-based
|
|
|
|
* translation of filenames.
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a newly allocated string containing
|
2004-11-02 22:29:33 +01:00
|
|
|
* a rendition of the filename in valid UTF-8
|
|
|
|
*
|
|
|
|
* Since: 2.6
|
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_filename_display_name (const gchar *filename)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
const gchar **charsets;
|
|
|
|
gchar *display_name = NULL;
|
|
|
|
gboolean is_utf8;
|
|
|
|
|
|
|
|
is_utf8 = g_get_filename_charsets (&charsets);
|
|
|
|
|
|
|
|
if (is_utf8)
|
|
|
|
{
|
|
|
|
if (g_utf8_validate (filename, -1, NULL))
|
|
|
|
display_name = g_strdup (filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!display_name)
|
|
|
|
{
|
|
|
|
/* Try to convert from the filename charsets to UTF-8.
|
|
|
|
* Skip the first charset if it is UTF-8.
|
|
|
|
*/
|
|
|
|
for (i = is_utf8 ? 1 : 0; charsets[i]; i++)
|
|
|
|
{
|
|
|
|
display_name = g_convert (filename, -1, "UTF-8", charsets[i],
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (display_name)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if all conversions failed, we replace invalid UTF-8
|
|
|
|
* by a question mark
|
|
|
|
*/
|
|
|
|
if (!display_name)
|
2017-03-02 09:10:35 +01:00
|
|
|
display_name = g_utf8_make_valid (filename, -1);
|
2004-11-02 22:29:33 +01:00
|
|
|
|
|
|
|
return display_name;
|
|
|
|
}
|
2017-03-28 07:59:09 +02:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
|
|
|
/* Binary compatibility versions. Not for newly compiled code. */
|
|
|
|
|
|
|
|
_GLIB_EXTERN gchar *g_filename_to_utf8_utf8 (const gchar *opsysstring,
|
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error) G_GNUC_MALLOC;
|
|
|
|
_GLIB_EXTERN gchar *g_filename_from_utf8_utf8 (const gchar *utf8string,
|
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error) G_GNUC_MALLOC;
|
|
|
|
_GLIB_EXTERN gchar *g_filename_from_uri_utf8 (const gchar *uri,
|
|
|
|
gchar **hostname,
|
|
|
|
GError **error) G_GNUC_MALLOC;
|
|
|
|
_GLIB_EXTERN gchar *g_filename_to_uri_utf8 (const gchar *filename,
|
|
|
|
const gchar *hostname,
|
|
|
|
GError **error) G_GNUC_MALLOC;
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
g_filename_to_utf8_utf8 (const gchar *opsysstring,
|
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return g_filename_to_utf8 (opsysstring, len, bytes_read, bytes_written, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
g_filename_from_utf8_utf8 (const gchar *utf8string,
|
|
|
|
gssize len,
|
|
|
|
gsize *bytes_read,
|
|
|
|
gsize *bytes_written,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return g_filename_from_utf8 (utf8string, len, bytes_read, bytes_written, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
g_filename_from_uri_utf8 (const gchar *uri,
|
|
|
|
gchar **hostname,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return g_filename_from_uri (uri, hostname, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
g_filename_to_uri_utf8 (const gchar *filename,
|
|
|
|
const gchar *hostname,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return g_filename_to_uri (filename, hostname, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|