]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
1a9e6b6576d2fb8aa14398ece1c49c9af1a259c5
[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$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "http.h"
27 #include "vlc_strings.h"
28
29 /****************************************************************************
30  * File and directory functions
31  ****************************************************************************/
32
33 /* ToUrl: create a good name for an url from filename */
34 char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
35 {
36     char *url, *p;
37
38     url = p = malloc( strlen( name ) + 1 );
39
40     *pb_index = VLC_FALSE;
41     if( !url || !p )
42     {
43         return NULL;
44     }
45
46 #ifdef WIN32
47     while( *name == '\\' || *name == '/' )
48 #else
49     while( *name == '/' )
50 #endif
51     {
52         name++;
53     }
54
55     *p++ = '/';
56     strcpy( p, name );
57
58 #ifdef WIN32
59     /* convert '\\' into '/' */
60     name = p;
61     while( *name )
62     {
63         if( *name == '\\' )
64             *name = '/';
65         name++;
66     }
67 #endif
68
69     /* index.* -> / */
70     if( ( p = strrchr( url, '/' ) ) != NULL )
71     {
72         if( !strncmp( p, "/index.", 7 ) )
73         {
74             p[1] = '\0';
75             *pb_index = VLC_TRUE;
76         }
77     }
78     return url;
79 }
80
81 /* Load a file */
82 int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
83 {
84     int i_read;
85
86     /* just load the file */
87     *pi_data = 0;
88     *pp_data = malloc( 1025 );  /* +1 for \0 */
89     while( ( i_read = fread( &(*pp_data)[*pi_data], 1, 1024, f ) ) == 1024 )
90     {
91         *pi_data += 1024;
92         *pp_data = realloc( *pp_data, *pi_data  + 1025 );
93     }
94     if( i_read > 0 )
95     {
96         *pi_data += i_read;
97     }
98     (*pp_data)[*pi_data] = '\0';
99
100     return VLC_SUCCESS;
101 }
102
103 /* Parse a directory and recursively add files */
104 int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
105                         char *psz_dir )
106 {
107     intf_sys_t     *p_sys = p_intf->p_sys;
108     char           dir[MAX_DIR_SIZE];
109 #ifdef HAVE_SYS_STAT_H
110     struct stat   stat_info;
111 #endif
112     DIR           *p_dir;
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( utf8_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 = utf8_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 = utf8_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         const char *psz_filename;
196         /* parse psz_src dir */
197         if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
198         {
199             break;
200         }
201
202         if( ( psz_filename[0] == '.' )
203          || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
204             continue;
205
206         sprintf( dir, "%s%c%s", psz_dir, sep, psz_filename );
207         LocaleFree( psz_filename );
208
209         if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
210         {
211             httpd_file_sys_t *f = NULL;
212             httpd_handler_sys_t *h = NULL;
213             vlc_bool_t b_index;
214             char *psz_tmp, *psz_file, *psz_name, *psz_ext;
215
216             psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
217             psz_file = E_(FromUTF8)( p_intf, psz_tmp );
218             free( psz_tmp );
219             psz_tmp = vlc_fix_readdir_charset( p_intf,
220                                                &dir[strlen( psz_root )] );
221             psz_name = E_(FileToUrl)( psz_tmp, &b_index );
222             free( psz_tmp );
223             psz_ext = strrchr( psz_file, '.' );
224             if( psz_ext != NULL )
225             {
226                 int i;
227                 psz_ext++;
228                 for( i = 0; i < p_sys->i_handlers; i++ )
229                     if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) )
230                         break;
231                 if( i < p_sys->i_handlers )
232                 {
233                     f = malloc( sizeof( httpd_handler_sys_t ) );
234                     h = (httpd_handler_sys_t *)f;
235                     f->b_handler = VLC_TRUE;
236                     h->p_association = p_sys->pp_handlers[i];
237                 }
238             }
239             if( f == NULL )
240             {
241                 f = malloc( sizeof( httpd_file_sys_t ) );
242                 f->b_handler = VLC_FALSE;
243             }
244
245             f->p_intf  = p_intf;
246             f->p_file = NULL;
247             f->p_redir = NULL;
248             f->p_redir2 = NULL;
249             f->file = psz_file;
250             f->name = psz_name;
251             f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? VLC_TRUE : VLC_FALSE;
252
253             if( !f->name )
254             {
255                 msg_Err( p_intf , "unable to parse directory" );
256                 closedir( p_dir );
257                 free( f );
258                 return( VLC_ENOMEM );
259             }
260             msg_Dbg( p_intf, "file=%s (url=%s)",
261                      f->file, f->name );
262
263             if( !f->b_handler )
264             {
265                 char *psz_type = strdup( p_sys->psz_html_type );
266                 if( strstr( &dir[strlen( psz_root )], ".xml" ) )
267                 {
268                     char *psz = strstr( psz_type, "html;" );
269                     if( psz )
270                     {
271                         psz[0] = 'x';
272                         psz[1] = 'm';
273                         psz[2] = 'l';
274                         psz[3] = ';';
275                         psz[4] = ' ';
276                     }
277                 }
278                 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
279                                            f->name,
280                                            f->b_html ? psz_type : NULL,
281                                            user, password, p_acl,
282                                            E_(HttpCallback), f );
283                 free( psz_type );
284                 if( f->p_file != NULL )
285                 {
286                     TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
287                 }
288             }
289             else
290             {
291                 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
292                                                  f->name,
293                                                  user, password, p_acl,
294                                                  E_(HandlerCallback), h );
295                 if( h->p_handler != NULL )
296                 {
297                     TAB_APPEND( p_sys->i_files, p_sys->pp_files,
298                                 (httpd_file_sys_t *)h );
299                 }
300             }
301
302             /* for url that ends by / add
303              *  - a redirect from rep to rep/
304              *  - in case of index.* rep/index.html to rep/ */
305             if( f && f->name[strlen(f->name) - 1] == '/' )
306             {
307                 char *psz_redir = strdup( f->name );
308                 char *p;
309                 psz_redir[strlen( psz_redir ) - 1] = '\0';
310
311                 msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
312                 f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir );
313                 free( psz_redir );
314
315                 if( b_index && ( p = strstr( f->file, "index." ) ) )
316                 {
317                     asprintf( &psz_redir, "%s%s", f->name, p );
318
319                     msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
320                     f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
321                                                      f->name, psz_redir );
322
323                     free( psz_redir );
324                 }
325             }
326         }
327     }
328
329     if( user )
330     {
331         free( user );
332     }
333     if( password )
334     {
335         free( password );
336     }
337
338     ACL_Destroy( p_acl );
339     closedir( p_dir );
340
341     return VLC_SUCCESS;
342 }
343
344
345 /**************************************************************************
346  * Locale functions
347  **************************************************************************/
348 char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
349 {
350     intf_sys_t    *p_sys = p_intf->p_sys;
351
352     if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
353     {
354         size_t i_in = strlen(psz_utf8);
355         size_t i_out = i_in * 2;
356         char *psz_local = malloc(i_out + 1);
357         char *psz_out = psz_local;
358         size_t i_ret;
359         char psz_tmp[i_in + 1];
360         const char *psz_in = psz_tmp;
361         strcpy( psz_tmp, psz_utf8 );
362
363         i_in = strlen( psz_tmp );
364
365         i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
366                            &psz_out, &i_out );
367         if( i_ret == (size_t)-1 || i_in )
368         {
369             msg_Warn( p_intf,
370                       "failed to convert \"%s\" to desired charset (%s)",
371                       psz_utf8, strerror(errno) );
372             free( psz_local );
373             return strdup( psz_utf8 );
374         }
375
376         *psz_out = '\0';
377         return psz_local;
378     }
379     else
380         return strdup( psz_utf8 );
381 }
382
383 char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
384 {
385     intf_sys_t    *p_sys = p_intf->p_sys;
386
387     if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
388     {
389         const char *psz_in = psz_local;
390         size_t i_in = strlen(psz_in);
391         size_t i_out = i_in * 6;
392         char *psz_utf8 = malloc(i_out + 1);
393         char *psz_out = psz_utf8;
394
395         size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
396                                   &psz_out, &i_out );
397         if( i_ret == (size_t)-1 || i_in )
398         {
399             msg_Warn( p_intf,
400                       "failed to convert \"%s\" to desired charset (%s)",
401                       psz_local, strerror(errno) );
402             free( psz_utf8 );
403             return strdup( psz_local );
404         }
405
406         *psz_out = '\0';
407         return psz_utf8;
408     }
409     else
410         return strdup( psz_local );
411 }
412
413 /*************************************************************************
414  * Playlist stuff
415  *************************************************************************/
416 void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
417                            playlist_item_t *p_node, char *name, mvar_t *s,
418                            int i_depth )
419 {
420     if( p_node != NULL )
421     {
422         if( p_node->i_children == -1 )
423         {
424             char value[512];
425             char *psz;
426             mvar_t *itm = E_(mvar_New)( name, "set" );
427
428             sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
429             E_(mvar_AppendNewVar)( itm, "current", value );
430
431             sprintf( value, "%d", p_node->input.i_id );
432             E_(mvar_AppendNewVar)( itm, "index", value );
433
434             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
435             E_(mvar_AppendNewVar)( itm, "name", psz );
436             free( psz );
437
438             psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
439             E_(mvar_AppendNewVar)( itm, "uri", psz );
440             free( psz );
441
442             sprintf( value, "Item");
443             E_(mvar_AppendNewVar)( itm, "type", value );
444
445             sprintf( value, "%d", i_depth );
446             E_(mvar_AppendNewVar)( itm, "depth", value );
447
448             if( p_node->i_flags & PLAYLIST_RO_FLAG )
449             {
450                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
451             }
452             else
453             {
454                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
455             }
456
457             sprintf( value, "%ld", (long)p_node->input.i_duration );
458             E_(mvar_AppendNewVar)( itm, "duration", value );
459
460             E_(mvar_AppendVar)( s, itm );
461         }
462         else
463         {
464             char value[512];
465             char *psz;
466             int i_child;
467             mvar_t *itm = E_(mvar_New)( name, "set" );
468
469             psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
470             E_(mvar_AppendNewVar)( itm, "name", psz );
471             E_(mvar_AppendNewVar)( itm, "uri", psz );
472             free( psz );
473
474             sprintf( value, "Node" );
475             E_(mvar_AppendNewVar)( itm, "type", value );
476
477             sprintf( value, "%d", p_node->input.i_id );
478             E_(mvar_AppendNewVar)( itm, "index", value );
479
480             sprintf( value, "%d", p_node->i_children);
481             E_(mvar_AppendNewVar)( itm, "i_children", value );
482
483             sprintf( value, "%d", i_depth );
484             E_(mvar_AppendNewVar)( itm, "depth", value );
485
486             if( p_node->i_flags & PLAYLIST_RO_FLAG )
487             {
488                 E_(mvar_AppendNewVar)( itm, "ro", "ro" );
489             }
490             else
491             {
492                 E_(mvar_AppendNewVar)( itm, "ro", "rw" );
493             }
494
495             E_(mvar_AppendVar)( s, itm );
496
497             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
498                 E_(PlaylistListNode)( p_intf, p_pl,
499                                       p_node->pp_children[i_child],
500                                       name, s, i_depth + 1);
501
502         }
503     }
504 }
505
506 /****************************************************************************
507  * Seek command parsing handling
508  ****************************************************************************/
509 void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
510 {
511     intf_sys_t     *p_sys = p_intf->p_sys;
512     vlc_value_t val;
513     int i_stock = 0;
514     uint64_t i_length;
515     int i_value = 0;
516     int i_relative = 0;
517 #define POSITION_ABSOLUTE 12
518 #define POSITION_REL_FOR 13
519 #define POSITION_REL_BACK 11
520 #define VL_TIME_ABSOLUTE 0
521 #define VL_TIME_REL_FOR 1
522 #define VL_TIME_REL_BACK -1
523     if( p_sys->p_input )
524     {
525         var_Get( p_sys->p_input, "length", &val );
526         i_length = val.i_time;
527
528         while( p_value[0] != '\0' )
529         {
530             switch(p_value[0])
531             {
532                 case '+':
533                 {
534                     i_relative = VL_TIME_REL_FOR;
535                     p_value++;
536                     break;
537                 }
538                 case '-':
539                 {
540                     i_relative = VL_TIME_REL_BACK;
541                     p_value++;
542                     break;
543                 }
544                 case '0': case '1': case '2': case '3': case '4':
545                 case '5': case '6': case '7': case '8': case '9':
546                 {
547                     i_stock = strtol( p_value , &p_value , 10 );
548                     break;
549                 }
550                 case '%': /* for percentage ie position */
551                 {
552                     i_relative += POSITION_ABSOLUTE;
553                     i_value = i_stock;
554                     i_stock = 0;
555                     p_value[0] = '\0';
556                     break;
557                 }
558                 case ':':
559                 {
560                     i_value = 60 * (i_value + i_stock) ;
561                     i_stock = 0;
562                     p_value++;
563                     break;
564                 }
565                 case 'h': case 'H': /* hours */
566                 {
567                     i_value += 3600 * i_stock;
568                     i_stock = 0;
569                     /* other characters which are not numbers are not important */
570                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
571                     {
572                         p_value++;
573                     }
574                     break;
575                 }
576                 case 'm': case 'M': case '\'': /* minutes */
577                 {
578                     i_value += 60 * i_stock;
579                     i_stock = 0;
580                     p_value++;
581                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
582                     {
583                         p_value++;
584                     }
585                     break;
586                 }
587                 case 's': case 'S': case '"':  /* seconds */
588                 {
589                     i_value += i_stock;
590                     i_stock = 0;
591                     while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
592                     {
593                         p_value++;
594                     }
595                     break;
596                 }
597                 default:
598                 {
599                     p_value++;
600                     break;
601                 }
602             }
603         }
604
605         /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
606         i_value += i_stock;
607
608         switch(i_relative)
609         {
610             case VL_TIME_ABSOLUTE:
611             {
612                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
613                     val.i_time = (uint64_t)( i_value ) * 1000000;
614                 else
615                     val.i_time = i_length;
616
617                 var_Set( p_sys->p_input, "time", val );
618                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
619                 break;
620             }
621             case VL_TIME_REL_FOR:
622             {
623                 var_Get( p_sys->p_input, "time", &val );
624                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
625                 {
626                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
627                 } else
628                 {
629                     val.i_time = i_length;
630                 }
631                 var_Set( p_sys->p_input, "time", val );
632                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
633                 break;
634             }
635             case VL_TIME_REL_BACK:
636             {
637                 var_Get( p_sys->p_input, "time", &val );
638                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
639                 {
640                     val.i_time = 0;
641                 } else
642                 {
643                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
644                 }
645                 var_Set( p_sys->p_input, "time", val );
646                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
647                 break;
648             }
649             case POSITION_ABSOLUTE:
650             {
651                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
652                 var_Set( p_sys->p_input, "position", val );
653                 msg_Dbg( p_intf, "requested seek percent: %d", i_value );
654                 break;
655             }
656             case POSITION_REL_FOR:
657             {
658                 var_Get( p_sys->p_input, "position", &val );
659                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
660                 var_Set( p_sys->p_input, "position", val );
661                 msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
662                 break;
663             }
664             case POSITION_REL_BACK:
665             {
666                 var_Get( p_sys->p_input, "position", &val );
667                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
668                 var_Set( p_sys->p_input, "position", val );
669                 msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
670                 break;
671             }
672             default:
673             {
674                 msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
675                 break;
676             }
677         }
678     }
679 #undef POSITION_ABSOLUTE
680 #undef POSITION_REL_FOR
681 #undef POSITION_REL_BACK
682 #undef VL_TIME_ABSOLUTE
683 #undef VL_TIME_REL_FOR
684 #undef VL_TIME_REL_BACK
685 }
686
687
688 /****************************************************************************
689  * URI Parsing functions
690  ****************************************************************************/
691 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
692 {
693     char *p = psz_uri;
694
695     while( (p = strstr( p, psz_name )) )
696     {
697         /* Verify that we are dealing with a post/get argument */
698         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
699               && p[strlen(psz_name)] == '=' )
700         {
701             return VLC_TRUE;
702         }
703         p++;
704     }
705
706     return VLC_FALSE;
707 }
708 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
709                              char *psz_value, int i_value_max )
710 {
711     char *p = psz_uri;
712
713     while( (p = strstr( p, psz_name )) )
714     {
715         /* Verify that we are dealing with a post/get argument */
716         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
717               && p[strlen(psz_name)] == '=' )
718             break;
719         p++;
720     }
721
722     if( p )
723     {
724         int i_len;
725
726         p += strlen( psz_name );
727         if( *p == '=' ) p++;
728
729         if( strchr( p, '&' ) )
730         {
731             i_len = strchr( p, '&' ) - p;
732         }
733         else
734         {
735             /* for POST method */
736             if( strchr( p, '\n' ) )
737             {
738                 i_len = strchr( p, '\n' ) - p;
739                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
740             }
741             else
742             {
743                 i_len = strlen( p );
744             }
745         }
746         i_len = __MIN( i_value_max - 1, i_len );
747         if( i_len > 0 )
748         {
749             strncpy( psz_value, p, i_len );
750             psz_value[i_len] = '\0';
751         }
752         else
753         {
754             strncpy( psz_value, "", i_value_max );
755         }
756         p += i_len;
757     }
758     else
759     {
760         strncpy( psz_value, "", i_value_max );
761     }
762
763     return p;
764 }
765
766 void E_(DecodeEncodedURI)( char *psz )
767 {
768     decode_encoded_URI( psz );
769 }
770
771 /* Since the resulting string is smaller we can work in place, so it is
772  * permitted to have psz == new. new points to the first word of the
773  * string, the function returns the remaining string. */
774 char *E_(FirstWord)( char *psz, char *new )
775 {
776     vlc_bool_t b_end;
777
778     while( *psz == ' ' )
779         psz++;
780
781     while( *psz != '\0' && *psz != ' ' )
782     {
783         if( *psz == '\'' )
784         {
785             char c = *psz++;
786             while( *psz != '\0' && *psz != c )
787             {
788                 if( *psz == '\\' && psz[1] != '\0' )
789                     psz++;
790                 *new++ = *psz++;
791             }
792             if( *psz == c )
793                 psz++;
794         }
795         else
796         {
797             if( *psz == '\\' && psz[1] != '\0' )
798                 psz++;
799             *new++ = *psz++;
800         }
801     }
802     b_end = !*psz;
803
804     *new++ = '\0';
805     if( !b_end )
806         return psz + 1;
807     else
808         return NULL;
809 }
810 /**********************************************************************
811  * Find_end_MRL: Find the end of the sentence :
812  * this function parses the string psz and find the end of the item
813  * and/or option with detecting the " and ' problems.
814  * returns NULL if an error is detected, otherwise, returns a pointer
815  * of the end of the sentence (after the last character)
816 **********************************************************************/
817 static char *Find_end_MRL( char *psz )
818 {
819     char *s_sent = psz;
820
821     switch( *s_sent )
822     {
823         case '\"':
824         {
825             s_sent++;
826
827             while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
828             {
829                 if( *s_sent == '\'' )
830                 {
831                     s_sent = Find_end_MRL( s_sent );
832
833                     if( s_sent == NULL )
834                     {
835                         return NULL;
836                     }
837                 } else
838                 {
839                     s_sent++;
840                 }
841             }
842
843             if( *s_sent == '\"' )
844             {
845                 s_sent++;
846                 return s_sent;
847             } else  /* *s_sent == '\0' , which means the number of " is incorrect */
848             {
849                 return NULL;
850             }
851             break;
852         }
853         case '\'':
854         {
855             s_sent++;
856
857             while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
858             {
859                 if( *s_sent == '\"' )
860                 {
861                     s_sent = Find_end_MRL( s_sent );
862
863                     if( s_sent == NULL )
864                     {
865                         return NULL;
866                     }
867                 } else
868                 {
869                     s_sent++;
870                 }
871             }
872
873             if( *s_sent == '\'' )
874             {
875                 s_sent++;
876                 return s_sent;
877             } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
878             {
879                 return NULL;
880             }
881             break;
882         }
883         default: /* now we can look for spaces */
884         {
885             while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
886             {
887                 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
888                 {
889                     s_sent = Find_end_MRL( s_sent );
890                 } else
891                 {
892                     s_sent++;
893                 }
894             }
895             return s_sent;
896         }
897     }
898 }
899 /**********************************************************************
900  * parse_MRL: parse the MRL, find the mrl string and the options,
901  * create an item with all information in it, and return the item.
902  * return NULL if there is an error.
903  **********************************************************************/
904 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
905                                    char *psz_name )
906 {
907     char **ppsz_options = NULL;
908     char *mrl;
909     char *s_mrl = psz;
910     int i_error = 0;
911     char *s_temp;
912     int i = 0;
913     int i_options = 0;
914     playlist_item_t * p_item = NULL;
915
916     /* In case there is spaces before the mrl */
917     while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
918     {
919         s_mrl++;
920     }
921
922     /* extract the mrl */
923     s_temp = strstr( s_mrl , " :" );
924     if( s_temp == NULL )
925     {
926         s_temp = s_mrl + strlen( s_mrl );
927     } else
928     {
929         while( (*s_temp == ' ') && (s_temp != s_mrl ) )
930         {
931             s_temp--;
932         }
933         s_temp++;
934     }
935
936     /* if the mrl is between " or ', we must remove them */
937     if( (*s_mrl == '\'') || (*s_mrl == '\"') )
938     {
939         mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
940         strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
941         mrl[ s_temp - s_mrl - 2 ] = '\0';
942     } else
943     {
944         mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
945         strncpy( mrl , s_mrl , s_temp - s_mrl );
946         mrl[ s_temp - s_mrl ] = '\0';
947     }
948
949     s_mrl = s_temp;
950
951     /* now we can take care of the options */
952     while( (*s_mrl != '\0') && (i_error == 0) )
953     {
954         switch( *s_mrl )
955         {
956             case ' ':
957             {
958                 s_mrl++;
959                 break;
960             }
961             case ':': /* an option */
962             {
963                 s_temp = Find_end_MRL( s_mrl );
964
965                 if( s_temp == NULL )
966                 {
967                     i_error = 1;
968                 }
969                 else
970                 {
971                     i_options++;
972                     ppsz_options = realloc( ppsz_options , i_options *
973                                             sizeof(char *) );
974                     ppsz_options[ i_options - 1 ] =
975                         malloc( (s_temp - s_mrl + 1) * sizeof(char) );
976
977                     strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
978                              s_temp - s_mrl );
979
980                     /* don't forget to finish the string with a '\0' */
981                     (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
982
983                     s_mrl = s_temp;
984                 }
985                 break;
986             }
987             default:
988             {
989                 i_error = 1;
990                 break;
991             }
992         }
993     }
994
995     if( i_error != 0 )
996     {
997         free( mrl );
998     }
999     else
1000     {
1001         /* now create an item */
1002         p_item = playlist_ItemNew( p_intf, mrl, psz_name );
1003         for( i = 0 ; i< i_options ; i++ )
1004         {
1005             playlist_ItemAddOption( p_item, ppsz_options[i] );
1006         }
1007     }
1008
1009     for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
1010     if( i_options ) free( ppsz_options );
1011
1012     return p_item;
1013 }
1014 /**********************************************************************
1015  * RealPath: parse ../, ~ and path stuff
1016  **********************************************************************/
1017 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
1018 {
1019     char *psz_dir;
1020     char *p;
1021     int i_len = strlen(psz_src);
1022     char sep;
1023
1024 #if defined( WIN32 )
1025     sep = '\\';
1026 #else
1027     sep = '/';
1028 #endif
1029
1030     psz_dir = malloc( i_len + 2 );
1031     strcpy( psz_dir, psz_src );
1032
1033     /* Add a trailing sep to ease the .. step */
1034     psz_dir[i_len] = sep;
1035     psz_dir[i_len + 1] = '\0';
1036
1037 #ifdef WIN32
1038     /* Convert all / to native separator */
1039     p = psz_dir;
1040     while( (p = strchr( p, '/' )) != NULL )
1041     {
1042         *p = sep;
1043     }
1044 #endif
1045
1046     /* Remove multiple separators and /./ */
1047     p = psz_dir;
1048     while( (p = strchr( p, sep )) != NULL )
1049     {
1050         if( p[1] == sep )
1051             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
1052         else if( p[1] == '.' && p[2] == sep )
1053             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
1054         else
1055             p++;
1056     }
1057
1058     if( psz_dir[0] == '~' )
1059     {
1060         char *dir = malloc( strlen(psz_dir)
1061                              + strlen(p_intf->p_vlc->psz_userdir) );
1062         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1063         sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1064         free( psz_dir );
1065         psz_dir = dir;
1066     }
1067
1068     if( strlen(psz_dir) > 2 )
1069     {
1070         /* Fix all .. dir */
1071         p = psz_dir + 3;
1072         while( (p = strchr( p, sep )) != NULL )
1073         {
1074             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1075             {
1076                 char *q;
1077                 p[-3] = '\0';
1078                 if( (q = strrchr( psz_dir, sep )) != NULL )
1079                 {
1080                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1081                     p = q + 1;
1082                 }
1083                 else
1084                 {
1085                     memmove( psz_dir, p, strlen(p) + 1 );
1086                     p = psz_dir + 3;
1087                 }
1088             }
1089             else
1090                 p++;
1091         }
1092     }
1093
1094     /* Remove trailing sep if there are at least 2 sep in the string
1095      * (handles the C:\ stuff) */
1096     p = strrchr( psz_dir, sep );
1097     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
1098         *p = '\0';
1099
1100     return psz_dir;
1101 }