]> git.sesse.net Git - vlc/blob - modules/control/http/util.c
Control interfaces first string review.
[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 directory (%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 directory (%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
570                      * important */
571                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
572                            && (p_value[0] != '\0') )
573                     {
574                         p_value++;
575                     }
576                     break;
577                 }
578                 case 'm': case 'M': case '\'': /* minutes */
579                 {
580                     i_value += 60 * i_stock;
581                     i_stock = 0;
582                     p_value++;
583                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
584                            && (p_value[0] != '\0') )
585                     {
586                         p_value++;
587                     }
588                     break;
589                 }
590                 case 's': case 'S': case '"':  /* seconds */
591                 {
592                     i_value += i_stock;
593                     i_stock = 0;
594                     while( ((p_value[0] < '0') || (p_value[0] > '9'))
595                            && (p_value[0] != '\0') )
596                     {
597                         p_value++;
598                     }
599                     break;
600                 }
601                 default:
602                 {
603                     p_value++;
604                     break;
605                 }
606             }
607         }
608
609         /* if there is no known symbol, I consider it as seconds.
610          * Otherwise, i_stock = 0 */
611         i_value += i_stock;
612
613         switch(i_relative)
614         {
615             case VL_TIME_ABSOLUTE:
616             {
617                 if( (uint64_t)( i_value ) * 1000000 <= i_length )
618                     val.i_time = (uint64_t)( i_value ) * 1000000;
619                 else
620                     val.i_time = i_length;
621
622                 var_Set( p_sys->p_input, "time", val );
623                 msg_Dbg( p_intf, "requested seek position: %dsec", i_value );
624                 break;
625             }
626             case VL_TIME_REL_FOR:
627             {
628                 var_Get( p_sys->p_input, "time", &val );
629                 if( (uint64_t)( i_value ) * 1000000 + val.i_time <= i_length )
630                 {
631                     val.i_time = ((uint64_t)( i_value ) * 1000000) + val.i_time;
632                 } else
633                 {
634                     val.i_time = i_length;
635                 }
636                 var_Set( p_sys->p_input, "time", val );
637                 msg_Dbg( p_intf, "requested seek position forward: %dsec", i_value );
638                 break;
639             }
640             case VL_TIME_REL_BACK:
641             {
642                 var_Get( p_sys->p_input, "time", &val );
643                 if( (int64_t)( i_value ) * 1000000 > val.i_time )
644                 {
645                     val.i_time = 0;
646                 } else
647                 {
648                     val.i_time = val.i_time - ((uint64_t)( i_value ) * 1000000);
649                 }
650                 var_Set( p_sys->p_input, "time", val );
651                 msg_Dbg( p_intf, "requested seek position backward: %dsec", i_value );
652                 break;
653             }
654             case POSITION_ABSOLUTE:
655             {
656                 val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
657                                             0.0 ), 100.0 );
658                 var_Set( p_sys->p_input, "position", val );
659                 msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
660                 break;
661             }
662             case POSITION_REL_FOR:
663             {
664                 var_Get( p_sys->p_input, "position", &val );
665                 val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
666                                              0.0 ) , 100.0 );
667                 var_Set( p_sys->p_input, "position", val );
668                 msg_Dbg( p_intf, "requested seek percent forward: %d%%",
669                          i_value );
670                 break;
671             }
672             case POSITION_REL_BACK:
673             {
674                 var_Get( p_sys->p_input, "position", &val );
675                 val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
676                                              0.0 ) , 100.0 );
677                 var_Set( p_sys->p_input, "position", val );
678                 msg_Dbg( p_intf, "requested seek percent backward: %d%%",
679                          i_value );
680                 break;
681             }
682             default:
683             {
684                 msg_Dbg( p_intf, "invalid seek request" );
685                 break;
686             }
687         }
688     }
689 #undef POSITION_ABSOLUTE
690 #undef POSITION_REL_FOR
691 #undef POSITION_REL_BACK
692 #undef VL_TIME_ABSOLUTE
693 #undef VL_TIME_REL_FOR
694 #undef VL_TIME_REL_BACK
695 }
696
697
698 /****************************************************************************
699  * URI Parsing functions
700  ****************************************************************************/
701 int E_(TestURIParam)( char *psz_uri, const char *psz_name )
702 {
703     char *p = psz_uri;
704
705     while( (p = strstr( p, psz_name )) )
706     {
707         /* Verify that we are dealing with a post/get argument */
708         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
709               && p[strlen(psz_name)] == '=' )
710         {
711             return VLC_TRUE;
712         }
713         p++;
714     }
715
716     return VLC_FALSE;
717 }
718 char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
719                              char *psz_value, int i_value_max )
720 {
721     char *p = psz_uri;
722
723     while( (p = strstr( p, psz_name )) )
724     {
725         /* Verify that we are dealing with a post/get argument */
726         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
727               && p[strlen(psz_name)] == '=' )
728             break;
729         p++;
730     }
731
732     if( p )
733     {
734         int i_len;
735
736         p += strlen( psz_name );
737         if( *p == '=' ) p++;
738
739         if( strchr( p, '&' ) )
740         {
741             i_len = strchr( p, '&' ) - p;
742         }
743         else
744         {
745             /* for POST method */
746             if( strchr( p, '\n' ) )
747             {
748                 i_len = strchr( p, '\n' ) - p;
749                 if( i_len && *(p+i_len-1) == '\r' ) i_len--;
750             }
751             else
752             {
753                 i_len = strlen( p );
754             }
755         }
756         i_len = __MIN( i_value_max - 1, i_len );
757         if( i_len > 0 )
758         {
759             strncpy( psz_value, p, i_len );
760             psz_value[i_len] = '\0';
761         }
762         else
763         {
764             strncpy( psz_value, "", i_value_max );
765         }
766         p += i_len;
767     }
768     else
769     {
770         strncpy( psz_value, "", i_value_max );
771     }
772
773     return p;
774 }
775
776 void E_(DecodeEncodedURI)( char *psz )
777 {
778     decode_encoded_URI( psz );
779 }
780
781 /* Since the resulting string is smaller we can work in place, so it is
782  * permitted to have psz == new. new points to the first word of the
783  * string, the function returns the remaining string. */
784 char *E_(FirstWord)( char *psz, char *new )
785 {
786     vlc_bool_t b_end;
787
788     while( *psz == ' ' )
789         psz++;
790
791     while( *psz != '\0' && *psz != ' ' )
792     {
793         if( *psz == '\'' )
794         {
795             char c = *psz++;
796             while( *psz != '\0' && *psz != c )
797             {
798                 if( *psz == '\\' && psz[1] != '\0' )
799                     psz++;
800                 *new++ = *psz++;
801             }
802             if( *psz == c )
803                 psz++;
804         }
805         else
806         {
807             if( *psz == '\\' && psz[1] != '\0' )
808                 psz++;
809             *new++ = *psz++;
810         }
811     }
812     b_end = !*psz;
813
814     *new++ = '\0';
815     if( !b_end )
816         return psz + 1;
817     else
818         return NULL;
819 }
820 /**********************************************************************
821  * Find_end_MRL: Find the end of the sentence :
822  * this function parses the string psz and find the end of the item
823  * and/or option with detecting the " and ' problems.
824  * returns NULL if an error is detected, otherwise, returns a pointer
825  * of the end of the sentence (after the last character)
826 **********************************************************************/
827 static char *Find_end_MRL( char *psz )
828 {
829     char *s_sent = psz;
830
831     switch( *s_sent )
832     {
833         case '\"':
834         {
835             s_sent++;
836
837             while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
838             {
839                 if( *s_sent == '\'' )
840                 {
841                     s_sent = Find_end_MRL( s_sent );
842
843                     if( s_sent == NULL )
844                     {
845                         return NULL;
846                     }
847                 } else
848                 {
849                     s_sent++;
850                 }
851             }
852
853             if( *s_sent == '\"' )
854             {
855                 s_sent++;
856                 return s_sent;
857             } else  /* *s_sent == '\0' , which means the number of " is incorrect */
858             {
859                 return NULL;
860             }
861             break;
862         }
863         case '\'':
864         {
865             s_sent++;
866
867             while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
868             {
869                 if( *s_sent == '\"' )
870                 {
871                     s_sent = Find_end_MRL( s_sent );
872
873                     if( s_sent == NULL )
874                     {
875                         return NULL;
876                     }
877                 } else
878                 {
879                     s_sent++;
880                 }
881             }
882
883             if( *s_sent == '\'' )
884             {
885                 s_sent++;
886                 return s_sent;
887             } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
888             {
889                 return NULL;
890             }
891             break;
892         }
893         default: /* now we can look for spaces */
894         {
895             while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
896             {
897                 if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
898                 {
899                     s_sent = Find_end_MRL( s_sent );
900                 } else
901                 {
902                     s_sent++;
903                 }
904             }
905             return s_sent;
906         }
907     }
908 }
909 /**********************************************************************
910  * parse_MRL: parse the MRL, find the mrl string and the options,
911  * create an item with all information in it, and return the item.
912  * return NULL if there is an error.
913  **********************************************************************/
914 playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
915                                    char *psz_name )
916 {
917     char **ppsz_options = NULL;
918     char *mrl;
919     char *s_mrl = psz;
920     int i_error = 0;
921     char *s_temp;
922     int i = 0;
923     int i_options = 0;
924     playlist_item_t * p_item = NULL;
925
926     /* In case there is spaces before the mrl */
927     while( *s_mrl == ' ' )
928         s_mrl++;
929
930     /* extract the mrl */
931     s_temp = strstr( s_mrl , " :" );
932     if( s_temp == NULL )
933     {
934         s_temp = s_mrl + strlen( s_mrl );
935     } else
936     {
937         while( (*s_temp == ' ') && (s_temp != s_mrl ) )
938         {
939             s_temp--;
940         }
941         s_temp++;
942     }
943
944     /* if the mrl is between " or ', we must remove them */
945     if( (*s_mrl == '\'') || (*s_mrl == '\"') )
946     {
947         mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
948         strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
949         mrl[ s_temp - s_mrl - 2 ] = '\0';
950     } else
951     {
952         mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
953         strncpy( mrl , s_mrl , s_temp - s_mrl );
954         mrl[ s_temp - s_mrl ] = '\0';
955     }
956
957     s_mrl = s_temp;
958
959     /* now we can take care of the options */
960     while( (*s_mrl != '\0') && (i_error == 0) )
961     {
962         switch( *s_mrl )
963         {
964             case ' ':
965             {
966                 s_mrl++;
967                 break;
968             }
969             case ':': /* an option */
970             {
971                 s_temp = Find_end_MRL( s_mrl );
972
973                 if( s_temp == NULL )
974                 {
975                     i_error = 1;
976                 }
977                 else
978                 {
979                     i_options++;
980                     ppsz_options = realloc( ppsz_options , i_options *
981                                             sizeof(char *) );
982                     ppsz_options[ i_options - 1 ] =
983                         malloc( (s_temp - s_mrl + 1) * sizeof(char) );
984
985                     strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
986                              s_temp - s_mrl );
987
988                     /* don't forget to finish the string with a '\0' */
989                     (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
990
991                     s_mrl = s_temp;
992                 }
993                 break;
994             }
995             default:
996             {
997                 i_error = 1;
998                 break;
999             }
1000         }
1001     }
1002
1003     if( i_error != 0 )
1004     {
1005         free( mrl );
1006     }
1007     else
1008     {
1009         /* now create an item */
1010         p_item = playlist_ItemNew( p_intf, mrl, psz_name );
1011         for( i = 0 ; i< i_options ; i++ )
1012         {
1013             playlist_ItemAddOption( p_item, ppsz_options[i] );
1014         }
1015     }
1016
1017     for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
1018     if( i_options ) free( ppsz_options );
1019
1020     return p_item;
1021 }
1022 /**********************************************************************
1023  * RealPath: parse ../, ~ and path stuff
1024  **********************************************************************/
1025 char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
1026 {
1027     char *psz_dir;
1028     char *p;
1029     int i_len = strlen(psz_src);
1030     char sep;
1031
1032 #if defined( WIN32 )
1033     sep = '\\';
1034 #else
1035     sep = '/';
1036 #endif
1037
1038     psz_dir = malloc( i_len + 2 );
1039     strcpy( psz_dir, psz_src );
1040
1041     /* Add a trailing sep to ease the .. step */
1042     psz_dir[i_len] = sep;
1043     psz_dir[i_len + 1] = '\0';
1044
1045 #ifdef WIN32
1046     /* Convert all / to native separator */
1047     p = psz_dir;
1048     while( (p = strchr( p, '/' )) != NULL )
1049     {
1050         *p = sep;
1051     }
1052 #endif
1053
1054     /* Remove multiple separators and /./ */
1055     p = psz_dir;
1056     while( (p = strchr( p, sep )) != NULL )
1057     {
1058         if( p[1] == sep )
1059             memmove( &p[1], &p[2], strlen(&p[2]) + 1 );
1060         else if( p[1] == '.' && p[2] == sep )
1061             memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
1062         else
1063             p++;
1064     }
1065
1066     if( psz_dir[0] == '~' )
1067     {
1068         char *dir = malloc( strlen(psz_dir)
1069                              + strlen(p_intf->p_vlc->psz_userdir) );
1070         /* This is incomplete : we should also support the ~cmassiot/ syntax. */
1071         sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
1072         free( psz_dir );
1073         psz_dir = dir;
1074     }
1075
1076     if( strlen(psz_dir) > 2 )
1077     {
1078         /* Fix all .. dir */
1079         p = psz_dir + 3;
1080         while( (p = strchr( p, sep )) != NULL )
1081         {
1082             if( p[-1] == '.' && p[-2] == '.' && p[-3] == sep )
1083             {
1084                 char *q;
1085                 p[-3] = '\0';
1086                 if( (q = strrchr( psz_dir, sep )) != NULL )
1087                 {
1088                     memmove( q + 1, p + 1, strlen(p + 1) + 1 );
1089                     p = q + 1;
1090                 }
1091                 else
1092                 {
1093                     memmove( psz_dir, p, strlen(p) + 1 );
1094                     p = psz_dir + 3;
1095                 }
1096             }
1097             else
1098                 p++;
1099         }
1100     }
1101
1102     /* Remove trailing sep if there are at least 2 sep in the string
1103      * (handles the C:\ stuff) */
1104     p = strrchr( psz_dir, sep );
1105     if( p != NULL && p[1] == '\0' && p != strchr( psz_dir, sep ) )
1106         *p = '\0';
1107
1108     return psz_dir;
1109 }