]> git.sesse.net Git - vlc/blob - include/vlc_url.h
* include/vlc_url.h: Fixed vlc_UrlEncode to return correct values and avoid
[vlc] / include / vlc_url.h
1 /*****************************************************************************
2  * vlc_url.h: URL related macros
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          RĂ©mi Denis-Courmont <rem # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifndef __VLC_URL_H
26 # define __VLC_URL_H
27
28 typedef struct
29 {
30     char *psz_protocol;
31     char *psz_username;
32     char *psz_password;
33     char *psz_host;
34     int  i_port;
35
36     char *psz_path;
37
38     char *psz_option;
39
40     char *psz_buffer; /* to be freed */
41 } vlc_url_t;
42
43 /*****************************************************************************
44  * vlc_UrlParse:
45  *****************************************************************************
46  * option : if != 0 then path is split at this char
47  *
48  * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
49  *****************************************************************************/
50 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
51                                  char option )
52 {
53     char *psz_dup;
54     char *psz_parse;
55     char *p;
56
57     url->psz_protocol = NULL;
58     url->psz_username = NULL;
59     url->psz_password = NULL;
60     url->psz_host     = NULL;
61     url->i_port       = 0;
62     url->psz_path     = NULL;
63     url->psz_option   = NULL;
64
65     if( psz_url == NULL )
66     {
67         url->psz_buffer = NULL;
68         return;
69     }
70     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
71
72     p  = strstr( psz_parse, ":/" );
73     if( p != NULL )
74     {
75         /* we have a protocol */
76
77         /* skip :// */
78         *p++ = '\0';
79         if( p[1] == '/' )
80             p += 2;
81         url->psz_protocol = psz_parse;
82         psz_parse = p;
83     }
84     p = strchr( psz_parse, '@' );
85     if( p != NULL )
86     {
87         /* We have a login */
88         url->psz_username = psz_parse;
89         *p++ = '\0';
90
91         psz_parse = strchr( psz_parse, ':' );
92         if( psz_parse != NULL )
93         {
94             /* We have a password */
95             *psz_parse++ = '\0';
96             url->psz_password = psz_parse;
97         }
98
99         psz_parse = p;
100     }
101
102     p = strchr( psz_parse, '/' );
103     if( !p || psz_parse < p )
104     {
105         char *p2;
106
107         /* We have a host[:port] */
108         url->psz_host = strdup( psz_parse );
109         if( p )
110         {
111             url->psz_host[p - psz_parse] = '\0';
112         }
113
114         if( *url->psz_host == '[' )
115         {
116             /* Ipv6 address */
117             p2 = strchr( url->psz_host, ']' );
118             if( p2 )
119             {
120                 p2 = strchr( p2, ':' );
121             }
122         }
123         else
124         {
125             p2 = strchr( url->psz_host, ':' );
126         }
127         if( p2 )
128         {
129             *p2++ = '\0';
130             url->i_port = atoi( p2 );
131         }
132     }
133     psz_parse = p;
134
135     /* Now parse psz_path and psz_option */
136     if( psz_parse )
137     {
138         url->psz_path = psz_parse;
139         if( option != '\0' )
140         {
141             p = strchr( url->psz_path, option );
142             if( p )
143             {
144                 *p++ = '\0';
145                 url->psz_option = p;
146             }
147         }
148     }
149 }
150
151 /*****************************************************************************
152  * vlc_UrlClean:
153  *****************************************************************************
154  *
155  *****************************************************************************/
156 static inline void vlc_UrlClean( vlc_url_t *url )
157 {
158     if( url->psz_buffer ) free( url->psz_buffer );
159     if( url->psz_host )   free( url->psz_host );
160
161     url->psz_protocol = NULL;
162     url->psz_username = NULL;
163     url->psz_password = NULL;
164     url->psz_host     = NULL;
165     url->i_port       = 0;
166     url->psz_path     = NULL;
167     url->psz_option   = NULL;
168
169     url->psz_buffer   = NULL;
170 }
171
172 VLC_EXPORT( char *, unescape_URI_duplicate, ( const char *psz ) );
173 VLC_EXPORT( void, unescape_URI, ( char *psz ) );
174
175 static inline int isurlsafe( int c )
176 {
177     return ( (unsigned char)( c - 'a' ) < 26 )
178         || ( (unsigned char)( c - 'A' ) < 26 )
179         || ( (unsigned char)( c - '0' ) < 10 )
180         /* Hmm, we should not encode character that are allowed in URLs
181          * (even if they are not URL-safe), nor URL-safe characters.
182          * We still encode some of them because of Microsoft's crap browser.
183          */
184         || ( strchr( "-_.", c ) != NULL );
185 }
186
187 static inline char url_hexchar( int c )
188 {
189     return ( c < 10 ) ? c + '0' : c + 'A' - 10;
190 }
191
192 /*****************************************************************************
193  * vlc_UrlEncode:
194  *****************************************************************************
195  * perform URL encoding
196  * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
197  *****************************************************************************/
198 static inline char *vlc_UrlEncode( const char *psz_url )
199 {
200     char psz_enc[3 * strlen( psz_url ) + 1], *out = psz_enc;
201     const uint8_t *in;
202
203     for( in = (const uint8_t *)psz_url; *in; in++ )
204     {
205         uint8_t c = *in;
206
207         if( isurlsafe( c ) )
208         {
209             *out++ = (char)c;
210         }
211         else
212         {
213             uint16_t cp;
214
215             *out++ = '%';
216             /* UTF-8 to UCS-2 conversion */
217             if( ( c & 0x80 ) == 0 )
218             {
219                 cp = c;
220             }
221             else if( ( c & 0xe0 ) == 0xc0 )
222             {
223                 cp = (((uint16_t)c & 0x1f) << 6) | (in[1] & 0x3f);
224                 in++;
225             }
226             else if( ( c & 0xf0 ) == 0xe0 )
227             {
228                 cp = (((uint16_t)c & 0xf) << 12) | (((uint16_t)(in[1]) & 0x3f) << 6) | (in[2] & 0x3f);
229                 in += 2;
230             }
231             else
232             {
233                 /* cannot URL-encode code points outside the BMP */
234                 /* better a wrong conversion than a crash */
235                 cp = '?';
236             }
237
238             if( cp < 0xff )
239             {
240                 /* Encode ISO-8859-1 characters */
241                 *out++ = url_hexchar( cp >> 4 );
242                 *out++ = url_hexchar( cp & 0xf );
243             }
244             else
245             {
246                 /* Encode non-Latin-1 characters */
247                 *out++ = 'u';
248                 *out++ = url_hexchar( cp >> 12       );
249                 *out++ = url_hexchar((cp >>  8) & 0xf );
250                 *out++ = url_hexchar((cp >>  4) & 0xf );
251                 *out++ = url_hexchar( cp        & 0xf );
252             }
253         }
254     }
255     *out++ = '\0';
256
257     return strdup( psz_enc );
258 }
259
260 /*****************************************************************************
261  * vlc_UrlIsNotEncoded:
262  *****************************************************************************
263  * check if given string is not a valid URL and must hence be encoded
264  *****************************************************************************/
265 #include <ctype.h>
266
267 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
268 {
269     const char *ptr;
270
271     for( ptr = psz_url; *ptr; ptr++ )
272     {
273         char c = *ptr;
274
275         if( c == '%' )
276         {
277             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
278                 return 1; /* not encoded */
279             ptr += 2;
280         }
281         else
282         if( !isurlsafe( c ) )
283             return 1;
284     }
285     return 0; /* looks fine - but maybe it is not encoded */
286 }
287
288 /*****************************************************************************
289  * vlc_b64_encode:
290  *****************************************************************************
291  *
292  *****************************************************************************/
293 static inline char *vlc_b64_encode( char *src )
294 {
295     static const char b64[] =
296            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
297     size_t len = strlen( src );
298
299     char *ret;
300     char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
301     if( dst == NULL )
302         return NULL;
303
304     ret = dst;
305
306     while( len > 0 )
307     {
308         /* pops (up to) 3 bytes of input */
309         uint32_t v = *src++ << 24;
310
311         if( len >= 2 )
312         {
313             v |= *src++ << 16;
314             if( len >= 3 )
315                 v |= *src++ << 8;
316         }
317
318         /* pushes (up to) 4 bytes of output */
319         while( v )
320         {
321             *dst++ = b64[v >> 26];
322             v = v << 6;
323         }
324
325         switch( len )
326         {
327             case 1:
328                 *dst++ = '=';
329                 *dst++ = '=';
330                 len--;
331                 break;
332
333             case 2:
334                 *dst++ = '=';
335                 len -= 2;
336                 break;
337
338             default:
339                 len -= 3;
340         }
341     }
342
343     *dst = '\0';
344
345     return ret;
346 }
347 #endif