1 /*****************************************************************************
2 * vlc_url.h: URL related macros
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
40 char *psz_buffer; /* to be freed */
43 /*****************************************************************************
45 *****************************************************************************
46 * option : if != 0 then path is split at this char
48 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
49 *****************************************************************************/
50 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
57 url->psz_protocol = NULL;
58 url->psz_username = NULL;
59 url->psz_password = NULL;
63 url->psz_option = NULL;
67 url->psz_buffer = NULL;
70 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
72 p = strstr( psz_parse, ":/" );
75 /* we have a protocol */
81 url->psz_protocol = psz_parse;
84 p = strchr( psz_parse, '@' );
88 url->psz_username = psz_parse;
91 psz_parse = strchr( psz_parse, ':' );
92 if( psz_parse != NULL )
94 /* We have a password */
96 url->psz_password = psz_parse;
102 p = strchr( psz_parse, '/' );
103 if( !p || psz_parse < p )
107 /* We have a host[:port] */
108 url->psz_host = strdup( psz_parse );
111 url->psz_host[p - psz_parse] = '\0';
114 if( *url->psz_host == '[' )
117 p2 = strchr( url->psz_host, ']' );
120 p2 = strchr( p2, ':' );
125 p2 = strchr( url->psz_host, ':' );
130 url->i_port = atoi( p2 );
135 /* Now parse psz_path and psz_option */
138 url->psz_path = psz_parse;
141 p = strchr( url->psz_path, option );
151 /*****************************************************************************
153 *****************************************************************************
155 *****************************************************************************/
156 static inline void vlc_UrlClean( vlc_url_t *url )
158 if( url->psz_buffer ) free( url->psz_buffer );
159 if( url->psz_host ) free( url->psz_host );
161 url->psz_protocol = NULL;
162 url->psz_username = NULL;
163 url->psz_password = NULL;
164 url->psz_host = NULL;
166 url->psz_path = NULL;
167 url->psz_option = NULL;
169 url->psz_buffer = NULL;
172 static inline int isurlsafe( int c )
174 return ( (unsigned char)( c - 'a' ) < 26 )
175 || ( (unsigned char)( c - 'A' ) < 26 )
176 || ( (unsigned char)( c - '0' ) < 10 )
177 /* Hmm, we should not encode character that are allowed in URLs
178 * (even if they are not URL-safe), nor URL-safe characters.
179 * We still encode some of them because of Microsoft's crap browser.
181 || ( strchr( "-_.", c ) != NULL );
184 /*****************************************************************************
186 *****************************************************************************
187 * perform URL encoding
188 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
189 *****************************************************************************/
190 static inline char *vlc_UrlEncode( const char *psz_url )
193 const unsigned char *in;
195 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
196 if( psz_enc == NULL )
200 for( in = (const unsigned char *)psz_url; *in; in++ )
202 unsigned char c = *in;
209 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
211 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
217 return (char *)realloc( psz_enc, out - psz_enc );
220 /*****************************************************************************
221 * vlc_UrlIsNotEncoded:
222 *****************************************************************************
223 * check if given string is not a valid URL and must hence be encoded
224 *****************************************************************************/
227 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
231 for( ptr = psz_url; *ptr; ptr++ )
237 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
238 return 1; /* not encoded */
242 if( !isurlsafe( c ) )
245 return 0; /* looks fine - but maybe it is not encoded */
248 /*****************************************************************************
250 *****************************************************************************
252 *****************************************************************************/
253 static inline char *vlc_b64_encode( char *src )
255 static const char b64[] =
256 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
257 size_t len = strlen( src );
260 char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
268 /* pops (up to) 3 bytes of input */
269 uint32_t v = *src++ << 24;
278 /* pushes (up to) 4 bytes of output */
281 *dst++ = b64[v >> 26];