33d9eca440fbadbf8a20d00f988e69c56a72747e
[platform/upstream/glib.git] / glib / gurifuncs.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "gurifuncs.h"
24
25 #include <glib/gstrfuncs.h>
26 #include <glib/gmessages.h>
27 #include <glib/gstring.h>
28 #include <glib/gmem.h>
29
30 #include <string.h>
31
32 #include "config.h"
33
34 /**
35  * SECTION:gurifuncs
36  * @title: URI Functions
37  * @short_description: manipulating URIs
38  *
39  * Functions for manipulating Universal Resource Identifiers (URIs) as
40  * defined by <ulink url="http://www.ietf.org/rfc/rfc3986.txt">
41  * RFC 3986</ulink>. It is highly recommended that you have read and
42  * understand RFC 3986 for understanding this API.
43  */
44
45 static int
46 unescape_character (const char *scanner)
47 {
48   int first_digit;
49   int second_digit;
50   
51   first_digit = g_ascii_xdigit_value (*scanner++);
52   if (first_digit < 0)
53     return -1;
54
55   second_digit = g_ascii_xdigit_value (*scanner++);
56   if (second_digit < 0)
57     return -1;
58
59   return (first_digit << 4) | second_digit;
60 }
61
62 /**
63  * g_uri_unescape_segment:
64  * @escaped_string: (allow-none): A string, may be %NULL
65  * @escaped_string_end: (allow-none): Pointer to end of @escaped_string, may be %NULL
66  * @illegal_characters: (allow-none): An optional string of illegal characters not to be allowed, may be %NULL
67  * 
68  * Unescapes a segment of an escaped string.
69  *
70  * If any of the characters in @illegal_characters or the character zero appears
71  * as an escaped character in @escaped_string then that is an error and %NULL
72  * will be returned. This is useful it you want to avoid for instance having a
73  * slash being expanded in an escaped path element, which might confuse pathname
74  * handling.
75  *
76  * Returns: an unescaped version of @escaped_string or %NULL on error.
77  * The returned string should be freed when no longer needed.  As a
78  * special case if %NULL is given for @escaped_string, this function
79  * will return %NULL.
80  *
81  * Since: 2.16
82  **/
83 char *
84 g_uri_unescape_segment (const char *escaped_string,
85                         const char *escaped_string_end,
86                         const char *illegal_characters)
87 {
88   const char *in;
89   char *out, *result;
90   gint character;
91   
92   if (escaped_string == NULL)
93     return NULL;
94   
95   if (escaped_string_end == NULL)
96     escaped_string_end = escaped_string + strlen (escaped_string);
97   
98   result = g_malloc (escaped_string_end - escaped_string + 1);
99   
100   out = result;
101   for (in = escaped_string; in < escaped_string_end; in++)
102     {
103       character = *in;
104       
105       if (*in == '%')
106         {
107           in++;
108           
109           if (escaped_string_end - in < 2)
110             {
111               /* Invalid escaped char (to short) */
112               g_free (result);
113               return NULL;
114             }
115           
116           character = unescape_character (in);
117           
118           /* Check for an illegal character. We consider '\0' illegal here. */
119           if (character <= 0 ||
120               (illegal_characters != NULL &&
121                strchr (illegal_characters, (char)character) != NULL))
122             {
123               g_free (result);
124               return NULL;
125             }
126           
127           in++; /* The other char will be eaten in the loop header */
128         }
129       *out++ = (char)character;
130     }
131   
132   *out = '\0';
133   
134   return result;
135 }
136
137 /**
138  * g_uri_unescape_string:
139  * @escaped_string: an escaped string to be unescaped.
140  * @illegal_characters: (allow-none): a string of illegal characters not to be
141  *      allowed, or %NULL.
142  * 
143  * Unescapes a whole escaped string.
144  * 
145  * If any of the characters in @illegal_characters or the character zero appears
146  * as an escaped character in @escaped_string then that is an error and %NULL
147  * will be returned. This is useful it you want to avoid for instance having a
148  * slash being expanded in an escaped path element, which might confuse pathname
149  * handling.
150  *
151  * Returns: an unescaped version of @escaped_string. The returned string 
152  * should be freed when no longer needed.
153  *
154  * Since: 2.16
155  **/
156 char *
157 g_uri_unescape_string (const char *escaped_string,
158                        const char *illegal_characters)
159 {
160   return g_uri_unescape_segment (escaped_string, NULL, illegal_characters);
161 }
162
163 /**
164  * g_uri_parse_scheme:
165  * @uri: a valid URI.
166  * 
167  * Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
168  * |[
169  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 
170  * ]|
171  * Common schemes include "file", "http", "svn+ssh", etc.
172  * 
173  * Returns: The "Scheme" component of the URI, or %NULL on error. 
174  * The returned string should be freed when no longer needed.
175  *
176  * Since: 2.16
177  **/
178 char *
179 g_uri_parse_scheme (const char  *uri)
180 {
181   const char *p;
182   char c;
183
184   g_return_val_if_fail (uri != NULL, NULL);
185
186   /* From RFC 3986 Decodes:
187    * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
188    */ 
189
190   p = uri;
191   
192   /* Decode scheme:
193      scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
194   */
195
196   if (!g_ascii_isalpha (*p))
197     return NULL;
198   
199   while (1)
200     {
201       c = *p++;
202       
203       if (c == ':')
204         break;
205       
206       if (!(g_ascii_isalnum(c) ||
207             c == '+' ||
208             c == '-' ||
209             c == '.'))
210         return NULL;
211     }
212   
213   return g_strndup (uri, p - uri - 1);
214 }
215
216 /**
217  * g_uri_escape_string:
218  * @unescaped: the unescaped input string.
219  * @reserved_chars_allowed: (allow-none): a string of reserved characters that
220  *      are allowed to be used, or %NULL.
221  * @allow_utf8: %TRUE if the result can include UTF-8 characters.
222  * 
223  * Escapes a string for use in a URI.
224  *
225  * Normally all characters that are not "unreserved" (i.e. ASCII alphanumerical
226  * characters plus dash, dot, underscore and tilde) are escaped.
227  * But if you specify characters in @reserved_chars_allowed they are not
228  * escaped. This is useful for the "reserved" characters in the URI
229  * specification, since those are allowed unescaped in some portions of
230  * a URI. 
231  * 
232  * Returns: an escaped version of @unescaped. The returned string should be 
233  * freed when no longer needed.
234  *
235  * Since: 2.16
236  **/
237 char *
238 g_uri_escape_string (const char *unescaped,
239                      const char  *reserved_chars_allowed,
240                      gboolean     allow_utf8)
241 {
242   GString *s;
243
244   g_return_val_if_fail (unescaped != NULL, NULL);
245
246   s = g_string_sized_new (strlen (unescaped) + 10);
247   
248   g_string_append_uri_escaped (s, unescaped, reserved_chars_allowed, allow_utf8);
249   
250   return g_string_free (s, FALSE);
251 }