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

Sergey Radionov rsatom at gmail.com
Tue Aug 28 09:45:38 CEST 2012


2012/8/26 Sergey Radionov <rsatom at gmail.com>:
> ---
>  modules/demux/mjpeg.c |   83 ++++++++++++++++++++++++++++++++++++++++++++----
>  1 files changed, 76 insertions(+), 7 deletions(-)
>
> diff --git a/modules/demux/mjpeg.c b/modules/demux/mjpeg.c
> index a057db3..055a908 100644
> --- a/modules/demux/mjpeg.c
> +++ b/modules/demux/mjpeg.c
> @@ -200,10 +200,10 @@ static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
>              char *content_type = stream_ContentType( p_demux->s );
>              if ( content_type == NULL )
>                  return false;
> -            const char* boundary = strstr( content_type, "boundary=--" );
> +            const char* boundary = strstr( content_type, "boundary=" );
>              if ( boundary != NULL )
>              {
> -                p_sys->psz_separator = strdup( boundary + strlen( "boundary=--" ) );
> +                p_sys->psz_separator = strdup( boundary + sizeof("boundary=") - 1 );
>                  msg_Dbg( p_demux, "Video boundary extracted from Content-Type: %s", p_sys->psz_separator );
>                  free( content_type );
>                  /* Skip to HTTP header parsing as there's no boundary to extract
> @@ -223,7 +223,8 @@ static bool CheckMimeHeader( demux_t *p_demux, int *p_header_size )
>      }
>      else
>      {
> -        i_pos = *p_sys->p_peek == '-' ? 2 : 4;
> +        i_pos = *p_sys->p_peek == '-' ? 0 : 2;//skip "\r\n" if exist
> +        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,77 @@ 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;
> -            msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
> -                     p_sys->psz_separator );
> +            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...
> +            //some other vendors, forget to add "\r\n" after boundary - it's incorrect too...
> +            //and some else vendors, forget to add "\r\n" and forget to add "--" both
> +            //(or add excess "--" to ContentType, which is the same)...
> +
> +            size_t bl = boundary ? strlen(boundary) : 0;
> +            //if used boundary without additional "--"
> +            //(i.e. used same string was declared in ContentType), then
> +            if( boundary && !strncmp( psz_line, boundary, bl ) )
> +            {
> +                //if boundary identical to psz_line, then
> +                if( (psz_line + bl) == '\0' )
> +                {
> +                    //just use whole line as separator
> +                    p_sys->psz_separator = psz_line;
> +                }
> +                else
> +                {
> +                    //restore buffer position to right place
> +                    i_pos = i_pos_save + bl;
> +                    p_sys->psz_separator = strdup( boundary );
> +                    free( psz_line );
> +                }
> +            }
> +
> +            //if used boundary with additional "--"
> +            if( !p_sys->psz_separator && boundary &&
> +                !strncmp( psz_line + sizeof("--") - 1, boundary, bl ) )
> +            {
> +                char* s = psz_line + sizeof("--") - 1 + bl;
> +                if( !strncmp( s , "--", 2 ) ) //skip "--" if boundary is last
> +                    s += 2;
> +
> +                //"\r\n" is forgotten
> +                if ( s != '\0' ) {
> +                    //restore buffer position to right place
> +                    i_pos = i_pos_save + bl;
> +                }
> +
> +                p_sys->psz_separator = strdup( boundary );
> +                free( psz_line );
> +            }
> +
> +            if( !p_sys->psz_separator )
> +            {
> +                //separator was not found, then just use whole line
> +                //FIXME: don't sure it's right
> +                p_sys->psz_separator = psz_line;
> +            }
> +
> +            if ( p_sys->psz_separator ) {
> +                msg_Dbg( p_demux, "Multipart MIME detected, using separator: %s",
> +                         p_sys->psz_separator );
> +            }
> +
> +            free( content_type );
>          }
>          else
>          {
> -            if( strcmp( psz_line, p_sys->psz_separator ) )
> +            size_t sl = strlen(p_sys->psz_separator);
> +            if( strncmp( psz_line + sizeof("--") - 1, p_sys->psz_separator, sl ) &&
> +                strncmp( psz_line, p_sys->psz_separator, sl ) )
>              {
>                  msg_Warn( p_demux, "separator %s does not match %s", psz_line,
>                            p_sys->psz_separator );
> @@ -527,7 +592,11 @@ static int MimeDemux( demux_t *p_demux )
>              }
>          }
>
> +        //some vendors use as separator same string which was defined in ContentType
> +        //without additional "--"
>          if( !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i + 2),
> +                      strlen( p_sys->psz_separator ) ) ||
> +            !strncmp( p_sys->psz_separator, (char *)(p_sys->p_peek + i),
>                        strlen( p_sys->psz_separator ) ) )
>          {
>              break;
> --
> 1.7.7.1.msysgit.0
>

don't forget please....



More information about the vlc-devel mailing list