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