[HQ](Debian) Package version up
[external/glib2.0.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 #include "galias.h"
36
37 /**
38  * SECTION:gurifuncs
39  * @short_description: URI Functions
40  * 
41  * Functions for manipulating Universal Resource Identifiers (URIs) as 
42  * defined by <ulink url="http://www.ietf.org/rfc/rfc3986.txt">
43  * RFC 3986</ulink>. It is highly recommended that you have read and
44  * understand RFC 3986 for understanding this API.
45  */
46
47 static int
48 unescape_character (const char *scanner)
49 {
50   int first_digit;
51   int second_digit;
52   
53   first_digit = g_ascii_xdigit_value (*scanner++);
54   if (first_digit < 0)
55     return -1;
56
57   second_digit = g_ascii_xdigit_value (*scanner++);
58   if (second_digit < 0)
59     return -1;
60
61   return (first_digit << 4) | second_digit;
62 }
63
64 /**
65  * g_uri_unescape_segment:
66  * @escaped_string: a string.
67  * @escaped_string_end: a string.
68  * @illegal_characters: an optional string of illegal characters not to be allowed.
69  * 
70  * Unescapes a segment of an escaped string.
71  *
72  * If any of the characters in @illegal_characters or the character zero appears
73  * as an escaped character in @escaped_string then that is an error and %NULL
74  * will be returned. This is useful it you want to avoid for instance having a
75  * slash being expanded in an escaped path element, which might confuse pathname
76  * handling.
77  *
78  * Returns: an unescaped version of @escaped_string or %NULL on error.
79  * The returned string should be freed when no longer needed.
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: an optional string of illegal characters not to be allowed.
141  * 
142  * Unescapes a whole escaped string.
143  * 
144  * If any of the characters in @illegal_characters or the character zero appears
145  * as an escaped character in @escaped_string then that is an error and %NULL
146  * will be returned. This is useful it you want to avoid for instance having a
147  * slash being expanded in an escaped path element, which might confuse pathname
148  * handling.
149  *
150  * Returns: an unescaped version of @escaped_string. The returned string 
151  * should be freed when no longer needed.
152  *
153  * Since: 2.16
154  **/
155 char *
156 g_uri_unescape_string (const char *escaped_string,
157                        const char *illegal_characters)
158 {
159   return g_uri_unescape_segment (escaped_string, NULL, illegal_characters);
160 }
161
162 /**
163  * g_uri_parse_scheme:
164  * @uri: a valid URI.
165  * 
166  * Gets the scheme portion of a URI string. RFC 3986 decodes the scheme as:
167  * <programlisting>
168  * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] 
169  * </programlisting>
170  * Common schemes include "file", "http", "svn+ssh", etc.
171  * 
172  * Returns: The "Scheme" component of the URI, or %NULL on error. 
173  * The returned string should be freed when no longer needed.
174  *
175  * Since: 2.16
176  **/
177 char *
178 g_uri_parse_scheme (const char  *uri)
179 {
180   const char *p;
181   char c;
182
183   g_return_val_if_fail (uri != NULL, NULL);
184
185   /* From RFC 3986 Decodes:
186    * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
187    */ 
188
189   p = uri;
190   
191   /* Decode scheme:
192      scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
193   */
194
195   if (!g_ascii_isalpha (*p))
196     return NULL;
197   
198   while (1)
199     {
200       c = *p++;
201       
202       if (c == ':')
203         break;
204       
205       if (!(g_ascii_isalnum(c) ||
206             c == '+' ||
207             c == '-' ||
208             c == '.'))
209         return NULL;
210     }
211   
212   return g_strndup (uri, p - uri - 1);
213 }
214
215 /**
216  * g_uri_escape_string:
217  * @unescaped: the unescaped input string.
218  * @reserved_chars_allowed: a string of reserved characters that are
219  *      allowed to be used.
220  * @allow_utf8: %TRUE if the result can include UTF-8 characters.
221  * 
222  * Escapes a string for use in a URI.
223  *
224  * Normally all characters that are not "unreserved" (i.e. ASCII alphanumerical
225  * characters plus dash, dot, underscore and tilde) are escaped.
226  * But if you specify characters in @reserved_chars_allowed they are not
227  * escaped. This is useful for the "reserved" characters in the URI
228  * specification, since those are allowed unescaped in some portions of
229  * a URI. 
230  * 
231  * Returns: an escaped version of @unescaped. The returned string should be 
232  * freed when no longer needed.
233  *
234  * Since: 2.16
235  **/
236 char *
237 g_uri_escape_string (const char *unescaped,
238                      const char  *reserved_chars_allowed,
239                      gboolean     allow_utf8)
240 {
241   GString *s;
242
243   g_return_val_if_fail (unescaped != NULL, NULL);
244
245   s = g_string_sized_new (strlen (unescaped) + 10);
246   
247   g_string_append_uri_escaped (s, unescaped, reserved_chars_allowed, allow_utf8);
248   
249   return g_string_free (s, FALSE);
250 }
251
252 #define __G_URI_FUNCS_C__
253 #include "galiasdef.c"