[vlc-commits] stream_ReadLine: move BOM detection outside and before reading loop

Pierre Ynard git at videolan.org
Tue Oct 27 09:10:15 CET 2020


vlc | branch: master | Pierre Ynard <linkfanel at yahoo.fr> | Tue Oct 27 08:56:06 2020 +0100| [5cfcbd7c18abfe46cbddb2e5a5c287c14674b395] | committer: Pierre Ynard

stream_ReadLine: move BOM detection outside and before reading loop

No functional change

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5cfcbd7c18abfe46cbddb2e5a5c287c14674b395
---

 src/input/stream.c | 45 ++++++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 19 deletions(-)

diff --git a/src/input/stream.c b/src/input/stream.c
index 9cba64697f..b39e5bbd44 100644
--- a/src/input/stream.c
+++ b/src/input/stream.c
@@ -194,32 +194,28 @@ char *vlc_stream_ReadLine( stream_t *s )
     if( s->pf_read == NULL && s->pf_block == NULL )
         return NULL;
 
-    for( ;; )
+    /* BOM detection */
+    if( vlc_stream_Tell( s ) == 0 )
     {
-        char *psz_eol;
         const uint8_t *p_data;
-        int i_data;
-        int64_t i_pos;
+        ssize_t i_data = vlc_stream_Peek( s, &p_data, 2 );
 
-        /* Probe new data */
-        i_data = vlc_stream_Peek( s, &p_data, STREAM_PROBE_LINE );
-        if( i_data <= 0 ) break; /* No more data */
+        if( i_data <= 0 )
+            return NULL;
+
+        if( unlikely(priv->text.conv != (vlc_iconv_t)-1) )
+        {   /* seek back to beginning? reset */
+            vlc_iconv_close( priv->text.conv );
+            priv->text.conv = (vlc_iconv_t)-1;
+        }
+        priv->text.char_width = 1;
+        priv->text.little_endian = false;
 
-        /* BOM detection */
-        i_pos = vlc_stream_Tell( s );
-        if( i_pos == 0 && i_data >= 2 )
+        if( i_data >= 2 )
         {
             const char *psz_encoding = NULL;
             bool little_endian = false;
 
-            if( unlikely(priv->text.conv != (vlc_iconv_t)-1) )
-            {   /* seek back to beginning? reset */
-                vlc_iconv_close( priv->text.conv );
-                priv->text.conv = (vlc_iconv_t)-1;
-            }
-            priv->text.char_width = 1;
-            priv->text.little_endian = false;
-
             if( !memcmp( p_data, "\xFF\xFE", 2 ) )
             {
                 psz_encoding = "UTF-16LE";
@@ -238,12 +234,23 @@ char *vlc_stream_ReadLine( stream_t *s )
                 if( unlikely(priv->text.conv == (vlc_iconv_t)-1) )
                 {
                     msg_Err( s, "iconv_open failed" );
-                    goto error;
+                    return NULL;
                 }
                 priv->text.char_width = 2;
                 priv->text.little_endian = little_endian;
             }
         }
+    }
+
+    for( ;; )
+    {
+        char *psz_eol;
+        const uint8_t *p_data;
+        int i_data;
+
+        /* Probe new data */
+        i_data = vlc_stream_Peek( s, &p_data, STREAM_PROBE_LINE );
+        if( i_data <= 0 ) break; /* No more data */
 
         /* Deal here with lone-byte incomplete UTF-16 sequences at EOF
            that we won't be able to process anyway */



More information about the vlc-commits mailing list