]> git.sesse.net Git - vlc/blobdiff - modules/demux/playlist/m3u.c
m3u8: correctly deal with BOM (fixes #8859)
[vlc] / modules / demux / playlist / m3u.c
index 2ac9622900895af3b5974f27e1329a4762dd3cc4..b995895e00fb7c18fd3e78990150825c659c01f7 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * m3u.c : M3U playlist format import
  *****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  *          Sigmund Augdal Helberg <dnumgis@videolan.org>
  *
- * 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -46,15 +46,20 @@ struct demux_sys_t
  * Local prototypes
  *****************************************************************************/
 static int Demux( demux_t *p_demux);
-static int Control( demux_t *p_demux, int i_query, va_list args );
 static void parseEXTINF( char *psz_string, char **ppsz_artist, char **ppsz_name, int *pi_duration );
 static bool ContainsURL( demux_t *p_demux );
+static bool CheckContentType( stream_t * p_stream, const char * psz_ctype );
 
 static char *GuessEncoding (const char *str)
 {
     return IsUTF8 (str) ? strdup (str) : FromLatin1 (str);
 }
 
+static char *CheckUnicode (const char *str)
+{
+    return IsUTF8 (str) ? strdup (str): NULL;
+}
+
 /*****************************************************************************
  * Import_M3U: main import function
  *****************************************************************************/
@@ -65,16 +70,24 @@ int Import_M3U( vlc_object_t *p_this )
     CHECK_PEEK( p_peek, 8 );
     char *(*pf_dup) (const char *);
 
+    if( POKE( p_peek, "\xef\xbb\xbf", 3) )/* BOM at start */
+    {
+        pf_dup = CheckUnicode; /* UTF-8 */
+        stream_Seek( p_demux->s, 3 );
+    }
+    else
     if( POKE( p_peek, "RTSPtext", 8 ) /* QuickTime */
      || demux_IsPathExtension( p_demux, ".m3u8" )
-     || demux_IsForced( p_demux, "m3u8" ) )
-        pf_dup = strdup; /* UTF-8 */
+     || demux_IsForced( p_demux, "m3u8" )
+     || CheckContentType( p_demux->s, "application/vnd.apple.mpegurl" ) )
+        pf_dup = CheckUnicode; /* UTF-8 */
     else
     if( POKE( p_peek, "#EXTM3U", 7 )
      || demux_IsPathExtension( p_demux, ".m3u" )
      || demux_IsPathExtension( p_demux, ".vlc" )
      || demux_IsForced( p_demux, "m3u" )
-     || ContainsURL( p_demux ) )
+     || ContainsURL( p_demux )
+     || CheckContentType( p_demux->s, "audio/x-mpegurl") )
         pf_dup = GuessEncoding;
     else
         return VLC_EGENERIC;
@@ -97,7 +110,7 @@ static bool ContainsURL( demux_t *p_demux )
 
     while( p_peek + sizeof( "https://" ) < p_peek_end )
     {
-        /* One line starting with an URL is enough */
+        /* One line starting with a URL is enough */
         if( !strncasecmp( (const char *)p_peek, "http://", 7 ) ||
             !strncasecmp( (const char *)p_peek, "mms://", 6 ) ||
             !strncasecmp( (const char *)p_peek, "rtsp://", 7 ) ||
@@ -120,6 +133,24 @@ static bool ContainsURL( demux_t *p_demux )
     return false;
 }
 
+static bool CheckContentType( stream_t * p_stream, const char * psz_ctype )
+{
+    char *psz_check = stream_ContentType( p_stream );
+    if( !psz_check ) return false;
+
+    int i_len = strlen( psz_check );
+    if ( i_len == 0 )
+    {
+        free( psz_check );
+        return false;
+    }
+
+    int i_res = strncasecmp( psz_check, psz_ctype, i_len );
+    free( psz_check );
+
+    return ( i_res == 0 ) ? true : false;
+}
+
 /*****************************************************************************
  * Deactivate: frees unused data
  *****************************************************************************/
@@ -222,17 +253,17 @@ static int Demux( demux_t *p_demux )
             b_cleanup = true;
             if( !psz_mrl )
             {
-                LocaleFree( psz_parse );
+                free( psz_parse );
                 goto error;
             }
 
-            p_input = input_item_NewExt( p_demux, psz_mrl, psz_name,
+            p_input = input_item_NewExt( psz_mrl, psz_name,
                                         i_options, ppsz_options, 0, i_duration );
 
-            LocaleFree( psz_parse );
+            free( psz_parse );
             free( psz_mrl );
 
-            if ( psz_artist && *psz_artist )
+            if ( !EMPTY_STR(psz_artist) )
                 input_item_SetArtist( p_input, psz_artist );
             if( psz_name ) input_item_SetTitle( p_input, psz_name );
             if( !EMPTY_STR(psz_album_art) )
@@ -253,14 +284,11 @@ static int Demux( demux_t *p_demux )
         {
             /* Cleanup state */
             while( i_options-- ) free( (char*)ppsz_options[i_options] );
-            free( ppsz_options );
-            ppsz_options = NULL; i_options = 0;
-            free( psz_name );
-            psz_name = NULL;
-            free( psz_artist );
-            psz_artist = NULL;
-            free( psz_album_art );
-            psz_album_art = NULL;
+            FREENULL( ppsz_options );
+            i_options = 0;
+            FREENULL( psz_name );
+            FREENULL( psz_artist );
+            FREENULL( psz_album_art );
             i_parsed_duration = 0;
             i_duration = -1;
 
@@ -273,12 +301,6 @@ static int Demux( demux_t *p_demux )
     return 0; /* Needed for correct operation of go back */
 }
 
-static int Control( demux_t *p_demux, int i_query, va_list args )
-{
-    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
-    return VLC_EGENERIC;
-}
-
 static void parseEXTINF(char *psz_string, char **ppsz_artist,
                         char **ppsz_name, int *pi_duration)
 {