Commit 07262663 authored by Laurent Aimar's avatar Laurent Aimar

Improve subsdec html output robustness.

parent e422319d
...@@ -576,6 +576,25 @@ static char *StripTags( char *psz_subtitle ) ...@@ -576,6 +576,25 @@ static char *StripTags( char *psz_subtitle )
* returned, and the rendering engine will fall back to the * returned, and the rendering engine will fall back to the
* plain text version of the subtitle. * plain text version of the subtitle.
*/ */
static void HtmlNPut( char **ppsz_html, const char *psz_text, int i_max )
{
const int i_len = strlen(psz_text);
strncpy( *ppsz_html, psz_text, i_max );
*ppsz_html += __MIN(i_max,i_len);
}
static void HtmlPut( char **ppsz_html, const char *psz_text )
{
strcpy( *ppsz_html, psz_text );
*ppsz_html += strlen(psz_text);
}
static void HtmlCopy( char **ppsz_html, char **ppsz_subtitle, const char *psz_text )
{
HtmlPut( ppsz_html, psz_text );
*ppsz_subtitle += strlen(psz_text);
}
static char *CreateHtmlSubtitle( char *psz_subtitle ) static char *CreateHtmlSubtitle( char *psz_subtitle )
{ {
char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 ); char *psz_tag = malloc( ( strlen( psz_subtitle ) / 3 ) + 1 );
...@@ -600,50 +619,39 @@ static char *CreateHtmlSubtitle( char *psz_subtitle ) ...@@ -600,50 +619,39 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
{ {
if( *psz_subtitle == '\n' ) if( *psz_subtitle == '\n' )
{ {
strcpy( psz_html, "<br/>" ); HtmlPut( &psz_html, "<br/>" );
psz_html += 5;
psz_subtitle++; psz_subtitle++;
} }
else if( *psz_subtitle == '<' ) else if( *psz_subtitle == '<' )
{ {
if( !strncasecmp( psz_subtitle, "<br/>", 5 )) if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
{ {
strcpy( psz_html, "<br/>" ); HtmlCopy( &psz_html, &psz_subtitle, "<br/>" );
psz_html += 5;
psz_subtitle += 5;
} }
else if( !strncasecmp( psz_subtitle, "<b>", 3 ) ) else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
{ {
strcpy( psz_html, "<b>" ); HtmlCopy( &psz_html, &psz_subtitle, "<b>" );
strcat( psz_tag, "b" ); strcat( psz_tag, "b" );
psz_html += 3;
psz_subtitle += 3;
} }
else if( !strncasecmp( psz_subtitle, "<i>", 3 ) ) else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
{ {
strcpy( psz_html, "<i>" ); HtmlCopy( &psz_html, &psz_subtitle, "<i>" );
strcat( psz_tag, "i" ); strcat( psz_tag, "i" );
psz_html += 3;
psz_subtitle += 3;
} }
else if( !strncasecmp( psz_subtitle, "<u>", 3 ) ) else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
{ {
strcpy( psz_html, "<u>" ); HtmlCopy( &psz_html, &psz_subtitle, "<u>" );
strcat( psz_tag, "u" ); strcat( psz_tag, "u" );
psz_html += 3;
psz_subtitle += 3;
} }
else if( !strncasecmp( psz_subtitle, "<font ", 6 )) else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
{ {
const char *psz_attribs[] = { "face=\"", "family=\"", "size=\"", const char *psz_attribs[] = { "face=", "family=", "size=",
"color=\"", "outline-color=\"", "shadow-color=\"", "color=", "outline-color=", "shadow-color=",
"outline-level=\"", "shadow-level=\"", "back-color=\"", "outline-level=", "shadow-level=", "back-color=",
"alpha=\"", NULL }; "alpha=", NULL };
strcpy( psz_html, "<font " ); HtmlCopy( &psz_html, &psz_subtitle, "<font " );
strcat( psz_tag, "f" ); strcat( psz_tag, "f" );
psz_html += 6;
psz_subtitle += 6;
while( *psz_subtitle != '>' ) while( *psz_subtitle != '>' )
{ {
...@@ -653,13 +661,31 @@ static char *CreateHtmlSubtitle( char *psz_subtitle ) ...@@ -653,13 +661,31 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
{ {
int i_len = strlen( psz_attribs[ k ] ); int i_len = strlen( psz_attribs[ k ] );
if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len )) if( !strncasecmp( psz_subtitle, psz_attribs[k], i_len ) )
{ {
i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1; /* */
HtmlPut( &psz_html, psz_attribs[k] );
psz_subtitle += i_len;
/* */
if( *psz_subtitle == '"' )
{
psz_subtitle++;
i_len = strcspn( psz_subtitle, "\"" );
}
else
{
i_len = strcspn( psz_subtitle, " \t>" );
}
HtmlPut( &psz_html, "\"" );
if( !strcmp( psz_attribs[ k ], "color=" ) && *psz_subtitle >= '0' && *psz_subtitle <= '9' )
HtmlPut( &psz_html, "#" );
HtmlNPut( &psz_html, psz_subtitle, i_len );
HtmlPut( &psz_html, "\"" );
strncpy( psz_html, psz_subtitle, i_len );
psz_html += i_len;
psz_subtitle += i_len; psz_subtitle += i_len;
if( *psz_subtitle == '\"' )
psz_subtitle++;
break; break;
} }
} }
...@@ -728,26 +754,19 @@ static char *CreateHtmlSubtitle( char *psz_subtitle ) ...@@ -728,26 +754,19 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
{ {
if( !strncasecmp( psz_subtitle, "&lt;", 4 )) if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
{ {
strcpy( psz_html, "&lt;" ); HtmlCopy( &psz_html, &psz_subtitle, "&lt;" );
psz_html += 4;
psz_subtitle += 4;
} }
else if( !strncasecmp( psz_subtitle, "&gt;", 4 )) else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
{ {
strcpy( psz_html, "&gt;" ); HtmlCopy( &psz_html, &psz_subtitle, "&gt;" );
psz_html += 4;
psz_subtitle += 4;
} }
else if( !strncasecmp( psz_subtitle, "&amp;", 5 )) else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
{ {
strcpy( psz_html, "&amp;" ); HtmlCopy( &psz_html, &psz_subtitle, "&amp;" );
psz_html += 5;
psz_subtitle += 5;
} }
else else
{ {
strcpy( psz_html, "&amp;" ); HtmlPut( &psz_html, "&amp;" );
psz_html += 5;
psz_subtitle++; psz_subtitle++;
} }
} }
...@@ -757,24 +776,22 @@ static char *CreateHtmlSubtitle( char *psz_subtitle ) ...@@ -757,24 +776,22 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
if( psz_html > psz_html_start ) if( psz_html > psz_html_start )
{ {
/* Check for double whitespace */ /* Check for double whitespace */
if((( *psz_html == ' ' ) || if( ( *psz_html == ' ' || *psz_html == '\t' ) &&
( *psz_html == '\t' )) && ( *(psz_html-1) == ' ' || *(psz_html-1) == '\t' ) )
(( *(psz_html-1) == ' ' ) ||
( *(psz_html-1) == '\t' )))
{ {
strcpy( psz_html, NO_BREAKING_SPACE ); HtmlPut( &psz_html, NO_BREAKING_SPACE );
psz_html += strlen( NO_BREAKING_SPACE ) - 1; psz_html--;
} }
} }
psz_html++; psz_html++;
psz_subtitle++; psz_subtitle++;
} }
if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 10 ) if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 50 )
{ {
int i_len = psz_html - psz_html_start; int i_len = psz_html - psz_html_start;
i_buf_size += 100; i_buf_size += 200;
psz_html_start = realloc( psz_html_start, i_buf_size ); psz_html_start = realloc( psz_html_start, i_buf_size );
psz_html = psz_html_start + i_len; psz_html = psz_html_start + i_len;
*psz_html = '\0'; *psz_html = '\0';
...@@ -795,5 +812,7 @@ static char *CreateHtmlSubtitle( char *psz_subtitle ) ...@@ -795,5 +812,7 @@ static char *CreateHtmlSubtitle( char *psz_subtitle )
psz_html_start = realloc( psz_html_start, psz_html - psz_html_start + 1 ); psz_html_start = realloc( psz_html_start, psz_html - psz_html_start + 1 );
} }
free( psz_tag ); free( psz_tag );
return psz_html_start; return psz_html_start;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment