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