Merge branch 'uri' into 'master'

guri: new URI parsing and generating functions

See merge request GNOME/glib!1328
This commit is contained in:
Philip Withnall 2020-06-25 11:52:07 +00:00
commit 6801e06d83
20 changed files with 3794 additions and 444 deletions

View File

@ -138,6 +138,8 @@ installed-tests:
_build
- ninja -C _build
- sudo ninja -C _build install
# Remove old headers, possibly present in current installation
- sudo rm -f /usr/include/glib-2.0/glib/gurifuncs.h
- sudo chown -R `id -un`:`id -gn` _build/
# FIXME Install newer xdg-desktop-portal with
# GMemoryMonitor support, see:

View File

@ -81,7 +81,7 @@
<xi:include href="xml/timers.xml" />
<xi:include href="xml/spawn.xml" />
<xi:include href="xml/fileutils.xml" />
<xi:include href="xml/gurifuncs.xml" />
<xi:include href="xml/guri.xml" />
<xi:include href="xml/ghostutils.xml" />
<xi:include href="xml/shell.xml" />
<xi:include href="xml/option.xml" />

View File

@ -3335,19 +3335,63 @@ g_base64_decode_inplace
<SECTION>
<TITLE>URI Functions</TITLE>
<FILE>gurifuncs</FILE>
<FILE>guri</FILE>
GUri
g_uri_ref
g_uri_unref
<SUBSECTION>
GUriFlags
g_uri_split
g_uri_split_with_user
g_uri_split_network
g_uri_is_valid
g_uri_join
g_uri_join_with_user
g_uri_parse
g_uri_parse_relative
g_uri_resolve_relative
g_uri_build
g_uri_build_with_user
g_uri_peek_scheme
g_uri_parse_scheme
<SUBSECTION>
GUriHideFlags
g_uri_to_string
g_uri_to_string_partial
<SUBSECTION>
g_uri_get_scheme
g_uri_get_userinfo
g_uri_get_user
g_uri_get_password
g_uri_get_auth_params
g_uri_get_host
g_uri_get_port
g_uri_get_path
g_uri_get_query
g_uri_get_fragment
g_uri_get_flags
<SUBSECTION>
g_uri_parse_params
<SUBSECTION>
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT
G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO
G_URI_RESERVED_CHARS_GENERIC_DELIMITERS
G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS
g_uri_parse_scheme
g_uri_escape_string
g_uri_unescape_string
g_uri_escape_bytes
g_uri_unescape_bytes
g_uri_unescape_segment
<SUBSECTION>
g_uri_list_extract_uris
g_filename_from_uri
g_filename_to_uri
<SUBSECTION>
G_URI_ERROR
GUriError
<SUBSECTION Private>
g_uri_error_quark
</SECTION>
<SECTION>

View File

@ -408,6 +408,7 @@ G_TYPE_SOURCE
G_TYPE_POLLFD
G_TYPE_THREAD
G_TYPE_OPTION_GROUP
G_TYPE_URI
<SUBSECTION Standard>
G_TYPE_IS_BOXED
@ -441,6 +442,7 @@ g_mapped_file_get_type
g_markup_parse_context_get_type
g_thread_get_type
g_option_group_get_type
g_uri_get_type
</SECTION>
<SECTION>

29
fuzzing/fuzz_uri_escape.c Normal file
View File

@ -0,0 +1,29 @@
#include "fuzz.h"
int
LLVMFuzzerTestOneInput (const unsigned char *data, size_t size)
{
GBytes *unescaped_bytes = NULL;
gchar *escaped_string = NULL;
fuzz_set_logging_func ();
if (size > G_MAXSSIZE)
return 0;
unescaped_bytes = g_uri_unescape_bytes ((const gchar *) data, (gssize) size);
if (unescaped_bytes == NULL)
return 0;
escaped_string = g_uri_escape_bytes (g_bytes_get_data (unescaped_bytes, NULL),
g_bytes_get_size (unescaped_bytes),
NULL);
g_bytes_unref (unescaped_bytes);
if (escaped_string == NULL)
return 0;
g_free (escaped_string);
return 0;
}

26
fuzzing/fuzz_uri_parse.c Normal file
View File

@ -0,0 +1,26 @@
#include "fuzz.h"
int
LLVMFuzzerTestOneInput (const unsigned char *data, size_t size)
{
GUri *uri = NULL;
gchar *uri_string = NULL;
const GUriFlags flags = G_URI_FLAGS_NONE;
fuzz_set_logging_func ();
/* ignore @size */
uri = g_uri_parse ((const gchar *) data, flags, NULL);
if (uri == NULL)
return 0;
uri_string = g_uri_to_string (uri);
g_uri_unref (uri);
if (uri_string == NULL)
return 0;
g_free (uri_string);
return 0;
}

View File

@ -0,0 +1,20 @@
#include "fuzz.h"
int
LLVMFuzzerTestOneInput (const unsigned char *data, size_t size)
{
GHashTable *parsed_params = NULL;
fuzz_set_logging_func ();
if (size > G_MAXSSIZE)
return 0;
parsed_params = g_uri_parse_params ((const gchar *) data, (gssize) size, '&', FALSE);
if (parsed_params == NULL)
return 0;
g_hash_table_unref (parsed_params);
return 0;
}

View File

@ -2,6 +2,9 @@ fuzz_targets = [
'fuzz_bookmark',
'fuzz_dbus_message',
'fuzz_key',
'fuzz_uri_escape',
'fuzz_uri_parse',
'fuzz_uri_parse_params',
'fuzz_variant_binary',
'fuzz_variant_text',
]

View File

@ -96,6 +96,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariantDict, g_variant_dict_unref)
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GVariantDict, g_variant_dict_clear)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GVariantType, g_variant_type_free)
G_DEFINE_AUTO_CLEANUP_FREE_FUNC(GStrv, g_strfreev, NULL)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GRefString, g_ref_string_release)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GRefString, g_ref_string_release)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GUri, g_uri_unref)
G_GNUC_END_IGNORE_DEPRECATIONS

View File

@ -91,7 +91,7 @@
#include <glib/gtree.h>
#include <glib/gtypes.h>
#include <glib/gunicode.h>
#include <glib/gurifuncs.h>
#include <glib/guri.h>
#include <glib/gutils.h>
#include <glib/guuid.h>
#include <glib/gvariant.h>

View File

@ -35,7 +35,7 @@
#include <ctype.h>
#include "gstring.h"
#include "guriprivate.h"
#include "gprintf.h"
@ -506,34 +506,6 @@ g_string_insert_len (GString *string,
return string;
}
#define SUB_DELIM_CHARS "!$&'()*+,;="
static gboolean
is_valid (char c,
const char *reserved_chars_allowed)
{
if (g_ascii_isalnum (c) ||
c == '-' ||
c == '.' ||
c == '_' ||
c == '~')
return TRUE;
if (reserved_chars_allowed &&
strchr (reserved_chars_allowed, c) != NULL)
return TRUE;
return FALSE;
}
static gboolean
gunichar_ok (gunichar c)
{
return
(c != (gunichar) -2) &&
(c != (gunichar) -1);
}
/**
* g_string_append_uri_escaped:
* @string: a #GString
@ -542,7 +514,7 @@ gunichar_ok (gunichar c)
* to be used, or %NULL
* @allow_utf8: set %TRUE if the escaped string may include UTF8 characters
*
* Appends @unescaped to @string, escaped any characters that
* Appends @unescaped to @string, escaping any characters that
* are reserved in URIs using URI-style escape sequences.
*
* Returns: (transfer none): @string
@ -555,38 +527,8 @@ g_string_append_uri_escaped (GString *string,
const gchar *reserved_chars_allowed,
gboolean allow_utf8)
{
unsigned char c;
const gchar *end;
static const gchar hex[16] = "0123456789ABCDEF";
g_return_val_if_fail (string != NULL, NULL);
g_return_val_if_fail (unescaped != NULL, NULL);
end = unescaped + strlen (unescaped);
while ((c = *unescaped) != 0)
{
if (c >= 0x80 && allow_utf8 &&
gunichar_ok (g_utf8_get_char_validated (unescaped, end - unescaped)))
{
int len = g_utf8_skip [c];
g_string_append_len (string, unescaped, len);
unescaped += len;
}
else if (is_valid (c, reserved_chars_allowed))
{
g_string_append_c (string, c);
unescaped++;
}
else
{
g_string_append_c (string, '%');
g_string_append_c (string, hex[((guchar)c) >> 4]);
g_string_append_c (string, hex[((guchar)c) & 0xf]);
unescaped++;
}
}
_uri_encoder (string, (const guchar *) unescaped, strlen (unescaped),
reserved_chars_allowed, allow_utf8);
return string;
}

2350
glib/guri.c Normal file

File diff suppressed because it is too large Load Diff

347
glib/guri.h Normal file
View File

@ -0,0 +1,347 @@
/* GLIB - Library of useful routines for C programming
* Copyright © 2020 Red Hat, Inc.
*
* 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
* version 2 of the License, or (at your option) any later version.
*
* 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 License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
#pragma once
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
#error "Only <glib.h> can be included directly."
#endif
#include <glib/gtypes.h>
G_BEGIN_DECLS
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
typedef struct _GUri GUri;
GLIB_AVAILABLE_IN_2_66
GUri * g_uri_ref (GUri *uri);
GLIB_AVAILABLE_IN_2_66
void g_uri_unref (GUri *uri);
/**
* GUriFlags:
* @G_URI_FLAGS_PARSE_STRICT: Parse the URI strictly according to the RFC
* 3986 grammar, rather than fixing up or ignoring common mistakes.
* @G_URI_FLAGS_HAS_PASSWORD: The userinfo field may contain a password,
* which will be separated from the username by ':'.
* @G_URI_FLAGS_HAS_AUTH_PARAMS: The userinfo may contain additional
* authentication-related parameters, which will be separated from
* the username and/or password by ';'.
* @G_URI_FLAGS_NON_DNS: The host component should not be assumed to be a
* DNS hostname or IP address. (Eg, for `smb` URIs with NetBIOS
* hostnames).
* @G_URI_FLAGS_ENCODED: When parsing a URI, this indicates that `%`-encoded
* characters in the userinfo, path, query, and fragment fields
* should not be decoded. (And likewise the host field if
* %G_URI_FLAGS_NON_DNS is also set.) When building a URI, it indicates
* that you have already `%`-encoded the components, and so #GUri
* should not do any encoding itself.
* @G_URI_FLAGS_NONE: No flags set.
*
* Flags that describe a URI.
*
* When parsing a URI, if you need to choose different flags based on
* the type of URI, you can use g_uri_peek_scheme() on the URI string
* to check the scheme first, and use that to decide what flags to
* parse it with.
*
* Since: 2.66
*/
GLIB_AVAILABLE_TYPE_IN_2_66
typedef enum {
G_URI_FLAGS_NONE = 0,
G_URI_FLAGS_PARSE_STRICT = 1 << 0,
G_URI_FLAGS_HAS_PASSWORD = 1 << 1,
G_URI_FLAGS_HAS_AUTH_PARAMS = 1 << 2,
G_URI_FLAGS_ENCODED = 1 << 3,
G_URI_FLAGS_NON_DNS = 1 << 4,
} GUriFlags;
GLIB_AVAILABLE_IN_2_66
gboolean g_uri_split (const gchar *uri_string,
GUriFlags flags,
gchar **scheme,
gchar **userinfo,
gchar **host,
gint *port,
gchar **path,
gchar **query,
gchar **fragment,
GError **error);
GLIB_AVAILABLE_IN_2_66
gboolean g_uri_split_with_user (const gchar *uri_string,
GUriFlags flags,
gchar **scheme,
gchar **user,
gchar **password,
gchar **auth_params,
gchar **host,
gint *port,
gchar **path,
gchar **query,
gchar **fragment,
GError **error);
GLIB_AVAILABLE_IN_2_66
gboolean g_uri_split_network (const gchar *uri_string,
GUriFlags flags,
gchar **scheme,
gchar **host,
gint *port,
GError **error);
GLIB_AVAILABLE_IN_2_66
gboolean g_uri_is_valid (const gchar *uri_string,
GUriFlags flags,
GError **error);
GLIB_AVAILABLE_IN_2_66
gchar * g_uri_join (GUriFlags flags,
const gchar *scheme,
const gchar *userinfo,
const gchar *host,
gint port,
const gchar *path,
const gchar *query,
const gchar *fragment);
GLIB_AVAILABLE_IN_2_66
gchar * g_uri_join_with_user (GUriFlags flags,
const gchar *scheme,
const gchar *user,
const gchar *password,
const gchar *auth_params,
const gchar *host,
gint port,
const gchar *path,
const gchar *query,
const gchar *fragment);
GLIB_AVAILABLE_IN_2_66
GUri * g_uri_parse (const gchar *uri_string,
GUriFlags flags,
GError **error);
GLIB_AVAILABLE_IN_2_66
GUri * g_uri_parse_relative (GUri *base_uri,
const gchar *uri_string,
GUriFlags flags,
GError **error);
GLIB_AVAILABLE_IN_2_66
gchar * g_uri_resolve_relative (const gchar *base_uri_string,
const gchar *uri_string,
GUriFlags flags,
GError **error);
GLIB_AVAILABLE_IN_2_66
GUri * g_uri_build (GUriFlags flags,
const gchar *scheme,
const gchar *userinfo,
const gchar *host,
gint port,
const gchar *path,
const gchar *query,
const gchar *fragment);
GLIB_AVAILABLE_IN_2_66
GUri * g_uri_build_with_user (GUriFlags flags,
const gchar *scheme,
const gchar *user,
const gchar *password,
const gchar *auth_params,
const gchar *host,
gint port,
const gchar *path,
const gchar *query,
const gchar *fragment);
/**
* GUriHideFlags:
* @G_URI_HIDE_USERINFO: Hide the userinfo.
* @G_URI_HIDE_PASSWORD: Hide the password.
* @G_URI_HIDE_AUTH_PARAMS: Hide the auth_params.
* @G_URI_HIDE_FRAGMENT: Hide the fragment.
* @G_URI_HIDE_NONE: No flags set.
*
* Flags describing what parts of the URI to hide in
* g_uri_to_string_partial(). Note that %G_URI_HIDE_PASSWORD and
* %G_URI_HIDE_AUTH_PARAMS will only work if the #GUri was parsed with
* the corresponding flags.
*
* Since: 2.66
*/
GLIB_AVAILABLE_TYPE_IN_2_66
typedef enum {
G_URI_HIDE_NONE = 0,
G_URI_HIDE_USERINFO = 1 << 0,
G_URI_HIDE_PASSWORD = 1 << 1,
G_URI_HIDE_AUTH_PARAMS = 1 << 2,
G_URI_HIDE_FRAGMENT = 1 << 3,
} GUriHideFlags;
GLIB_AVAILABLE_IN_2_66
char * g_uri_to_string (GUri *uri);
GLIB_AVAILABLE_IN_2_66
char * g_uri_to_string_partial (GUri *uri,
GUriHideFlags flags);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_scheme (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_userinfo (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_user (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_password (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_auth_params (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_host (GUri *uri);
GLIB_AVAILABLE_IN_2_66
gint g_uri_get_port (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_path (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_query (GUri *uri);
GLIB_AVAILABLE_IN_2_66
const gchar *g_uri_get_fragment (GUri *uri);
GLIB_AVAILABLE_IN_2_66
GUriFlags g_uri_get_flags (GUri *uri);
GLIB_AVAILABLE_IN_2_66
GHashTable * g_uri_parse_params (const gchar *params,
gssize length,
gchar separator,
gboolean case_insensitive);
/**
* G_URI_ERROR:
*
* Error domain for URI methods. Errors in this domain will be from
* the #GUriError enumeration. See #GError for information on error
* domains.
*
* Since: 2.66
*/
#define G_URI_ERROR (g_uri_error_quark ()) GLIB_AVAILABLE_MACRO_IN_2_66
GLIB_AVAILABLE_IN_2_66
GQuark g_uri_error_quark (void);
/**
* GUriError:
* @G_URI_ERROR_MISC: miscellaneous error
* @G_URI_ERROR_BAD_SCHEME: the scheme of a URI could not be parsed.
* @G_URI_ERROR_BAD_USER: the user/userinfo of a URI could not be parsed.
* @G_URI_ERROR_BAD_PASSWORD: the password of a URI could not be parsed.
* @G_URI_ERROR_BAD_AUTH_PARAMS: the authentication parameters of a URI could not be parsed.
* @G_URI_ERROR_BAD_HOST: the host of a URI could not be parsed.
* @G_URI_ERROR_BAD_PORT: the port of a URI could not be parsed.
* @G_URI_ERROR_BAD_PATH: the path of a URI could not be parsed.
* @G_URI_ERROR_BAD_QUERY: the query of a URI could not be parsed.
* @G_URI_ERROR_BAD_FRAGMENT: the fragment of a URI could not be parsed.
*
* Error codes returned by #GUri methods.
*
* Since: 2.66
*/
typedef enum {
G_URI_ERROR_MISC,
G_URI_ERROR_BAD_SCHEME,
G_URI_ERROR_BAD_USER,
G_URI_ERROR_BAD_PASSWORD,
G_URI_ERROR_BAD_AUTH_PARAMS,
G_URI_ERROR_BAD_HOST,
G_URI_ERROR_BAD_PORT,
G_URI_ERROR_BAD_PATH,
G_URI_ERROR_BAD_QUERY,
G_URI_ERROR_BAD_FRAGMENT,
} GUriError;
/**
* G_URI_RESERVED_CHARS_GENERIC_DELIMITERS:
*
* Generic delimiters characters as defined in RFC 3986. Includes ":/?#[]@".
*
* Since: 2.16
**/
#define G_URI_RESERVED_CHARS_GENERIC_DELIMITERS ":/?#[]@"
/**
* G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS:
*
* Subcomponent delimiter characters as defined in RFC 3986. Includes "!$&'()*+,;=".
*
* Since: 2.16
**/
#define G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS "!$&'()*+,;="
/**
* G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT:
*
* Allowed characters in path elements. Includes "!$&'()*+,;=:@".
*
* Since: 2.16
**/
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":@"
/**
* G_URI_RESERVED_CHARS_ALLOWED_IN_PATH:
*
* Allowed characters in a path. Includes "!$&'()*+,;=:@/".
*
* Since: 2.16
**/
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/"
/**
* G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO:
*
* Allowed characters in userinfo as defined in RFC 3986. Includes "!$&'()*+,;=:".
*
* Since: 2.16
**/
#define G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":"
GLIB_AVAILABLE_IN_ALL
char * g_uri_unescape_string (const char *escaped_string,
const char *illegal_characters);
GLIB_AVAILABLE_IN_ALL
char * g_uri_unescape_segment (const char *escaped_string,
const char *escaped_string_end,
const char *illegal_characters);
GLIB_AVAILABLE_IN_ALL
char * g_uri_parse_scheme (const char *uri);
GLIB_AVAILABLE_IN_2_66
const char *g_uri_peek_scheme (const char *uri);
GLIB_AVAILABLE_IN_ALL
char * g_uri_escape_string (const char *unescaped,
const char *reserved_chars_allowed,
gboolean allow_utf8);
GLIB_AVAILABLE_IN_2_66
GBytes * g_uri_unescape_bytes (const char *escaped_string,
gssize length);
GLIB_AVAILABLE_IN_2_66
char * g_uri_escape_bytes (const guchar *unescaped,
gsize length,
const char *reserved_chars_allowed);
G_GNUC_END_IGNORE_DEPRECATIONS
G_END_DECLS

View File

@ -1,252 +0,0 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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 License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include "gurifuncs.h"
#include <glib/gstrfuncs.h>
#include <glib/gmessages.h>
#include <glib/gstring.h>
#include <glib/gmem.h>
#include <string.h>
#include "config.h"
/**
* SECTION:gurifuncs
* @title: URI Functions
* @short_description: manipulating URIs
*
* Functions for manipulating Universal Resource Identifiers (URIs) as
* defined by
* [RFC 3986](http://www.ietf.org/rfc/rfc3986.txt).
* It is highly recommended that you have read and
* understand RFC 3986 for understanding this API.
*/
static int
unescape_character (const char *scanner)
{
int first_digit;
int second_digit;
first_digit = g_ascii_xdigit_value (*scanner++);
if (first_digit < 0)
return -1;
second_digit = g_ascii_xdigit_value (*scanner++);
if (second_digit < 0)
return -1;
return (first_digit << 4) | second_digit;
}
/**
* g_uri_unescape_segment:
* @escaped_string: (nullable): A string, may be %NULL
* @escaped_string_end: (nullable): Pointer to end of @escaped_string, may be %NULL
* @illegal_characters: (nullable): An optional string of illegal characters not to be allowed, may be %NULL
*
* Unescapes a segment of an escaped string.
*
* If any of the characters in @illegal_characters or the character zero appears
* as an escaped character in @escaped_string then that is an error and %NULL
* will be returned. This is useful it you want to avoid for instance having a
* slash being expanded in an escaped path element, which might confuse pathname
* handling.
*
* Returns: an unescaped version of @escaped_string or %NULL on error.
* The returned string should be freed when no longer needed. As a
* special case if %NULL is given for @escaped_string, this function
* will return %NULL.
*
* Since: 2.16
**/
char *
g_uri_unescape_segment (const char *escaped_string,
const char *escaped_string_end,
const char *illegal_characters)
{
const char *in;
char *out, *result;
gint character;
if (escaped_string == NULL)
return NULL;
if (escaped_string_end == NULL)
escaped_string_end = escaped_string + strlen (escaped_string);
result = g_malloc (escaped_string_end - escaped_string + 1);
out = result;
for (in = escaped_string; in < escaped_string_end; in++)
{
character = *in;
if (*in == '%')
{
in++;
if (escaped_string_end - in < 2)
{
/* Invalid escaped char (to short) */
g_free (result);
return NULL;
}
character = unescape_character (in);
/* Check for an illegal character. We consider '\0' illegal here. */
if (character <= 0 ||
(illegal_characters != NULL &&
strchr (illegal_characters, (char)character) != NULL))
{
g_free (result);
return NULL;
}
in++; /* The other char will be eaten in the loop header */
}
*out++ = (char)character;
}
*out = '\0';
return result;
}
/**
* g_uri_unescape_string:
* @escaped_string: an escaped string to be unescaped.
* @illegal_characters: (nullable): a string of illegal characters not to be
* allowed, or %NULL.
*
* Unescapes a whole escaped string.
*
* If any of the characters in @illegal_characters or the character zero appears
* as an escaped character in @escaped_string then that is an error and %NULL
* will be returned. This is useful it you want to avoid for instance having a
* slash being expanded in an escaped path element, which might confuse pathname
* handling.
*
* Returns: an unescaped version of @escaped_string. The returned string
* should be freed when no longer needed.
*
* Since: 2.16
**/
char *
g_uri_unescape_string (const char *escaped_string,
const char *illegal_characters)
{
return g_uri_unescape_segment (escaped_string, NULL, illegal_characters);
}
/**
* g_uri_parse_scheme:
* @uri: a valid URI.
*
* Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
* |[
* URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
* ]|
* Common schemes include "file", "http", "svn+ssh", etc.
*
* Returns: The "Scheme" component of the URI, or %NULL on error.
* The returned string should be freed when no longer needed.
*
* Since: 2.16
**/
char *
g_uri_parse_scheme (const char *uri)
{
const char *p;
char c;
g_return_val_if_fail (uri != NULL, NULL);
/* From RFC 3986 Decodes:
* URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
*/
p = uri;
/* Decode scheme:
scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
*/
if (!g_ascii_isalpha (*p))
return NULL;
while (1)
{
c = *p++;
if (c == ':')
break;
if (!(g_ascii_isalnum(c) ||
c == '+' ||
c == '-' ||
c == '.'))
return NULL;
}
return g_strndup (uri, p - uri - 1);
}
/**
* g_uri_escape_string:
* @unescaped: the unescaped input string.
* @reserved_chars_allowed: (nullable): a string of reserved characters that
* are allowed to be used, or %NULL.
* @allow_utf8: %TRUE if the result can include UTF-8 characters.
*
* Escapes a string for use in a URI.
*
* Normally all characters that are not "unreserved" (i.e. ASCII alphanumerical
* characters plus dash, dot, underscore and tilde) are escaped.
* But if you specify characters in @reserved_chars_allowed they are not
* escaped. This is useful for the "reserved" characters in the URI
* specification, since those are allowed unescaped in some portions of
* a URI.
*
* Returns: an escaped version of @unescaped. The returned string should be
* freed when no longer needed.
*
* Since: 2.16
**/
char *
g_uri_escape_string (const char *unescaped,
const char *reserved_chars_allowed,
gboolean allow_utf8)
{
GString *s;
g_return_val_if_fail (unescaped != NULL, NULL);
s = g_string_sized_new (strlen (unescaped) + 10);
g_string_append_uri_escaped (s, unescaped, reserved_chars_allowed, allow_utf8);
return g_string_free (s, FALSE);
}

View File

@ -1,83 +0,0 @@
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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 License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#ifndef __G_URI_FUNCS_H__
#define __G_URI_FUNCS_H__
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
#error "Only <glib.h> can be included directly."
#endif
#include <glib/gtypes.h>
G_BEGIN_DECLS
/**
* G_URI_RESERVED_CHARS_GENERIC_DELIMITERS:
*
* Generic delimiters characters as defined in RFC 3986. Includes ":/?#[]@".
**/
#define G_URI_RESERVED_CHARS_GENERIC_DELIMITERS ":/?#[]@"
/**
* G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS:
*
* Subcomponent delimiter characters as defined in RFC 3986. Includes "!$&'()*+,;=".
**/
#define G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS "!$&'()*+,;="
/**
* G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT:
*
* Allowed characters in path elements. Includes "!$&'()*+,;=:@".
**/
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":@"
/**
* G_URI_RESERVED_CHARS_ALLOWED_IN_PATH:
*
* Allowed characters in a path. Includes "!$&'()*+,;=:@/".
**/
#define G_URI_RESERVED_CHARS_ALLOWED_IN_PATH G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/"
/**
* G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO:
*
* Allowed characters in userinfo as defined in RFC 3986. Includes "!$&'()*+,;=:".
**/
#define G_URI_RESERVED_CHARS_ALLOWED_IN_USERINFO G_URI_RESERVED_CHARS_SUBCOMPONENT_DELIMITERS ":"
GLIB_AVAILABLE_IN_ALL
char * g_uri_unescape_string (const char *escaped_string,
const char *illegal_characters);
GLIB_AVAILABLE_IN_ALL
char * g_uri_unescape_segment (const char *escaped_string,
const char *escaped_string_end,
const char *illegal_characters);
GLIB_AVAILABLE_IN_ALL
char * g_uri_parse_scheme (const char *uri);
GLIB_AVAILABLE_IN_ALL
char * g_uri_escape_string (const char *unescaped,
const char *reserved_chars_allowed,
gboolean allow_utf8);
G_END_DECLS
#endif /* __G_URI_FUNCS_H__ */

36
glib/guriprivate.h Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright © 2020 Red Hat, Inc.
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Marc-André Lureau <marcandre.lureau@redhat.com>
*/
#ifndef __G_URI_PRIVATE_H__
#define __G_URI_PRIVATE_H__
#include "gtypes.h"
G_BEGIN_DECLS
void
_uri_encoder (GString *out,
const guchar *start,
gsize length,
const gchar *reserved_chars_allowed,
gboolean allow_utf8);
G_END_DECLS
#endif /* __G_URI_PRIVATE_H__ */

View File

@ -194,7 +194,7 @@ glib_sub_headers = files(
'gtypes.h',
'guuid.h',
'gunicode.h',
'gurifuncs.h',
'guri.h',
'gutils.h',
'gvarianttype.h',
'gvariant.h',
@ -283,7 +283,8 @@ glib_sources = files(
'gunibreak.c',
'gunicollate.c',
'gunidecomp.c',
'gurifuncs.c',
'guri.c',
'guriprivate.h',
'gutils.c',
'gutilsprivate.h',
'guuid.c',

File diff suppressed because it is too large Load Diff

View File

@ -169,6 +169,7 @@ G_DEFINE_BOXED_TYPE (GMarkupParseContext, g_markup_parse_context, g_markup_parse
G_DEFINE_BOXED_TYPE (GThread, g_thread, g_thread_ref, g_thread_unref)
G_DEFINE_BOXED_TYPE (GChecksum, g_checksum, g_checksum_copy, g_checksum_free)
G_DEFINE_BOXED_TYPE (GUri, g_uri, g_uri_ref, g_uri_unref)
G_DEFINE_BOXED_TYPE (GOptionGroup, g_option_group, g_option_group_ref, g_option_group_unref)

View File

@ -297,6 +297,15 @@ typedef gsize GType;
*/
#define G_TYPE_OPTION_GROUP (g_option_group_get_type ())
/**
* G_TYPE_URI:
*
* The #GType for a boxed type holding a #GUri.
*
* Since: 2.66
*/
#define G_TYPE_URI (g_uri_get_type ())
GLIB_AVAILABLE_IN_ALL
GType g_date_get_type (void) G_GNUC_CONST;
GLIB_AVAILABLE_IN_ALL
@ -353,6 +362,8 @@ GLIB_AVAILABLE_IN_2_40
GType g_mapped_file_get_type (void) G_GNUC_CONST;
GLIB_AVAILABLE_IN_2_44
GType g_option_group_get_type (void) G_GNUC_CONST;
GLIB_AVAILABLE_IN_2_66
GType g_uri_get_type (void) G_GNUC_CONST;
GLIB_DEPRECATED_FOR('G_TYPE_VARIANT')
GType g_variant_get_gtype (void) G_GNUC_CONST;