]> git.sesse.net Git - vlc/blobdiff - modules/demux/m3u.c
* Stringreview !!!
[vlc] / modules / demux / m3u.c
index 2ab43cd2b179fe1a9a986bd639464ac4c9a74ce4..58b914e9df08c52a1144fa8ceb4404d8f68ec55f 100644 (file)
@@ -1,17 +1,18 @@
 /*****************************************************************************
- * m3u.c: a meta demux to parse pls, m3u and asx playlists
+ * m3u.c: a meta demux to parse pls, m3u, asx et b4s playlists
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: m3u.c,v 1.16 2003/03/22 14:35:03 gbazin Exp $
+ * Copyright (C) 2001-2004 VideoLAN
+ * $Id: m3u.c,v 1.26 2004/01/25 20:05:28 hartman Exp $
  *
  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
  *          Gildas Bazin <gbazin@netcourrier.com>
+ *          Clément Stenac <zorglub@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * Preamble
  *****************************************************************************/
 #include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>                                              /* strdup() */
-#include <errno.h>
 
 #include <vlc/vlc.h>
 #include <vlc/input.h>
 #include <vlc_playlist.h>
 
-#include <sys/types.h>
-
 /*****************************************************************************
  * Constants and structures
  *****************************************************************************/
@@ -45,6 +42,7 @@
 #define TYPE_ASX 2
 #define TYPE_HTML 3
 #define TYPE_PLS 4
+#define TYPE_B4S 5
 
 struct demux_sys_t
 {
@@ -62,13 +60,14 @@ static int  Demux ( input_thread_t * );
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("playlist metademux") );
-    set_capability( "demux", 10 );
+    set_description( _("Playlist metademux") );
+    set_capability( "demux", 180 );
     set_callbacks( Activate, Deactivate );
     add_shortcut( "m3u" );
     add_shortcut( "asx" );
     add_shortcut( "html" );
     add_shortcut( "pls" );
+    add_shortcut( "b4s" );
 vlc_module_end();
 
 /*****************************************************************************
@@ -78,8 +77,8 @@ static int Activate( vlc_object_t * p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
     char           *psz_ext;
-    demux_sys_t    *p_m3u;
-    int            i_type = 0;
+    int             i_type  = TYPE_UNKNOWN;
+    int             i_type2 = TYPE_UNKNOWN;
 
     /* Initialize access plug-in structures. */
     if( p_input->i_mtu == 0 )
@@ -114,12 +113,18 @@ static int Activate( vlc_object_t * p_this )
     {
         i_type = TYPE_PLS;
     }
+    else if( ( psz_ext && !strcasecmp( psz_ext, ".b4s") ) ||
+             ( p_input->psz_demux && !strcmp(p_input->psz_demux, "b4s") ) )
+    {
+        i_type = TYPE_B4S;
+    }
 
     /* we had no luck looking at the file extention, so we have a look
      * at the content. This is useful for .asp, .php and similar files
-     * that are actually html. Also useful for som asx files that have
-     * another extention */
-    if( !i_type )
+     * that are actually html. Also useful for some asx files that have
+     * another extension */
+    /* XXX we double check for file != m3u as some asx ... are just m3u file */
+    if( i_type != TYPE_M3U )
     {
         byte_t *p_peek;
         int i_size = input_Peek( p_input, &p_peek, MAX_LINE );
@@ -128,41 +133,52 @@ static int Activate( vlc_object_t * p_this )
             while ( i_size
                     && strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") - 1 )
                     && strncasecmp( p_peek, "<html>", sizeof("<html>") - 1 )
-                    && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 ) )
+                    && strncasecmp( p_peek, "<asx", sizeof("<asx") - 1 )
+                    && strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
             {
                 p_peek++;
                 i_size--;
             }
             if ( !i_size )
             {
-                return -1;
+                ;
             }
             else if ( !strncasecmp( p_peek, "[playlist]", sizeof("[playlist]") -1 ) )
             {
-                i_type = TYPE_PLS;
+                i_type2 = TYPE_PLS;
             }
             else if ( !strncasecmp( p_peek, "<html>", sizeof("<html>") -1 ) )
             {
-                i_type = TYPE_HTML;
+                i_type2 = TYPE_HTML;
             }
             else if ( !strncasecmp( p_peek, "<asx", sizeof("<asx") -1 ) )
             {
-                i_type = TYPE_ASX;
+                i_type2 = TYPE_ASX;
+            }
+            else if ( !strncasecmp( p_peek, "<?xml", sizeof("<?xml") -1 ) )
+            {
+                i_type2 = TYPE_B4S;
             }
         }
     }
-
-    /* Allocate p_m3u */
-    if( !( p_m3u = malloc( sizeof( demux_sys_t ) ) ) )
+    if ( i_type == TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN)
     {
-        msg_Err( p_input, "out of memory" );
-        return -1;
+        return VLC_EGENERIC;
+    }
+    if ( i_type  != TYPE_UNKNOWN && i_type2 == TYPE_UNKNOWN )
+    {
+        i_type = TYPE_M3U;
+    }
+    else
+    {
+        i_type = i_type2;
     }
-    p_input->p_demux_data = p_m3u;
 
-    p_m3u->i_type = i_type;
+    /* Allocate p_m3u */
+    p_input->p_demux_data = malloc( sizeof( demux_sys_t ) );
+    p_input->p_demux_data->i_type = i_type;
 
-    return 0;
+    return VLC_SUCCESS;
 }
 
 /*****************************************************************************
@@ -171,32 +187,91 @@ static int Activate( vlc_object_t * p_this )
 static void Deactivate( vlc_object_t *p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
-    demux_sys_t *p_m3u = (demux_sys_t *)p_input->p_demux_data  ; 
 
-    free( p_m3u );
+    free( p_input->p_demux_data );
 }
 
 /*****************************************************************************
- * ProcessLine: read a "line" from the file and add any entries found
- * to the playlist. Return number of items added ( 0 or 1 )
- *****************************************************************************/
-static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
-        , playlist_t *p_playlist , char psz_line[MAX_LINE], int i_position )
+ * XMLSpecialChars: Handle the special chars in a XML file.
+ * ***************************************************************************/
+static void XMLSpecialChars ( char *str )
+{
+    char *src = str;
+    char *dst = str;
+
+    while( *src )
+    {
+        if( *src == '&' )
+        {
+            if( !strncasecmp( src, "&#xe0;", 6 ) ) *dst++ = 'à';
+            else if( !strncasecmp( src, "&#xee;", 6 ) ) *dst++ = 'î';
+            else if( !strncasecmp( src, "&apos;", 6 ) ) *dst++ = '\'';
+            else if( !strncasecmp( src, "&#xe8;", 6 ) ) *dst++ = 'è';
+            else if( !strncasecmp( src, "&#xe9;", 6 ) ) *dst++ = 'é';
+            else if( !strncasecmp( src, "&#xea;", 6 ) ) *dst++ = 'ê';
+            else
+            {
+                *dst++ = '?';
+            }
+            src += 6;
+        }
+        else
+        {
+            *dst++ = *src++;
+        }
+    }
+
+    *dst = '\0';
+}
+
+
+/*****************************************************************************
+ * ParseLine: read a "line" from the file and add any entries found
+ * to the playlist. Returns:
+ * 0 if nothing was found
+ * 1 if a URI was found (it is then copied in psz_data)
+ * 2 if a name was found (  "  )
+ *
+ * XXX psz_data has the same length that psz_line so no problem if you don't
+ * expand it
+ *    psz_line is \0 terminated
+ ******************************************************************************/
+static int ParseLine ( input_thread_t *p_input, char *psz_line, char *psz_data, vlc_bool_t *pb_next )
 {
+    demux_sys_t   *p_m3u = p_input->p_demux_data;
+
     char          *psz_bol, *psz_name;
+
     psz_bol = psz_line;
 
+    *pb_next = VLC_FALSE;
+
     /* Remove unnecessary tabs or spaces at the beginning of line */
     while( *psz_bol == ' ' || *psz_bol == '\t' ||
            *psz_bol == '\n' || *psz_bol == '\r' )
+    {
         psz_bol++;
+    }
 
     if( p_m3u->i_type == TYPE_M3U )
     {
         /* Check for comment line */
         if( *psz_bol == '#' )
-            /*line is comment or extended info, ignored for now */
-            return 0;
+        {
+            while( *psz_bol &&
+                   strncasecmp( psz_bol, "EXTINF:", sizeof("EXTINF:") - 1 ) )
+               psz_bol++;
+            if( !*psz_bol ) return 0;
+
+            psz_bol = strchr( psz_bol, ',' );
+            if ( !psz_bol ) return 0;
+            psz_bol++;
+            /* From now, we have a name line */
+
+            strcpy( psz_data , psz_bol );
+            return 2;
+        }
+        /* If we don't have a comment, the line is directly the URI */
     }
     else if ( p_m3u->i_type == TYPE_PLS )
     {
@@ -236,6 +311,10 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
         while( *psz_bol &&
                strncasecmp( psz_bol, "mms://",
                             sizeof("mms://") - 1 ) &&
+               strncasecmp( psz_bol, "mmsu://",
+                            sizeof("mmsu://") - 1 ) &&
+               strncasecmp( psz_bol, "mmst://",
+                            sizeof("mmst://") - 1 ) &&
                strncasecmp( psz_bol, "http://",
                             sizeof("http://") - 1 ) &&
                strncasecmp( psz_bol, "file://",
@@ -283,6 +362,70 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
         *psz_eol = '\0';
 
     }
+    else if ( p_m3u->i_type == TYPE_B4S )
+    {
+
+        char *psz_eol;
+
+        msg_Dbg( p_input, "b4s line=%s", psz_line );
+        /* We are dealing with a B4S file from Winamp 3 */
+
+        /* First, search for name *
+         * <Name>Blabla</Name> */
+
+        if( strstr ( psz_bol, "<Name>" ) )
+        {
+            /* We have a name */
+            while ( *psz_bol &&
+                    strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
+                psz_bol++;
+
+            if( !*psz_bol ) return 0;
+
+            psz_bol = psz_bol + 5 ;
+            /* We are now at the beginning of the name */
+
+            if( !psz_bol ) return 0;
+
+
+            psz_eol = strchr(psz_bol, '<' );
+            if( !psz_eol) return 0;
+
+            *psz_eol='\0';
+
+            XMLSpecialChars( psz_bol );
+
+            strcpy( psz_data, psz_bol );
+            return 2;
+        }
+        else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
+        {
+            *pb_next = VLC_TRUE;
+            return 0;
+        }
+
+         /* We are looking for <entry Playstring="blabla"> */
+
+
+        while ( *psz_bol &&
+                strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
+            psz_bol++;
+
+        if( !*psz_bol ) return 0;
+
+        psz_bol = strchr( psz_bol, '=' );
+        if ( !psz_bol ) return 0;
+
+        psz_bol += 2;
+
+        psz_eol= strchr(psz_bol, '"');
+        if( !psz_eol ) return 0;
+
+        *psz_eol= '\0';
+
+        /* Handle the XML special characters */
+        XMLSpecialChars( psz_bol );
+    }
     else
     {
         msg_Warn( p_input, "unknown file type" );
@@ -365,13 +508,63 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
         psz_name = strdup( psz_bol );
     }
 
-    playlist_Add( p_playlist, psz_name,
-                  PLAYLIST_INSERT, i_position );
+    strcpy(psz_data, psz_name ) ;
 
     free( psz_name );
+
+    if( p_m3u->i_type != TYPE_B4S )
+    {
+       *pb_next = VLC_TRUE;
+    }
+
     return 1;
 }
 
+static void ProcessLine ( input_thread_t *p_input, playlist_t *p_playlist,
+                          char *psz_line,
+                          char **ppsz_uri, char **ppsz_name,
+                          int *pi_position )
+{
+    char          psz_data[MAX_LINE];
+    vlc_bool_t    b_next;
+
+    switch( ParseLine( p_input, psz_line, psz_data, &b_next ) )
+    {
+        case 1:
+            if( *ppsz_uri )
+            {
+                free( *ppsz_uri );
+            }
+            *ppsz_uri = strdup( psz_data );
+            break;
+        case 2:
+            if( *ppsz_name )
+            {
+                free( *ppsz_name );
+            }
+            *ppsz_name = strdup( psz_data );
+            break;
+        case 0:
+        default:
+            break;
+    }
+
+    if( b_next && *ppsz_uri )
+    {
+        playlist_Add( p_playlist, *ppsz_uri,
+                         *ppsz_name ? *ppsz_name : *ppsz_uri,
+                          PLAYLIST_INSERT, *pi_position );
+        (*pi_position)++;
+        if( *ppsz_name )
+        {
+            free( *ppsz_name );
+        }
+        free( *ppsz_uri );
+        *ppsz_name = NULL;
+        *ppsz_uri  = NULL;
+    }
+}
+
 /*****************************************************************************
  * Demux: reads and demuxes data packets
  *****************************************************************************
@@ -379,14 +572,20 @@ static int ProcessLine ( input_thread_t *p_input , demux_sys_t *p_m3u
  *****************************************************************************/
 static int Demux ( input_thread_t *p_input )
 {
+    demux_sys_t   *p_m3u = p_input->p_demux_data;
+
     data_packet_t *p_data;
-    char          *p_buf, psz_line[MAX_LINE], eol_tok;
+    char          psz_line[MAX_LINE];
+    char          *p_buf, eol_tok;
     int           i_size, i_bufpos, i_linepos = 0;
     playlist_t    *p_playlist;
-    int           i_position;
     vlc_bool_t    b_discard = VLC_FALSE;
 
-    demux_sys_t   *p_m3u = (demux_sys_t *)p_input->p_demux_data;
+
+    char          *psz_name = NULL;
+    char          *psz_uri  = NULL;
+
+    int           i_position;
 
     p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
                                                  FIND_ANYWHERE );
@@ -444,8 +643,9 @@ static int Demux ( input_thread_t *p_input )
 
             psz_line[i_linepos] = '\0';
             i_linepos = 0;
-            i_position += ProcessLine ( p_input, p_m3u , p_playlist ,
-                                        psz_line, i_position );
+
+            ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
+                         &i_position );
         }
 
         input_DeletePacket( p_input->p_method_data, p_data );
@@ -454,9 +654,24 @@ static int Demux ( input_thread_t *p_input )
     if ( i_linepos && b_discard != VLC_TRUE && eol_tok == '\n' )
     {
         psz_line[i_linepos] = '\0';
-        i_linepos = 0;
-        i_position += ProcessLine ( p_input, p_m3u , p_playlist , psz_line,
-                                    i_position );
+
+        ProcessLine( p_input, p_playlist, psz_line, &psz_uri, &psz_name,
+                     &i_position );
+        /* is there a pendding uri without b_next */
+        if( psz_uri )
+        {
+            playlist_Add( p_playlist, psz_uri, psz_uri,
+                          PLAYLIST_INSERT, i_position );
+        }
+    }
+
+    if( psz_uri )
+    {
+        free( psz_uri );
+    }
+    if( psz_name )
+    {
+        free( psz_name );
     }
 
     vlc_object_release( p_playlist );