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