]> git.sesse.net Git - vlc/blobdiff - modules/control/http/util.c
codecleanup: Replace input_Item by input_item.
[vlc] / modules / control / http / util.c
index 56ca0d5677b873aeec91a367b05269e384fb274d..54ba5c44b8e2dc4058a7bb5a0ca83ed0e8066818 100644 (file)
@@ -2,7 +2,7 @@
  * util.c : Utility functions for HTTP interface
  *****************************************************************************
  * Copyright (C) 2001-2005 the VideoLAN team
- * $Id: http.c 12225 2005-08-18 10:01:30Z massiot $
+ * $Id$
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  *          Laurent Aimar <fenrir@via.ecp.fr>
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 #include "http.h"
+#include "vlc_strings.h"
 
 /****************************************************************************
  * File and directory functions
  ****************************************************************************/
 
 /* ToUrl: create a good name for an url from filename */
-char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
+char *FileToUrl( char *name, bool *pb_index )
 {
     char *url, *p;
 
     url = p = malloc( strlen( name ) + 1 );
 
-    *pb_index = VLC_FALSE;
+    *pb_index = false;
     if( !url || !p )
     {
         return NULL;
@@ -71,14 +77,14 @@ char *E_(FileToUrl)( char *name, vlc_bool_t *pb_index )
         if( !strncmp( p, "/index.", 7 ) )
         {
             p[1] = '\0';
-            *pb_index = VLC_TRUE;
+            *pb_index = true;
         }
     }
     return url;
 }
 
 /* Load a file */
-int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
+int FileLoad( FILE *f, char **pp_data, int *pi_data )
 {
     int i_read;
 
@@ -100,16 +106,12 @@ int E_(FileLoad)( FILE *f, char **pp_data, int *pi_data )
 }
 
 /* Parse a directory and recursively add files */
-int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
+int ParseDirectory( intf_thread_t *p_intf, char *psz_root,
                         char *psz_dir )
 {
     intf_sys_t     *p_sys = p_intf->p_sys;
     char           dir[MAX_DIR_SIZE];
-#ifdef HAVE_SYS_STAT_H
-    struct stat   stat_info;
-#endif
     DIR           *p_dir;
-    struct dirent *p_dir_content;
     vlc_acl_t     *p_acl;
     FILE          *file;
 
@@ -126,30 +128,25 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
     sep = '/';
 #endif
 
-#ifdef HAVE_SYS_STAT_H
-    if( stat( psz_dir, &stat_info ) == -1 || !S_ISDIR( stat_info.st_mode ) )
-    {
-        return VLC_EGENERIC;
-    }
-#endif
-
-    if( ( p_dir = opendir( psz_dir ) ) == NULL )
+    if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
     {
-        msg_Err( p_intf, "cannot open dir (%s)", psz_dir );
+        if( errno != ENOENT && errno != ENOTDIR )
+            msg_Err( p_intf, "cannot open directory (%s)", psz_dir );
         return VLC_EGENERIC;
     }
 
     i_dirlen = strlen( psz_dir );
     if( i_dirlen + 10 > MAX_DIR_SIZE )
     {
-        msg_Warn( p_intf, "skipping too deep dir (%s)", psz_dir );
+        msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir );
+        closedir( p_dir );
         return 0;
     }
 
     msg_Dbg( p_intf, "dir=%s", psz_dir );
 
-    sprintf( dir, "%s%c.access", psz_dir, sep );
-    if( ( file = fopen( dir, "r" ) ) != NULL )
+    snprintf( dir, sizeof( dir ), "%s%c.access", psz_dir, sep );
+    if( ( file = utf8_fopen( dir, "r" ) ) != NULL )
     {
         char line[1024];
         int  i_size;
@@ -182,42 +179,49 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
         fclose( file );
     }
 
-    sprintf( dir, "%s%c.hosts", psz_dir, sep );
-    p_acl = ACL_Create( p_intf, VLC_FALSE );
+    snprintf( dir, sizeof( dir ), "%s%c.hosts", psz_dir, sep );
+    p_acl = ACL_Create( p_intf, false );
     if( ACL_LoadFile( p_acl, dir ) )
     {
         ACL_Destroy( p_acl );
+
+        struct stat st;
+        if( stat( dir, &st ) == 0 )
+        {
+            closedir( p_dir );
+            return VLC_EGENERIC;
+        }
         p_acl = NULL;
     }
 
     for( ;; )
     {
+        char *psz_filename;
         /* parse psz_src dir */
-        if( ( p_dir_content = readdir( p_dir ) ) == NULL )
+        if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL )
         {
             break;
         }
 
-        if( ( p_dir_content->d_name[0] == '.' )
-         || ( i_dirlen + strlen( p_dir_content->d_name ) > MAX_DIR_SIZE ) )
+        if( ( psz_filename[0] == '.' )
+         || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) )
+        {
+            free( psz_filename );
             continue;
+        }
+
+        snprintf( dir, sizeof( dir ), "%s%c%s", psz_dir, sep, psz_filename );
+        free( psz_filename );
 
-        sprintf( dir, "%s%c%s", psz_dir, sep, p_dir_content->d_name );
-        if( E_(ParseDirectory)( p_intf, psz_root, dir ) )
+        if( ParseDirectory( p_intf, psz_root, dir ) )
         {
             httpd_file_sys_t *f = NULL;
             httpd_handler_sys_t *h = NULL;
-            vlc_bool_t b_index;
-            char *psz_tmp, *psz_file, *psz_name, *psz_ext;
-
-            psz_tmp = vlc_fix_readdir_charset( p_intf, dir );
-            psz_file = E_(FromUTF8)( p_intf, psz_tmp );
-            free( psz_tmp );
-            psz_tmp = vlc_fix_readdir_charset( p_intf,
-                                               &dir[strlen( psz_root )] );
-            psz_name = E_(FileToUrl)( psz_tmp, &b_index );
-            free( psz_tmp );
-            psz_ext = strrchr( psz_file, '.' );
+            bool b_index;
+            char *psz_name, *psz_ext;
+
+            psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index );
+            psz_ext = strrchr( dir, '.' );
             if( psz_ext != NULL )
             {
                 int i;
@@ -229,23 +233,23 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
                 {
                     f = malloc( sizeof( httpd_handler_sys_t ) );
                     h = (httpd_handler_sys_t *)f;
-                    f->b_handler = VLC_TRUE;
+                    f->b_handler = true;
                     h->p_association = p_sys->pp_handlers[i];
                 }
             }
             if( f == NULL )
             {
                 f = malloc( sizeof( httpd_file_sys_t ) );
-                f->b_handler = VLC_FALSE;
+                f->b_handler = false;
             }
 
             f->p_intf  = p_intf;
             f->p_file = NULL;
             f->p_redir = NULL;
             f->p_redir2 = NULL;
-            f->file = psz_file;
+            f->file = strdup (dir);
             f->name = psz_name;
-            f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) ? VLC_TRUE : VLC_FALSE;
+            f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false;
 
             if( !f->name )
             {
@@ -259,12 +263,25 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
 
             if( !f->b_handler )
             {
+                char *psz_type = strdup( "text/html; charset=UTF-8" );
+                if( strstr( &dir[strlen( psz_root )], ".xml" ) )
+                {
+                    char *psz = strstr( psz_type, "html;" );
+                    if( psz )
+                    {
+                        psz[0] = 'x';
+                        psz[1] = 'm';
+                        psz[2] = 'l';
+                        psz[3] = ';';
+                        psz[4] = ' ';
+                    }
+                }
                 f->p_file = httpd_FileNew( p_sys->p_httpd_host,
                                            f->name,
-                                           f->b_html ? p_sys->psz_html_type :
-                                            NULL,
+                                           f->b_html ? psz_type : NULL,
                                            user, password, p_acl,
-                                           E_(HttpCallback), f );
+                                           HttpCallback, f );
+                free( psz_type );
                 if( f->p_file != NULL )
                 {
                     TAB_APPEND( p_sys->i_files, p_sys->pp_files, f );
@@ -275,7 +292,7 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
                 h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host,
                                                  f->name,
                                                  user, password, p_acl,
-                                                 E_(HandlerCallback), h );
+                                                 HandlerCallback, h );
                 if( h->p_handler != NULL )
                 {
                     TAB_APPEND( p_sys->i_files, p_sys->pp_files,
@@ -298,26 +315,21 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
 
                 if( b_index && ( p = strstr( f->file, "index." ) ) )
                 {
-                    asprintf( &psz_redir, "%s%s", f->name, p );
-
-                    msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
-                    f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
-                                                     f->name, psz_redir );
+                    if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 )
+                    {
+                        msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name );
+                        f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host,
+                                                         f->name, psz_redir );
 
-                    free( psz_redir );
+                        free( psz_redir );
+                    }
                 }
             }
         }
     }
 
-    if( user )
-    {
-        free( user );
-    }
-    if( password )
-    {
-        free( password );
-    }
+    free( user );
+    free( password );
 
     ACL_Destroy( p_acl );
     closedir( p_dir );
@@ -326,95 +338,10 @@ int E_(ParseDirectory)( intf_thread_t *p_intf, char *psz_root,
 }
 
 
-/**************************************************************************
- * Locale functions
- **************************************************************************/
-char *E_(FromUTF8)( intf_thread_t *p_intf, char *psz_utf8 )
-{
-    intf_sys_t    *p_sys = p_intf->p_sys;
-
-    if ( p_sys->iconv_from_utf8 != (vlc_iconv_t)-1 )
-    {
-        size_t i_in = strlen(psz_utf8);
-        size_t i_out = i_in * 2;
-        char *psz_local = malloc(i_out + 1);
-        char *psz_out = psz_local;
-        size_t i_ret;
-        char psz_tmp[i_in + 1];
-        char *psz_in = psz_tmp;
-        uint8_t *p = (uint8_t *)psz_tmp;
-        strcpy( psz_tmp, psz_utf8 );
-
-        /* Fix Unicode quotes. If we are here we are probably converting
-         * to an inferior charset not understanding Unicode quotes. */
-        while( *p )
-        {
-            if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x99 )
-            {
-                *p = '\'';
-                memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
-            }
-            if( p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x9a )
-            {
-                *p = '"';
-                memmove( &p[1], &p[3], strlen(&p[3]) + 1 );
-            }
-            p++;
-        }
-        i_in = strlen( psz_tmp );
-
-        i_ret = vlc_iconv( p_sys->iconv_from_utf8, &psz_in, &i_in,
-                           &psz_out, &i_out );
-        if( i_ret == (size_t)-1 || i_in )
-        {
-            msg_Warn( p_intf,
-                      "failed to convert \"%s\" to desired charset (%s)",
-                      psz_utf8, strerror(errno) );
-            free( psz_local );
-            return strdup( psz_utf8 );
-        }
-
-        *psz_out = '\0';
-        return psz_local;
-    }
-    else
-        return strdup( psz_utf8 );
-}
-
-char *E_(ToUTF8)( intf_thread_t *p_intf, char *psz_local )
-{
-    intf_sys_t    *p_sys = p_intf->p_sys;
-
-    if ( p_sys->iconv_to_utf8 != (vlc_iconv_t)-1 )
-    {
-        char *psz_in = psz_local;
-        size_t i_in = strlen(psz_in);
-        size_t i_out = i_in * 6;
-        char *psz_utf8 = malloc(i_out + 1);
-        char *psz_out = psz_utf8;
-
-        size_t i_ret = vlc_iconv( p_sys->iconv_to_utf8, &psz_in, &i_in,
-                                  &psz_out, &i_out );
-        if( i_ret == (size_t)-1 || i_in )
-        {
-            msg_Warn( p_intf,
-                      "failed to convert \"%s\" to desired charset (%s)",
-                      psz_local, strerror(errno) );
-            free( psz_utf8 );
-            return strdup( psz_local );
-        }
-
-        *psz_out = '\0';
-        return psz_utf8;
-    }
-    else
-        return strdup( psz_local );
-}
-
 /*************************************************************************
  * Playlist stuff
  *************************************************************************/
-void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
+void PlaylistListNode( intf_thread_t *p_intf, playlist_t *p_pl,
                            playlist_item_t *p_node, char *name, mvar_t *s,
                            int i_depth )
 {
@@ -424,58 +351,85 @@ void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
         {
             char value[512];
             char *psz;
-            mvar_t *itm = E_(mvar_New)( name, "set" );
+            mvar_t *itm = mvar_New( name, "set" );
 
-            sprintf( value, "%d", ( p_pl->status.p_item == p_node )? 1 : 0 );
-            E_(mvar_AppendNewVar)( itm, "current", value );
+            if( p_pl->status.p_item && p_node &&
+                p_pl->status.p_item->p_input && p_node->p_input &&
+                p_pl->status.p_item->p_input->i_id == p_node->p_input->i_id )
+            {
+                mvar_AppendNewVar( itm, "current", "1" );
+            }
+            else
+            {
+                mvar_AppendNewVar( itm, "current", "0" );
+            }
 
-            sprintf( value, "%d", p_node->input.i_id );
-            E_(mvar_AppendNewVar)( itm, "index", value );
+            sprintf( value, "%d", p_node->i_id );
+            mvar_AppendNewVar( itm, "index", value );
 
-            psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
-            E_(mvar_AppendNewVar)( itm, "name", psz );
+            psz = input_item_GetName( p_node->p_input );
+            mvar_AppendNewVar( itm, "name", psz );
             free( psz );
 
-            psz = E_(FromUTF8)( p_intf, p_node->input.psz_uri );
-            E_(mvar_AppendNewVar)( itm, "uri", psz );
+            psz = input_item_GetURI( p_node->p_input );
+            mvar_AppendNewVar( itm, "uri", psz );
             free( psz );
 
             sprintf( value, "Item");
-            E_(mvar_AppendNewVar)( itm, "type", value );
+            mvar_AppendNewVar( itm, "type", value );
 
             sprintf( value, "%d", i_depth );
-            E_(mvar_AppendNewVar)( itm, "depth", value );
+            mvar_AppendNewVar( itm, "depth", value );
+
+            if( p_node->i_flags & PLAYLIST_RO_FLAG )
+            {
+                mvar_AppendNewVar( itm, "ro", "ro" );
+            }
+            else
+            {
+                mvar_AppendNewVar( itm, "ro", "rw" );
+            }
+
+            sprintf( value, "%ld",
+                    (long) input_item_GetDuration( p_node->p_input ) );
+            mvar_AppendNewVar( itm, "duration", value );
 
-            E_(mvar_AppendVar)( s, itm );
+            mvar_AppendVar( s, itm );
         }
         else
         {
             char value[512];
-            char *psz;
             int i_child;
-            mvar_t *itm = E_(mvar_New)( name, "set" );
+            mvar_t *itm = mvar_New( name, "set" );
 
-            psz = E_(FromUTF8)( p_intf, p_node->input.psz_name );
-            E_(mvar_AppendNewVar)( itm, "name", psz );
-            E_(mvar_AppendNewVar)( itm, "uri", psz );
-            free( psz );
+            mvar_AppendNewVar( itm, "name", p_node->p_input->psz_name );
+            mvar_AppendNewVar( itm, "uri", p_node->p_input->psz_name );
 
             sprintf( value, "Node" );
-            E_(mvar_AppendNewVar)( itm, "type", value );
+            mvar_AppendNewVar( itm, "type", value );
 
-            sprintf( value, "%d", p_node->input.i_id );
-            E_(mvar_AppendNewVar)( itm, "index", value );
+            sprintf( value, "%d", p_node->i_id );
+            mvar_AppendNewVar( itm, "index", value );
 
             sprintf( value, "%d", p_node->i_children);
-            E_(mvar_AppendNewVar)( itm, "i_children", value );
+            mvar_AppendNewVar( itm, "i_children", value );
 
             sprintf( value, "%d", i_depth );
-            E_(mvar_AppendNewVar)( itm, "depth", value );
+            mvar_AppendNewVar( itm, "depth", value );
 
-            E_(mvar_AppendVar)( s, itm );
+            if( p_node->i_flags & PLAYLIST_RO_FLAG )
+            {
+                mvar_AppendNewVar( itm, "ro", "ro" );
+            }
+            else
+            {
+                mvar_AppendNewVar( itm, "ro", "rw" );
+            }
+
+            mvar_AppendVar( s, itm );
 
             for (i_child = 0 ; i_child < p_node->i_children ; i_child++)
-                E_(PlaylistListNode)( p_intf, p_pl,
+                PlaylistListNode( p_intf, p_pl,
                                       p_node->pp_children[i_child],
                                       name, s, i_depth + 1);
 
@@ -486,7 +440,7 @@ void E_(PlaylistListNode)( intf_thread_t *p_intf, playlist_t *p_pl,
 /****************************************************************************
  * Seek command parsing handling
  ****************************************************************************/
-void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
+void HandleSeek( intf_thread_t *p_intf, char *p_value )
 {
     intf_sys_t     *p_sys = p_intf->p_sys;
     vlc_value_t val;
@@ -546,8 +500,10 @@ void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
                 {
                     i_value += 3600 * i_stock;
                     i_stock = 0;
-                    /* other characters which are not numbers are not important */
-                    while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
+                    /* other characters which are not numbers are not
+                     * important */
+                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
+                           && (p_value[0] != '\0') )
                     {
                         p_value++;
                     }
@@ -558,7 +514,8 @@ void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
                     i_value += 60 * i_stock;
                     i_stock = 0;
                     p_value++;
-                    while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
+                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
+                           && (p_value[0] != '\0') )
                     {
                         p_value++;
                     }
@@ -568,7 +525,8 @@ void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
                 {
                     i_value += i_stock;
                     i_stock = 0;
-                    while( ((p_value[0] < '0') || (p_value[0] > '9')) && (p_value[0] != '\0') )
+                    while( ((p_value[0] < '0') || (p_value[0] > '9'))
+                           && (p_value[0] != '\0') )
                     {
                         p_value++;
                     }
@@ -582,7 +540,8 @@ void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
             }
         }
 
-        /* if there is no known symbol, I consider it as seconds. Otherwise, i_stock = 0 */
+        /* if there is no known symbol, I consider it as seconds.
+         * Otherwise, i_stock = 0 */
         i_value += i_stock;
 
         switch(i_relative)
@@ -628,30 +587,35 @@ void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
             }
             case POSITION_ABSOLUTE:
             {
-                val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
+                val.f_float = __MIN( __MAX( ((float) i_value ) / 100.0 ,
+                                            0.0 ), 100.0 );
                 var_Set( p_sys->p_input, "position", val );
-                msg_Dbg( p_intf, "requested seek percent: %d", i_value );
+                msg_Dbg( p_intf, "requested seek percent: %d%%", i_value );
                 break;
             }
             case POSITION_REL_FOR:
             {
                 var_Get( p_sys->p_input, "position", &val );
-                val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
+                val.f_float += __MIN( __MAX( ((float) i_value ) / 100.0,
+                                             0.0 ) , 100.0 );
                 var_Set( p_sys->p_input, "position", val );
-                msg_Dbg( p_intf, "requested seek percent forward: %d", i_value );
+                msg_Dbg( p_intf, "requested seek percent forward: %d%%",
+                         i_value );
                 break;
             }
             case POSITION_REL_BACK:
             {
                 var_Get( p_sys->p_input, "position", &val );
-                val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0 , 0.0 ) , 100.0 );
+                val.f_float -= __MIN( __MAX( ((float) i_value ) / 100.0,
+                                             0.0 ) , 100.0 );
                 var_Set( p_sys->p_input, "position", val );
-                msg_Dbg( p_intf, "requested seek percent backward: %d", i_value );
+                msg_Dbg( p_intf, "requested seek percent backward: %d%%",
+                         i_value );
                 break;
             }
             default:
             {
-                msg_Dbg( p_intf, "requested seek: what the f*** is going on here ?" );
+                msg_Dbg( p_intf, "invalid seek request" );
                 break;
             }
         }
@@ -668,7 +632,7 @@ void E_(HandleSeek)( intf_thread_t *p_intf, char *p_value )
 /****************************************************************************
  * URI Parsing functions
  ****************************************************************************/
-int E_(TestURIParam)( char *psz_uri, const char *psz_name )
+int TestURIParam( char *psz_uri, const char *psz_name )
 {
     char *p = psz_uri;
 
@@ -678,17 +642,19 @@ int E_(TestURIParam)( char *psz_uri, const char *psz_name )
         if( (p == psz_uri || *(p - 1) == '&' || *(p - 1) == '\n')
               && p[strlen(psz_name)] == '=' )
         {
-            return VLC_TRUE;
+            return true;
         }
         p++;
     }
 
-    return VLC_FALSE;
+    return false;
 }
-char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
-                             char *psz_value, int i_value_max )
+
+static char *FindURIValue( char *psz_uri, const char *restrict psz_name,
+                           size_t *restrict p_len )
 {
-    char *p = psz_uri;
+    char *p = psz_uri, *end;
+    size_t len;
 
     while( (p = strstr( p, psz_name )) )
     {
@@ -699,92 +665,89 @@ char *E_(ExtractURIValue)( char *psz_uri, const char *psz_name,
         p++;
     }
 
-    if( p )
+    if( p == NULL )
     {
-        int i_len;
+        *p_len = 0;
+        return NULL;
+    }
 
-        p += strlen( psz_name );
-        if( *p == '=' ) p++;
+    p += strlen( psz_name );
+    if( *p == '=' ) p++;
 
-        if( strchr( p, '&' ) )
-        {
-            i_len = strchr( p, '&' ) - p;
-        }
-        else
-        {
-            /* for POST method */
-            if( strchr( p, '\n' ) )
-            {
-                i_len = strchr( p, '\n' ) - p;
-                if( i_len && *(p+i_len-1) == '\r' ) i_len--;
-            }
-            else
-            {
-                i_len = strlen( p );
-            }
-        }
-        i_len = __MIN( i_value_max - 1, i_len );
-        if( i_len > 0 )
-        {
-            strncpy( psz_value, p, i_len );
-            psz_value[i_len] = '\0';
-        }
-        else
-        {
-            strncpy( psz_value, "", i_value_max );
-        }
-        p += i_len;
+    if( ( end = strchr( p, '\n' ) ) != NULL )
+    {
+        /* POST method */
+        if( ( end > p ) && ( end[-1] == '\r' ) )
+            end--;
+
+        len = end - p;
     }
     else
     {
-        strncpy( psz_value, "", i_value_max );
+        /* GET method */
+        if( ( end = strchr( p, '&' ) ) != NULL )
+            len = end - p;
+        else
+            len = strlen( p );
     }
 
+    *p_len = len;
     return p;
 }
 
-void E_(DecodeEncodedURI)( char *psz )
+char *ExtractURIValue( char *restrict psz_uri,
+                           const char *restrict psz_name,
+                           char *restrict psz_buf, size_t bufsize )
 {
-    char *dup = strdup( psz );
-    char *p = dup;
+    size_t len;
+    char *psz_value = FindURIValue( psz_uri, psz_name, &len );
+    char *psz_next;
 
-    while( *p )
+    if( psz_value == NULL )
     {
-        if( *p == '%' )
-        {
-            char val[3];
-            p++;
-            if( !*p )
-            {
-                break;
-            }
+        if( bufsize > 0 )
+            *psz_buf = '\0';
+        return NULL;
+    }
 
-            val[0] = *p++;
-            val[1] = *p++;
-            val[2] = '\0';
+    psz_next = psz_value + len;
 
-            *psz++ = strtol( val, NULL, 16 );
-        }
-        else if( *p == '+' )
-        {
-            *psz++ = ' ';
-            p++;
-        }
-        else
-        {
-            *psz++ = *p++;
-        }
-    }
-    *psz++ = '\0';
-    free( dup );
+    if( len >= bufsize )
+        len = bufsize - 1;
+
+    if( len > 0 )
+        strncpy( psz_buf, psz_value, len );
+    if( bufsize > 0 )
+        psz_buf[len] = '\0';
+
+    return psz_next;
+}
+
+char *ExtractURIString( char *restrict psz_uri,
+                            const char *restrict psz_name )
+{
+    size_t len;
+    char *psz_value = FindURIValue( psz_uri, psz_name, &len );
+
+    if( psz_value == NULL )
+        return NULL;
+
+    char *res = malloc( len + 1 );
+    if( res == NULL )
+        return NULL;
+
+    memcpy( res, psz_value, len );
+    res[len] = '\0';
+
+    return res;
 }
 
 /* Since the resulting string is smaller we can work in place, so it is
  * permitted to have psz == new. new points to the first word of the
  * string, the function returns the remaining string. */
-char *E_(FirstWord)( char *psz, char *new )
+char *FirstWord( char *psz, char *new )
 {
-    vlc_bool_t b_end;
+    bool b_end;
 
     while( *psz == ' ' )
         psz++;
@@ -818,225 +781,103 @@ char *E_(FirstWord)( char *psz, char *new )
     else
         return NULL;
 }
-/**********************************************************************
- * Find_end_MRL: Find the end of the sentence :
- * this function parses the string psz and find the end of the item
- * and/or option with detecting the " and ' problems.
- * returns NULL if an error is detected, otherwise, returns a pointer
- * of the end of the sentence (after the last character)
-**********************************************************************/
-static char *Find_end_MRL( char *psz )
-{
-    char *s_sent = psz;
 
-    switch( *s_sent )
-    {
-        case '\"':
-        {
-            s_sent++;
+/**********************************************************************
+ * MRLParse: parse the MRL, find the MRL string and the options,
+ * create an item with all information in it, and return the item.
+ * return NULL if there is an error.
+ **********************************************************************/
 
-            while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
-            {
-                if( *s_sent == '\'' )
-                {
-                    s_sent = Find_end_MRL( s_sent );
+/* Function analog to FirstWord except that it relies on colon instead
+ * of space to delimit option boundaries. */
+static char *FirstOption( char *psz, char *new )
+{
+    bool b_end, b_start = true;
 
-                    if( s_sent == NULL )
-                    {
-                        return NULL;
-                    }
-                } else
-                {
-                    s_sent++;
-                }
-            }
+    while( *psz == ' ' )
+        psz++;
 
-            if( *s_sent == '\"' )
-            {
-                s_sent++;
-                return s_sent;
-            } else  /* *s_sent == '\0' , which means the number of " is incorrect */
-            {
-                return NULL;
-            }
-            break;
-        }
-        case '\'':
+    while( *psz != '\0' && (*psz != ' ' || psz[1] != ':') )
+    {
+        if( *psz == '\'' )
         {
-            s_sent++;
-
-            while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
-            {
-                if( *s_sent == '\"' )
-                {
-                    s_sent = Find_end_MRL( s_sent );
-
-                    if( s_sent == NULL )
-                    {
-                        return NULL;
-                    }
-                } else
-                {
-                    s_sent++;
-                }
-            }
-
-            if( *s_sent == '\'' )
-            {
-                s_sent++;
-                return s_sent;
-            } else  /* *s_sent == '\0' , which means the number of ' is incorrect */
+            char c = *psz++;
+            while( *psz != '\0' && *psz != c )
             {
-                return NULL;
+                if( *psz == '\\' && psz[1] != '\0' )
+                    psz++;
+                *new++ = *psz++;
+                b_start = false;
             }
-            break;
+            if( *psz == c )
+                psz++;
         }
-        default: /* now we can look for spaces */
+        else
         {
-            while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
-            {
-                if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
-                {
-                    s_sent = Find_end_MRL( s_sent );
-                } else
-                {
-                    s_sent++;
-                }
-            }
-            return s_sent;
+            if( *psz == '\\' && psz[1] != '\0' )
+                psz++;
+            *new++ = *psz++;
+            b_start = false;
         }
     }
+    b_end = !*psz;
+
+    if ( !b_start )
+        while (new[-1] == ' ')
+            new--;
+
+    *new++ = '\0';
+    if( !b_end )
+        return psz + 1;
+    else
+        return NULL;
 }
-/**********************************************************************
- * parse_MRL: parse the MRL, find the mrl string and the options,
- * create an item with all information in it, and return the item.
- * return NULL if there is an error.
- **********************************************************************/
-playlist_item_t *E_(MRLParse)( intf_thread_t *p_intf, char *psz,
+
+input_item_t *MRLParse( intf_thread_t *p_intf, char *_psz,
                                    char *psz_name )
 {
-    char **ppsz_options = NULL;
-    char *mrl;
+    char *psz = strdup( _psz );
     char *s_mrl = psz;
-    int i_error = 0;
     char *s_temp;
-    int i = 0;
-    int i_options = 0;
-    playlist_item_t * p_item = NULL;
-
-    /* In case there is spaces before the mrl */
-    while( ( *s_mrl == ' ' ) && ( *s_mrl != '\0' ) )
-    {
-        s_mrl++;
-    }
+    input_item_t * p_input = NULL;
 
     /* extract the mrl */
-    s_temp = strstr( s_mrl , " :" );
+    s_temp = FirstOption( s_mrl, s_mrl );
     if( s_temp == NULL )
     {
         s_temp = s_mrl + strlen( s_mrl );
-    } else
-    {
-        while( (*s_temp == ' ') && (s_temp != s_mrl ) )
-        {
-            s_temp--;
-        }
-        s_temp++;
-    }
-
-    /* if the mrl is between " or ', we must remove them */
-    if( (*s_mrl == '\'') || (*s_mrl == '\"') )
-    {
-        mrl = (char *)malloc( (s_temp - s_mrl - 1) * sizeof( char ) );
-        strncpy( mrl , (s_mrl + 1) , s_temp - s_mrl - 2 );
-        mrl[ s_temp - s_mrl - 2 ] = '\0';
-    } else
-    {
-        mrl = (char *)malloc( (s_temp - s_mrl + 1) * sizeof( char ) );
-        strncpy( mrl , s_mrl , s_temp - s_mrl );
-        mrl[ s_temp - s_mrl ] = '\0';
     }
 
+    p_input = input_item_New( p_intf, s_mrl, psz_name );
     s_mrl = s_temp;
 
     /* now we can take care of the options */
-    while( (*s_mrl != '\0') && (i_error == 0) )
+    while ( *s_mrl != '\0' )
     {
-        switch( *s_mrl )
-        {
-            case ' ':
-            {
-                s_mrl++;
-                break;
-            }
-            case ':': /* an option */
-            {
-                s_temp = Find_end_MRL( s_mrl );
-
-                if( s_temp == NULL )
-                {
-                    i_error = 1;
-                }
-                else
-                {
-                    i_options++;
-                    ppsz_options = realloc( ppsz_options , i_options *
-                                            sizeof(char *) );
-                    ppsz_options[ i_options - 1 ] =
-                        malloc( (s_temp - s_mrl + 1) * sizeof(char) );
-
-                    strncpy( ppsz_options[ i_options - 1 ] , s_mrl ,
-                             s_temp - s_mrl );
-
-                    /* don't forget to finish the string with a '\0' */
-                    (ppsz_options[ i_options - 1 ])[ s_temp - s_mrl ] = '\0';
-
-                    s_mrl = s_temp;
-                }
-                break;
-            }
-            default:
-            {
-                i_error = 1;
-                break;
-            }
-        }
-    }
-
-    if( i_error != 0 )
-    {
-        free( mrl );
-    }
-    else
-    {
-        /* now create an item */
-        p_item = playlist_ItemNew( p_intf, mrl, psz_name );
-        for( i = 0 ; i< i_options ; i++ )
+        s_temp = FirstOption( s_mrl, s_mrl );
+        if( s_mrl == '\0' )
+            break;
+        if( s_temp == NULL )
         {
-            playlist_ItemAddOption( p_item, ppsz_options[i] );
+            s_temp = s_mrl + strlen( s_mrl );
         }
+        input_item_AddOption( p_input, s_mrl );
+        s_mrl = s_temp;
     }
 
-    for( i = 0; i < i_options; i++ ) free( ppsz_options[i] );
-    if( i_options ) free( ppsz_options );
-
-    return p_item;
+    free( psz );
+    return p_input;
 }
+
 /**********************************************************************
  * RealPath: parse ../, ~ and path stuff
  **********************************************************************/
-char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
+char *RealPath( const char *psz_src )
 {
     char *psz_dir;
     char *p;
     int i_len = strlen(psz_src);
-    char sep;
-
-#if defined( WIN32 )
-    sep = '\\';
-#else
-    sep = '/';
-#endif
+    const char sep = DIR_SEP_CHAR;
 
     psz_dir = malloc( i_len + 2 );
     strcpy( psz_dir, psz_src );
@@ -1045,7 +886,7 @@ char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
     psz_dir[i_len] = sep;
     psz_dir[i_len + 1] = '\0';
 
-#ifdef WIN32
+#if (DIR_SEP_CHAR != '/')
     /* Convert all / to native separator */
     p = psz_dir;
     while( (p = strchr( p, '/' )) != NULL )
@@ -1054,6 +895,7 @@ char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
     }
 #endif
 
+    /* FIXME: this could be O(N) rather than O(N²)... */
     /* Remove multiple separators and /./ */
     p = psz_dir;
     while( (p = strchr( p, sep )) != NULL )
@@ -1068,10 +910,8 @@ char *E_(RealPath)( intf_thread_t *p_intf, const char *psz_src )
 
     if( psz_dir[0] == '~' )
     {
-        char *dir = malloc( strlen(psz_dir)
-                             + strlen(p_intf->p_vlc->psz_userdir) );
-        /* This is incomplete : we should also support the ~cmassiot/ syntax. */
-        sprintf( dir, "%s%s", p_intf->p_vlc->psz_userdir, psz_dir + 1 );
+        char *dir;
+        asprintf( &dir, "%s%s", config_GetHomeDir(), psz_dir + 1 );
         free( psz_dir );
         psz_dir = dir;
     }