]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
* ALL: New p_vlc->psz_userdir. This is different from psz_homedir in
[vlc] / modules / control / http / util.c
1 /*****************************************************************************
2  * util.c : Utility functions for HTTP interface
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #include "http.h"
27
28 /****************************************************************************
29  * File and directory functions
30  ****************************************************************************/
31
32 /* ToUrl: create a good name for an url from filename */
33 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
34 {
35     char *url, *p;
36
37     url = p = malloc( strlen( name ) + 1 );
38
39     *pb_index = VLC_FALSE;
40     if( !url || !p )
41     {
42         return NULL;
43     }
44
45 #ifdef WIN32
46     while( *name == '\\' || *name == '/' )
47 #else
48     while( *name == '/' )
49 #endif
50     {
51         name++;
52     }
53
54     *p++ = '/';
55     strcpy( p, name );
56
57 #ifdef WIN32
58     /* convert '\\' into '/' */
59     name = p;
60     while( *name )
61     {
62         if( *name == '\\' )
63             *name = '/';
64         name++;
65     }
66 #endif
67
68     /* index.* -> / */
69     if( ( p = strrchr( url, '/' ) ) != NULL )
70     {
71         if( !strncmp( p, "/index.", 7 ) )
72         {
73             p[1] = '\0';
74             *pb_index = VLC_TRUE;
75         }
76     }
77     return url;
78 }
79
80 /* Load a file */
81 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
82 {
83     int i_read;
84
85     /* just load the file */
86     *pi_data = 0;
87     *pp_data = malloc( 1025 );  /* +1 for \0 */
88     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
89     {
90         *pi_data += 1024;
91         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
92     }
93     if( i_read > 0 )
94     {
95         *pi_data += i_read;
96     }
97     (*pp_data)[*pi_data] = '\0';
98
99     return VLC_SUCCESS;
100 }
101
102 /* Parse a directory and recursively add files */
103 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
104                         char *psz_dir )
105 {
106     intf_sys_t     *p_sys = p_intf->p_sys;
107     char           dir[MAX_DIR_SIZE];
108 #ifdef HAVE_SYS_STAT_H
109     struct stat   stat_info;
110 #endif
111     DIR           *p_dir;
112     struct dirent *p_dir_content;
113     vlc_acl_t     *p_acl;
114     FILE          *file;
115
116     char          *user = NULL;
117     char          *password = NULL;
118
119     int           i_dirlen;
120
121     char sep;
122
123 #if defined( WIN32 )
124     sep = '\\';
125 #else
126     sep = '/';
127 #endif
128
129 #ifdef HAVE_SYS_STAT_H
130     if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
131     {
132         return VLC_EGENERIC;
133     }
134 #endif
135
136     if( ( p_dir = opendir( psz_dir ) ) == NULL )
137     {
138         msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
139         return VLC_EGENERIC;
140     }
141
142     i_dirlen = strlen( psz_dir );
143     if( i_dirlen + 10 > MAX_DIR_SIZE )
144     {
145         msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
146         return 0;
147     }
148
149     msg_Dbg( p_intf, "dir=%s", psz_dir );
150
151     sprintf( dir, "%s%c.access", psz_dir, sep );
152     if( ( file = fopen( dir, "r" ) ) != NULL )
153     {
154         char line[1024];
155         int  i_size;
156
157         msg_Dbg( p_intf, "find .access in dir=%s", psz_dir );
158
159         i_size = fread( line, 1, 1023, file );
160         if( i_size > 0 )
161         {
162             char *p;
163             while( i_size > 0 && ( line[i_size-1] == '\n' ||
164                    line[i_size-1] == '\r' ) )
165             {
166                 i_size--;
167             }
168
169             line[i_size] = '\0';
170
171             p = strchr( line, ':' );
172             if( p )
173             {
174                 *p++ = '\0';
175                 user = strdup( line );
176                 password = strdup( p );
177             }
178         }
179         msg_Dbg( p_intf, "using user=%s password=%s (read=%d)",
180                  user, password, i_size );
181
182         fclose( file );
183     }
184
185     sprintf( dir, "%s%c.hosts", psz_dir, sep );
186     p_acl = ACL_Create( p_intf, VLC_FALSE );
187     if( ACL_LoadFile( p_acl, dir ) )
188     {
189         ACL_Destroy( p_acl );
190         p_acl = NULL;
191     }
192
193     for( ;; )
194     {
195         /* parse psz_src dir */
196         if( ( p_dir_content = readdir( p_dir ) ) == NULL )
197         {
198             break;
199         }
200
201         if( ( p_dir_content->d_name[0] == '.' )
202          || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
203             continue;
204
205         sprintf( dir, "%s%c%s", psz_dir, sep, p_dir_content->d_name );
206         if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
207         {
208             httpd_file_sys_t *f = NULL;
209             httpd_handler_sys_t *h = NULL;
210             vlc_bool_t b_index;
211             char *psz_tmp, *psz_file, *psz_name, *psz_ext;
212
213             psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
214             psz_file = E_(FromUTF8)( p_intf, psz_tmp );
215             free( psz_tmp );
216             psz_tmp = vlc_fix_readdir_charset( p_intf,
217                                                &dir[strlen( psz_root )] );
218             psz_name = E_(FileToUrl)( psz_tmp, &b_index );
219             free( psz_tmp );
220             psz_ext = strrchr( psz_file, '.' );
221             if( psz_ext != NULL )
222             {
223                 int i;
224                 psz_ext++;
225                 for( i = 0; i < p_sys->i_handlers; i++ )
226                     if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
227                         break;
228                 if( i < p_sys->i_handlers )
229                 {
230                     f = malloc( sizeof( httpd_handler_sys_t ) );
231                     h = (httpd_handler_sys_t *)f;
232                     f->b_handler = VLC_TRUE;
233                     h->p_association = p_sys->pp_handlers[i];
234                 }
235             }
236             if( f == NULL )
237             {
238                 f = malloc( sizeof( httpd_file_sys_t ) );
239                 f->b_handler = VLC_FALSE;
240             }
241
242             f->p_intf  = p_intf;
243             f->p_file = NULL;
244             f->p_redir = NULL;
245             f->p_redir2 = NULL;
246             f->file = psz_file;
247             f->name = psz_name;
248             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
249
250             if( !f->name )
251             {
252                 msg_Err( p_intf , "unable to parse directory" );
253                 closedir( p_dir );
254                 free( f );
255                 return( VLC_ENOMEM );
256             }
257             msg_Dbg( p_intf, "file=%s (url=%s)",
258                      f->file, f->name );
259
260             if( !f->b_handler )
261             {
262                 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
263                                            f->name,
264                                            f->b_html ? p_sys->psz_html_type :
265                                             NULL,
266                                            user, password, p_acl,
267                                            E_(HttpCallback), f );
268                 if( f->p_file != NULL )
269                 {
270                     TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
271                 }
272             }
273             else
274             {
275                 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
276                                                  f->name,
277                                                  user, password, p_acl,
278                                                  E_(HandlerCallback), h );
279                 if( h->p_handler != NULL )
280                 {
281                     TAB_APPEND( p_sys->i_files, p_sys->pp_files,
282                                 (httpd_file_sys_t *)h );
283                 }
284             }
285
286             /* for url that ends by / add
287              *  - a redirect from rep to rep/
288              *  - in case of index.* rep/index.html to rep/ */
289             if( f && f->name[strlen(f->name) - 1] == '/' )
290             {
291                 char *psz_redir = strdup( f->name );
292                 char *p;
293                 psz_redir[strlen( psz_redir ) - 1] = '\0';
294
295                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
296                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
297                 free( psz_redir );
298
299                 if( b_index && ( p = strstr( f->file, "index." ) ) )
300                 {
301                     asprintf( &psz_redir, "%s%s", f->name, p );
302
303                     msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
304                     f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
305                                                      f->name, psz_redir );
306
307                     free( psz_redir );
308                 }
309             }
310         }
311     }
312
313     if( user )
314     {
315         free( user );
316     }
317     if( password )
318     {
319         free( password );
320     }
321
322     ACL_Destroy( p_acl );
323     closedir( p_dir );
324
325     return VLC_SUCCESS;
326 }
327
328
329 /**************************************************************************
330  * Locale functions
331  **************************************************************************/
332 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
333 {
334     intf_sys_t    *p_sys = p_intf->p_sys;
335
336     if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
337     {
338         size_t i_in = strlen(psz_utf8);
339         size_t i_out = i_in * 2;
340         char *psz_local = malloc(i_out + 1);
341         char *psz_out = psz_local;
342         size_t i_ret;
343         char psz_tmp[i_in + 1];
344         char *psz_in = psz_tmp;
345         uint8_t *p = (uint8_t *)psz_tmp;
346         strcpy( psz_tmp, psz_utf8 );
347
348         /* Fix Unicode quotes. If we are here we are probably converting
349          * to an inferior charset not understanding Unicode quotes. */
350         while( *p )
351         {
352             if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x99 )
353             {
354                 *p = '\'';
355                 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
356             }
357             if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x9a )
358             {
359                 *p = '"';
360                 memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
361             }
362             p++;
363         }
364         i_in = strlen( psz_tmp );
365
366         i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
367                            &psz_out, &i_out );
368         if( i_ret == (size_t)-1 || i_in )
369         {
370             msg_Warn( p_intf,
371                       "failed to convert \"%s\" to desired charset (%s)",
372                       psz_utf8, strerror(errno) );
373             free( psz_local );
374             return strdup( psz_utf8 );
375         }
376
377         *psz_out = '\0';
378         return psz_local;
379     }
380     else
381         return strdup( psz_utf8 );
382 }
383
384 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
385 {
386     intf_sys_t    *p_sys = p_intf->p_sys;
387
388     if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
389     {
390         char *psz_in = psz_local;
391         size_t i_in = strlen(psz_in);
392         size_t i_out = i_in * 6;
393         char *psz_utf8 = malloc(i_out + 1);
394         char *psz_out = psz_utf8;
395
396         size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
397                                   &psz_out, &i_out );
398         if( i_ret == (size_t)-1 || i_in )
399         {
400             msg_Warn( p_intf,
401                       "failed to convert \"%s\" to desired charset (%s)",
402                       psz_local, strerror(errno) );
403             free( psz_utf8 );
404             return strdup( psz_local );
405         }
406
407         *psz_out = '\0';
408         return psz_utf8;
409     }
410     else
411         return strdup( psz_local );
412 }
413
414 /*************************************************************************
415  * Playlist stuff
416  *************************************************************************/
417 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
418                            playlist_item_t *p_node, char *name, mvar_t *s,
419                            int i_depth )
420 {
421     if( p_node != NULL )
422     {
423         if( p_node->i_children == -1 )
424         {
425             char value[512];
426             char *psz;
427             mvar_t *itm = E_(mvar_New)( name, "set" );
428
429             sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
430             E_(mvar_AppendNewVar)( itm, "current", value );
431
432             sprintf( value, "%d", p_node->input.i_id );
433             E_(mvar_AppendNewVar)( itm, "index", value );
434
435             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
436             E_(mvar_AppendNewVar)( itm, "name", psz );
437             free( psz );
438
439             psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
440             E_(mvar_AppendNewVar)( itm, "uri", psz );
441             free( psz );
442
443             sprintf( value, "Item");
444             E_(mvar_AppendNewVar)( itm, "type", value );
445
446             sprintf( value, "%d", i_depth );
447             E_(mvar_AppendNewVar)( itm, "depth", value );
448
449             E_(mvar_AppendVar)( s, itm );
450         }
451         else
452         {
453             char value[512];
454             char *psz;
455             int i_child;
456             mvar_t *itm = E_(mvar_New)( name, "set" );
457
458             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
459             E_(mvar_AppendNewVar)( itm, "name", psz );
460             E_(mvar_AppendNewVar)( itm, "uri", psz );
461             free( psz );
462
463             sprintf( value, "Node" );
464             E_(mvar_AppendNewVar)( itm, "type", value );
465
466             sprintf( value, "%d", p_node->input.i_id );
467             E_(mvar_AppendNewVar)( itm, "index", value );
468
469             sprintf( value, "%d", p_node->i_children);
470             E_(mvar_AppendNewVar)( itm, "i_children", value );
471
472             sprintf( value, "%d", i_depth );
473             E_(mvar_AppendNewVar)( itm, "depth", value );
474
475             E_(mvar_AppendVar)( s, itm );
476
477             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
478                 E_(PlaylistListNode)( p_intf, p_pl,
479                                       p_node->pp_children[i_child],
480                                       name, s, i_depth + 1);
481
482         }
483     }
484 }
485
486 /****************************************************************************
487  * Seek command parsing handling
488  ****************************************************************************/
489 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
490 {
491     intf_sys_t     *p_sys = p_intf->p_sys;
492     vlc_value_t val;
493     int i_stock = 0;
494     uint64_t i_length;
495     int i_value = 0;
496     int i_relative = 0;
497 #define POSITION_ABSOLUTE 12
498 #define POSITION_REL_FOR 13
499 #define POSITION_REL_BACK 11
500 #define VL_TIME_ABSOLUTE 0
501 #define VL_TIME_REL_FOR 1
502 #define VL_TIME_REL_BACK -1
503     if( p_sys->p_input )
504     {
505         var_Get( p_sys->p_input, "length", &val );
506         i_length = val.i_time;
507
508         while( p_value[0] != '\0' )
509         {
510             switch(p_value[0])
511             {
512                 case '+':
513                 {
514                     i_relative = VL_TIME_REL_FOR;
515                     p_value++;
516                     break;
517                 }
518                 case '-':
519                 {
520                     i_relative = VL_TIME_REL_BACK;
521                     p_value++;
522                     break;
523                 }
524                 case '0': case '1': case '2': case '3': case '4':
525                 case '5': case '6': case '7': case '8': case '9':
526                 {
527                     i_stock = strtol( p_value , &p_value , 10 );
528                     break;
529                 }
530                 case '%': /* for percentage ie position */
531                 {
532                     i_relative += POSITION_ABSOLUTE;
533                     i_value = i_stock;
534                     i_stock = 0;
535                     p_value[0] = '\0';
536                     break;
537                 }
538                 case ':':
539                 {
540                     i_value = 60 * (i_value + i_stock) ;
541                     i_stock = 0;
542                     p_value++;
543                     break;
544                 }
545                 case 'h': case 'H': /* hours */
546                 {
547                     i_value += 3600 * i_stock;
548                     i_stock = 0;
549                     /* other characters which are not numbers are not important */
550                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
551                     {
552                         p_value++;
553                     }
554                     break;
555                 }
556                 case 'm': case 'M': case '\'': /* minutes */
557                 {
558                     i_value += 60 * i_stock;
559                     i_stock = 0;
560                     p_value++;
561                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
562                     {
563                         p_value++;
564                     }
565                     break;
566                 }
567                 case 's': case 'S': case '"':  /* seconds */
568                 {
569                     i_value += i_stock;
570                     i_stock = 0;
571                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
572                     {
573                         p_value++;
574                     }
575                     break;
576                 }
577                 default:
578                 {
579                     p_value++;
580                     break;
581                 }
582             }
583         }
584
585         /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
586         i_value += i_stock;
587
588         switch(i_relative)
589         {
590             case VL_TIME_ABSOLUTE:
591             {
592                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
593                     val.i_time = (uint64_t)( i_value ) * 1000000;
594                 else
595                     val.i_time = i_length;
596
597                 var_Set( p_sys->p_input, "time", val );
598                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
599                 break;
600             }
601             case VL_TIME_REL_FOR:
602             {
603                 var_Get( p_sys->p_input, "time", &val );
604                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
605                 {
606                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
607                 } else
608                 {
609                     val.i_time = i_length;
610                 }
611                 var_Set( p_sys->p_input, "time", val );
612                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
613                 break;
614             }
615             case VL_TIME_REL_BACK:
616             {
617                 var_Get( p_sys->p_input, "time", &val );
618                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
619                 {
620                     val.i_time = 0;
621                 } else
622                 {
623                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
624                 }
625                 var_Set( p_sys->p_input, "time", val );
626                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
627                 break;
628             }
629             case POSITION_ABSOLUTE:
630             {
631                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
632                 var_Set( p_sys->p_input, "position", val );
633                 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
634                 break;
635             }
636             case POSITION_REL_FOR:
637             {
638                 var_Get( p_sys->p_input, "position", &val );
639                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
640                 var_Set( p_sys->p_input, "position", val );
641                 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
642                 break;
643             }
644             case POSITION_REL_BACK:
645             {
646                 var_Get( p_sys->p_input, "position", &val );
647                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
648                 var_Set( p_sys->p_input, "position", val );
649                 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
650                 break;
651             }
652             default:
653             {
654                 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
655                 break;
656             }
657         }
658     }
659 #undef POSITION_ABSOLUTE
660 #undef POSITION_REL_FOR
661 #undef POSITION_REL_BACK
662 #undef VL_TIME_ABSOLUTE
663 #undef VL_TIME_REL_FOR
664 #undef VL_TIME_REL_BACK
665 }
666
667
668 /****************************************************************************
669  * URI Parsing functions
670  ****************************************************************************/
671 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
672 {
673     char *p = psz_uri;
674
675     while( (p = strstr( p, psz_name )) )
676     {
677         /* Verify that we are dealing with a post/get argument */
678         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
679               && p[strlen(psz_name)] == '=' )
680         {
681             return VLC_TRUE;
682         }
683         p++;
684     }
685
686     return VLC_FALSE;
687 }
688 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
689                              char *psz_value, int i_value_max )
690 {
691     char *p = psz_uri;
692
693     while( (p = strstr( p, psz_name )) )
694     {
695         /* Verify that we are dealing with a post/get argument */
696         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
697               && p[strlen(psz_name)] == '=' )
698             break;
699         p++;
700     }
701
702     if( p )
703     {
704         int i_len;
705
706         p += strlen( psz_name );
707         if( *p == '=' ) p++;
708
709         if( strchr( p, '&' ) )
710         {
711             i_len = strchr( p, '&' ) - p;
712         }
713         else
714         {
715             /* for POST method */
716             if( strchr( p, '\n' ) )
717             {
718                 i_len = strchr( p, '\n' ) - p;
719                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
720             }
721             else
722             {
723                 i_len = strlen( p );
724             }
725         }
726         i_len = __MIN( i_value_max - 1, i_len );
727         if( i_len > 0 )
728         {
729             strncpy( psz_value, p, i_len );
730             psz_value[i_len] = '\0';
731         }
732         else
733         {
734             strncpy( psz_value, "", i_value_max );
735         }
736         p += i_len;
737     }
738     else
739     {
740         strncpy( psz_value, "", i_value_max );
741     }
742
743     return p;
744 }
745
746 void E_(DecodeEncodedURI)( char *psz )
747 {
748     char *dup = strdup( psz );
749     char *p = dup;
750
751     while( *p )
752     {
753         if( *p == '%' )
754         {
755             char val[3];
756             p++;
757             if( !*p )
758             {
759                 break;
760             }
761
762             val[0] = *p++;
763             val[1] = *p++;
764             val[2] = '\0';
765
766             *psz++ = strtol( val, NULL, 16 );
767         }
768         else if( *p == '+' )
769         {
770             *psz++ = ' ';
771             p++;
772         }
773         else
774         {
775             *psz++ = *p++;
776         }
777     }
778     *psz++ = '\0';
779     free( dup );
780 }
781
782 /* Since the resulting string is smaller we can work in place, so it is
783  * permitted to have psz == new. new points to the first word of the
784  * string, the function returns the remaining string. */
785 char *E_(FirstWord)( char *psz, char *new )
786 {
787     vlc_bool_t b_end;
788
789     while( *psz == ' ' )
790         psz++;
791
792     while( *psz != '\0' && *psz != ' ' )
793     {
794         if( *psz == '\'' )
795         {
796             char c = *psz++;
797             while( *psz != '\0' && *psz != c )
798             {
799                 if( *psz == '\\' && psz[1] != '\0' )
800                     psz++;
801                 *new++ = *psz++;
802             }
803             if( *psz == c )
804                 psz++;
805         }
806         else
807         {
808             if( *psz == '\\' && psz[1] != '\0' )
809                 psz++;
810             *new++ = *psz++;
811         }
812     }
813     b_end = !*psz;
814
815     *new++ = '\0';
816     if( !b_end )
817         return psz + 1;
818     else
819         return NULL;
820 }
821
822 /**********************************************************************
823  * parse_MRL: parse the MRL, find the mrl string and the options,
824  * create an item with all information in it, and return the item.
825  * return NULL if there is an error.
826  **********************************************************************/
827 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *_psz,
828                                    char *psz_name )
829 {
830     char *psz = strdup( _psz );
831     char *s_mrl = psz;
832     char *s_temp;
833     playlist_item_t * p_item = NULL;
834
835     /* extract the mrl */
836     s_temp = E_(FirstWord)( s_mrl, s_mrl );
837     if( s_temp == NULL )
838     {
839         s_temp = s_mrl + strlen( s_mrl );
840     }
841
842     p_item = playlist_ItemNew( p_intf, s_mrl, psz_name );
843     s_mrl = s_temp;
844
845     /* now we can take care of the options */
846     while( *s_mrl != '\0' )
847     {
848         s_temp = E_(FirstWord)( s_mrl, s_mrl );
849         if( s_mrl == '\0' )
850             break;
851         if( s_temp == NULL )
852         {
853             s_temp = s_mrl + strlen( s_mrl );
854         }
855         if( *s_mrl != ':' )
856             msg_Warn( p_intf, "invalid MRL option: %s", s_mrl );
857         else
858             playlist_ItemAddOption( p_item, s_mrl );
859         s_mrl = s_temp;
860     }
861
862     free( psz );
863     return p_item;
864 }
865
866 /**********************************************************************
867  * RealPath: parse ../, ~ and path stuff
868  **********************************************************************/
869 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
870 {
871     char *psz_dir;
872     char *p;
873     int i_len = strlen(psz_src);
874     char sep;
875
876 #if defined( WIN32 )
877     sep = '\\';
878 #else
879     sep = '/';
880 #endif
881
882     psz_dir = malloc( i_len + 2 );
883     strcpy( psz_dir, psz_src );
884
885     /* Add a trailing sep to ease the .. step */
886     psz_dir[i_len] = sep;
887     psz_dir[i_len + 1] = '\0';
888
889 #ifdef WIN32
890     /* Convert all / to native separator */
891     p = psz_dir;
892     while( (p = strchr( p, '/' )) != NULL )
893     {
894         *p = sep;
895     }
896 #endif
897
898     /* Remove multiple separators and /./ */
899     p = psz_dir;
900     while( (p = strchr( p, sep )) != NULL )
901     {
902         if( p[1] == sep )
903             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
904         else if( p[1] == '.' && p[2] == sep )
905             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
906         else
907             p++;
908     }
909
910     if( psz_dir[0] == '~' )
911     {
912         char *dir = malloc( strlen(psz_dir)
913                              + strlen(p_intf->p_vlc->psz_userdir) );
914         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
915         sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
916         free( psz_dir );
917         psz_dir = dir;
918     }
919
920     if( strlen(psz_dir) > 2 )
921     {
922         /* Fix all .. dir */
923         p = psz_dir + 3;
924         while( (p = strchr( p, sep )) != NULL )
925         {
926             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
927             {
928                 char *q;
929                 p[-3] = '\0';
930                 if( (q = strrchr( psz_dir, sep )) != NULL )
931                 {
932                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
933                     p = q + 1;
934                 }
935                 else
936                 {
937                     memmove( psz_dir, p, strlen(p) + 1 );
938                     p = psz_dir + 3;
939                 }
940             }
941             else
942                 p++;
943         }
944     }
945
946     /* Remove trailing sep if there are at least 2 sep in the string
947      * (handles the C:\ stuff) */
948     p = strrchr( psz_dir, sep );
949     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
950         *p = '\0';
951
952     return psz_dir;
953 }