1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gurifuncs.h"
32 * @short_description: URI Functions
34 * Functions for manipulating Universal Resource Identifiers (URIs) as
35 * defined by <ulink url="http://www.ietf.org/rfc/rfc3986.txt">
36 * RFC 3986</ulink>. It is highly recommended that you have read and
37 * understand RFC 3986 for understanding this API.
41 unescape_character (const char *scanner)
46 first_digit = g_ascii_xdigit_value (*scanner++);
50 second_digit = g_ascii_xdigit_value (*scanner++);
54 return (first_digit << 4) | second_digit;
58 * g_uri_unescape_segment:
59 * @escaped_string: a string.
60 * @escaped_string_end: a string.
61 * @illegal_characters: an optional string of illegal characters not to be allowed.
63 * Unescapes a segment of an escaped string.
65 * If any of the characters in @illegal_characters or the character zero appears
66 * as an escaped character in @escaped_string then that is an error and %NULL
67 * will be returned. This is useful it you want to avoid for instance having a
68 * slash being expanded in an escaped path element, which might confuse pathname
71 * Returns: an unescaped version of @escaped_string or %NULL on error.
72 * The returned string should be freed when no longer needed.
77 g_uri_unescape_segment (const char *escaped_string,
78 const char *escaped_string_end,
79 const char *illegal_characters)
85 if (escaped_string == NULL)
88 if (escaped_string_end == NULL)
89 escaped_string_end = escaped_string + strlen (escaped_string);
91 result = g_malloc (escaped_string_end - escaped_string + 1);
94 for (in = escaped_string; in < escaped_string_end; in++)
102 if (escaped_string_end - in < 2)
104 /* Invalid escaped char (to short) */
109 character = unescape_character (in);
111 /* Check for an illegal character. We consider '\0' illegal here. */
112 if (character <= 0 ||
113 (illegal_characters != NULL &&
114 strchr (illegal_characters, (char)character) != NULL))
120 in++; /* The other char will be eaten in the loop header */
122 *out++ = (char)character;
131 * g_uri_unescape_string:
132 * @escaped_string: an escaped string to be unescaped.
133 * @illegal_characters: an optional string of illegal characters not to be allowed.
135 * Unescapes a whole escaped string.
137 * If any of the characters in @illegal_characters or the character zero appears
138 * as an escaped character in @escaped_string then that is an error and %NULL
139 * will be returned. This is useful it you want to avoid for instance having a
140 * slash being expanded in an escaped path element, which might confuse pathname
143 * Returns: an unescaped version of @escaped_string. The returned string
144 * should be freed when no longer needed.
149 g_uri_unescape_string (const char *escaped_string,
150 const char *illegal_characters)
152 return g_uri_unescape_segment (escaped_string, NULL, illegal_characters);
156 * g_uri_parse_scheme:
159 * Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
161 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
163 * Common schemes include "file", "http", "svn+ssh", etc.
165 * Returns: The "Scheme" component of the URI, or %NULL on error.
166 * The returned string should be freed when no longer needed.
171 g_uri_parse_scheme (const char *uri)
176 g_return_val_if_fail (uri != NULL, NULL);
178 /* From RFC 3986 Decodes:
179 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
185 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
188 if (!g_ascii_isalpha (*p))
198 if (!(g_ascii_isalnum(c) ||
205 return g_strndup (uri, p - uri - 1);
209 * g_uri_escape_string:
210 * @unescaped: the unescaped input string.
211 * @reserved_chars_allowed: a string of reserved characters that are
212 * allowed to be used.
213 * @allow_utf8: %TRUE if the result can include UTF-8 characters.
215 * Escapes a string for use in a URI.
217 * Normally all characters that are not "unreserved" (i.e. ASCII alphanumerical
218 * characters plus dash, dot, underscore and tilde) are escaped.
219 * But if you specify characters in @reserved_chars_allowed they are not
220 * escaped. This is useful for the "reserved" characters in the URI
221 * specification, since those are allowed unescaped in some portions of
224 * Returns: an escaped version of @unescaped. The returned string should be
225 * freed when no longer needed.
230 g_uri_escape_string (const char *unescaped,
231 const char *reserved_chars_allowed,
236 g_return_val_if_fail (unescaped != NULL, NULL);
238 s = g_string_sized_new (strlen (unescaped) + 10);
240 g_string_append_uri_escaped (s, unescaped, reserved_chars_allowed, allow_utf8);
242 return g_string_free (s, FALSE);
245 #define __G_URI_FUNCS_C__
246 #include "galiasdef.c"