[vlc-devel] [PATCH 2/2] demux/subtitles: fix 17602: replace usage of legacy line-peeking

Filip Roséen filip at atch.se
Sun Nov 6 17:58:45 CET 2016


The formerly used function, peek_Readline, would not respect the BOM
of the underlying stream when peeking (resulting in lines being
interpreted in the wrong encoding if such was present).

These changes removes the broken implementation, and its usage with
that of the more stable peek_ReadLine.

fixes #17602
---
 modules/demux/subtitle.c        | 10 ++++++----
 modules/demux/subtitle_helper.h | 26 --------------------------
 modules/demux/vobsub.c          | 18 ++++++++++++------
 3 files changed, 18 insertions(+), 36 deletions(-)

diff --git a/modules/demux/subtitle.c b/modules/demux/subtitle.c
index 653c529..a8ae429 100644
--- a/modules/demux/subtitle.c
+++ b/modules/demux/subtitle.c
@@ -305,8 +305,6 @@ static int Open ( vlc_object_t *p_this )
 #ifndef NDEBUG
     const uint64_t i_start_pos = vlc_stream_Tell( p_demux->s );
 #endif
-    uint64_t i_read_offset = 0;
-
     /* Detect Unicode while skipping the UTF-8 Byte Order Mark */
     bool unicode = false;
     const uint8_t *p_data;
@@ -314,7 +312,6 @@ static int Open ( vlc_object_t *p_this )
      && !memcmp( p_data, "\xEF\xBB\xBF", 3 ) )
     {
         unicode = true;
-        i_read_offset = 3; /* skip BOM */
         msg_Dbg( p_demux, "detected Unicode Byte Order Mark" );
     }
 
@@ -324,13 +321,15 @@ static int Open ( vlc_object_t *p_this )
         int     i_try;
         char    *s = NULL;
 
+        stream_t* p_peekstream = NULL;
+
         msg_Dbg( p_demux, "autodetecting subtitle format" );
         for( i_try = 0; i_try < 256; i_try++ )
         {
             int i_dummy;
             char p_dummy;
 
-            if( (s = peek_Readline( p_demux->s, &i_read_offset )) == NULL )
+            if( (s = subtitle_PeekLine( p_demux->s, &p_peekstream )) == NULL )
                 break;
 
             if( strcasestr( s, "<SAMI>" ) )
@@ -482,6 +481,9 @@ static int Open ( vlc_object_t *p_this )
         }
 
         free( s );
+
+        if( p_peekstream )
+            vlc_stream_Delete( p_peekstream );
     }
 
     /* Quit on unknown subtitles */
diff --git a/modules/demux/subtitle_helper.h b/modules/demux/subtitle_helper.h
index fc71c12..7b0cfad 100644
--- a/modules/demux/subtitle_helper.h
+++ b/modules/demux/subtitle_helper.h
@@ -21,32 +21,6 @@
 #include <vlc_common.h>
 #include <vlc_stream.h>
 
-inline static char * peek_Readline( stream_t *p_demuxstream, uint64_t *pi_offset )
-{
-    uint8_t *p_peek;
-    ssize_t i_peek = vlc_stream_Peek( p_demuxstream, (const uint8_t **) &p_peek,
-                                  *pi_offset + 2048 );
-    if( i_peek < 0 || (uint64_t) i_peek < *pi_offset )
-        return NULL;
-
-    const uint64_t i_bufsize = (uint64_t) i_peek - *pi_offset;
-    char *psz_line = NULL;
-
-    /* Create a stream memory from that offset */
-    stream_t *p_memorystream = vlc_stream_MemoryNew( p_demuxstream,
-                                                     &p_peek[*pi_offset],
-                                                     i_bufsize, true );
-    if( p_memorystream )
-    {
-        psz_line = vlc_stream_ReadLine( p_memorystream );
-
-        *pi_offset += vlc_stream_Tell( p_memorystream );
-        vlc_stream_Delete( p_memorystream );
-    }
-
-    return psz_line;
-}
-
 /**
  * Peek next line of stream
  *
diff --git a/modules/demux/vobsub.c b/modules/demux/vobsub.c
index 77cb024..30de5e9 100644
--- a/modules/demux/vobsub.c
+++ b/modules/demux/vobsub.c
@@ -121,19 +121,25 @@ static int Open ( vlc_object_t *p_this )
 {
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
-    char *psz_vobname, *s;
+    stream_t* p_peekstream = NULL;
+    char *psz_vobname;
     int i_len;
-    uint64_t i_read_offset = 0;
 
-    if( ( s = peek_Readline( p_demux->s, &i_read_offset ) ) != NULL )
+    char* line = subtitle_PeekLine( p_demux->s, &p_peekstream );
+
+    if( p_peekstream )
+        vlc_stream_Delete( p_peekstream );
+
+    if( line )
     {
-        if( !strcasestr( s, "# VobSub index file" ) )
+        char* token = strcasestr( line, "# VobSub index file" );
+        free( line );
+
+        if( !token )
         {
             msg_Dbg( p_demux, "this doesn't seem to be a vobsub file" );
-            free( s );
             return VLC_EGENERIC;
         }
-        free( s );
     }
     else
     {
-- 
2.10.2



More information about the vlc-devel mailing list