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