]> git.sesse.net Git - vlc/blob - include/vlc_url.h
A bit of vlc/libvlc cleanup:
[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 #if !defined( __LIBVLC__ )
26   #error You are not libvlc or one of its plugins. You cannot include this file
27 #endif
28
29 #ifndef __VLC_URL_H
30 # define __VLC_URL_H
31
32 typedef struct
33 {
34     char *psz_protocol;
35     char *psz_username;
36     char *psz_password;
37     char *psz_host;
38     int  i_port;
39
40     char *psz_path;
41
42     char *psz_option;
43
44     char *psz_buffer; /* to be freed */
45 } vlc_url_t;
46
47 /*****************************************************************************
48  * vlc_UrlParse:
49  *****************************************************************************
50  * option : if != 0 then path is split at this char
51  *
52  * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
53  *****************************************************************************/
54 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
55                                  char option )
56 {
57     char *psz_dup;
58     char *psz_parse;
59     char *p;
60
61     url->psz_protocol = NULL;
62     url->psz_username = NULL;
63     url->psz_password = NULL;
64     url->psz_host     = NULL;
65     url->i_port       = 0;
66     url->psz_path     = NULL;
67     url->psz_option   = NULL;
68
69     if( psz_url == NULL )
70     {
71         url->psz_buffer = NULL;
72         return;
73     }
74     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
75
76     p  = strstr( psz_parse, ":/" );
77     if( p != NULL )
78     {
79         /* we have a protocol */
80
81         /* skip :// */
82         *p++ = '\0';
83         if( p[1] == '/' )
84             p += 2;
85         url->psz_protocol = psz_parse;
86         psz_parse = p;
87     }
88     p = strchr( psz_parse, '@' );
89     if( p != NULL )
90     {
91         /* We have a login */
92         url->psz_username = psz_parse;
93         *p++ = '\0';
94
95         psz_parse = strchr( psz_parse, ':' );
96         if( psz_parse != NULL )
97         {
98             /* We have a password */
99             *psz_parse++ = '\0';
100             url->psz_password = psz_parse;
101         }
102
103         psz_parse = p;
104     }
105
106     p = strchr( psz_parse, '/' );
107     if( !p || psz_parse < p )
108     {
109         char *p2;
110
111         /* We have a host[:port] */
112         url->psz_host = strdup( psz_parse );
113         if( p )
114         {
115             url->psz_host[p - psz_parse] = '\0';
116         }
117
118         if( *url->psz_host == '[' )
119         {
120             /* Ipv6 address */
121             p2 = strchr( url->psz_host, ']' );
122             if( p2 )
123             {
124                 p2 = strchr( p2, ':' );
125             }
126         }
127         else
128         {
129             p2 = strchr( url->psz_host, ':' );
130         }
131         if( p2 )
132         {
133             *p2++ = '\0';
134             url->i_port = atoi( p2 );
135         }
136     }
137     psz_parse = p;
138
139     /* Now parse psz_path and psz_option */
140     if( psz_parse )
141     {
142         url->psz_path = psz_parse;
143         if( option != '\0' )
144         {
145             p = strchr( url->psz_path, option );
146             if( p )
147             {
148                 *p++ = '\0';
149                 url->psz_option = p;
150             }
151         }
152     }
153 }
154
155 /*****************************************************************************
156  * vlc_UrlClean:
157  *****************************************************************************/
158 static inline void vlc_UrlClean( vlc_url_t *url )
159 {
160     if( url->psz_buffer ) free( url->psz_buffer );
161     if( url->psz_host )   free( url->psz_host );
162
163     url->psz_protocol = NULL;
164     url->psz_username = NULL;
165     url->psz_password = NULL;
166     url->psz_host     = NULL;
167     url->i_port       = 0;
168     url->psz_path     = NULL;
169     url->psz_option   = NULL;
170
171     url->psz_buffer   = NULL;
172 }
173
174 VLC_EXPORT( char *, unescape_URI_duplicate, ( const char *psz ) );
175 VLC_EXPORT( void, unescape_URI, ( char *psz ) );
176 VLC_EXPORT( char *, decode_URI_duplicate, ( const char *psz ) );
177 VLC_EXPORT( void, decode_URI, ( char *psz ) );
178 VLC_EXPORT( char *, encode_URI_component, ( const char *psz ) );
179
180 static inline char *vlc_UrlEncode( const char *psz_url )
181 {
182     /* FIXME: do not encode / : ? and & _when_ not needed */
183     return encode_URI_component( psz_url );
184 }
185
186 #include <ctype.h>
187
188 /** Check whether a given string is not a valid URL and must hence be
189  *  encoded */
190 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
191 {
192     const char *ptr;
193
194     for( ptr = psz_url; *ptr; ptr++ )
195     {
196         char c = *ptr;
197
198         if( c == '%' )
199         {
200             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
201                 return 1; /* not encoded */
202             ptr += 2;
203         }
204         else
205         if(  ( (unsigned char)( c - 'a' ) < 26 )
206           || ( (unsigned char)( c - 'A' ) < 26 )
207           || ( (unsigned char)( c - '0' ) < 10 )
208           || ( strchr( "-_.", c ) != NULL ) )
209             return 1;
210     }
211     return 0; /* looks fine - but maybe it is not encoded */
212 }
213
214 /* Base64 encoding */
215 static inline char *vlc_b64_encode( const char *src )
216 {
217     static const char b64[] =
218            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
219     size_t len = strlen( src );
220     const uint8_t *in = (const uint8_t *)src;
221
222     char *ret;
223     char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
224     if( dst == NULL )
225         return NULL;
226
227     ret = dst;
228
229     while( len > 0 )
230     {
231         /* pops (up to) 3 bytes of input, push 4 bytes */
232         uint32_t v = *in++ << 24; // 1/3
233         *dst++ = b64[v >> 26]; // 1/4
234         v = v << 6;
235
236         if( len >= 2 )
237             v |= *in++ << 22; // 2/3
238         *dst++ = b64[v >> 26]; // 2/4
239         v = v << 6;
240
241         if( len >= 3 )
242             v |= *in++ << 20; // 3/3
243         *dst++ = ( len >= 2 ) ? b64[v >> 26] : '='; // 3/4
244         v = v << 6;
245
246         *dst++ = ( len >= 3 ) ? b64[v >> 26] : '='; // 4/4
247
248         len--;
249         if( len > 0 )
250         {
251             len--;
252             if( len > 0 )
253                 len--;
254         }
255     }
256
257     *dst = '\0';
258
259     return ret;
260 }
261 #endif