[vlc-devel] [PATCH] added handling of wrong MIME boundary separator to mjpeg demux

Sergey Radionov rsatom at gmail.com
Fri Aug 24 19:12:59 CEST 2012


---
 modules/demux/mjpeg.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/modules/demux/mjpeg.c b/modules/demux/mjpeg.c
index a057db3..1d571ff 100644
--- a/modules/demux/mjpeg.c
+++ b/modules/demux/mjpeg.c
@@ -224,6 +224,7 @@ static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
     else
     {
         i_pos = *p_sys->p_peek == '-' ? 2 : 4;
+        int i_pos_save = i_pos;//to allow restore after missing "\r\n"...
         psz_line = GetLine( p_demux, &i_pos );
         if( NULL == psz_line )
         {
@@ -235,13 +236,63 @@ static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
         /* Read the separator and remember it if not yet stored */
         if( p_sys->psz_separator == NULL )
         {
-            p_sys->psz_separator = psz_line;
+            char *content_type = stream_ContentType( p_demux->s );
+            char* boundary = NULL;
+            if ( content_type != NULL ) {
+                boundary = strstr( content_type, "boundary=" );
+                if( boundary ) boundary += sizeof("boundary=") - 1;
+            }
+
+            //some vendors add "--" at boundary begin and end in ContentType declaration,
+            //and don't add additional "--" in the body - it's incorrect...
+            char* clean_boundary = NULL;
+            if( boundary ) {
+                clean_boundary = boundary;
+                if( !strncmp( clean_boundary, "--", 2 ) ) clean_boundary += 2;
+                const char* clean_boundary_end = strstr(clean_boundary, "--");
+                if( clean_boundary_end )
+                    clean_boundary = strndup( clean_boundary, clean_boundary_end-clean_boundary );
+                else
+                    clean_boundary = strdup( clean_boundary );
+            }
+
+            int bl  = boundary ? strlen(boundary) : 0;
+            int cbl = clean_boundary ? strlen(clean_boundary) : 0;
+            //some other vendors, forget to add "\r\n" after boundary - it's incorrect too...
+            if( boundary && 0 == strncmp( psz_line, boundary, bl ) )
+            {
+                //restore buffer position to right place
+                i_pos = i_pos_save + bl;
+                p_sys->psz_separator = strdup( boundary );
+                if( !strncmp( ( p_sys->p_peek + i_pos ), "--", 2 ) ) //last boundary have "--" at the end
+                    i_pos += 2;
+            }
+            //and some else vendors, forget to add "\r\n" and forget to add "--" too.
+            //(or add excess "--" to ContentType, wich is the same)...
+            else if( clean_boundary && 0 == strncmp( psz_line, clean_boundary, cbl ) )
+            {
+                //restore buffer position to right place
+                i_pos = i_pos_save + cbl;
+                p_sys->psz_separator = strdup( clean_boundary );
+                if( !strncmp( ( p_sys->p_peek + i_pos ), "--", 2 ) ) //last boundary have "--" at the end
+                    i_pos += 2;
+            }
+
+            free( content_type );
+            free( clean_boundary );
+
+            //and some of them do all right - I am proud of them!
+            if( !p_sys->psz_separator )
+                p_sys->psz_separator = psz_line;
+            else
+                free( psz_line );
+
             msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
                      p_sys->psz_separator );
         }
         else
         {
-            if( strcmp( psz_line, p_sys->psz_separator ) )
+            if( strncmp( psz_line, p_sys->psz_separator, strlen(p_sys->psz_separator) ) )
             {
                 msg_Warn( p_demux, "separator %s does not match %s", psz_line,
                           p_sys->psz_separator );
-- 
1.7.7.1.msysgit.0




More information about the vlc-devel mailing list