Commit 69f016b5 authored by Derk-Jan Hartman's avatar Derk-Jan Hartman

* xtag parser improvements by Daniel Stranger.

it now ignores
 - commments <!-- ... -->
 - processing instructions <? ... ?>
 - doctype declarations <!DOCTYPE ... >
and can handle
 - CDATA element content <![CDATA[ ... ]]>
parent 666a3ebf
...@@ -38,7 +38,7 @@ Colin Simmonds <colin_simmonds at Mac.lover.org> - compile fix for Mac OS X ...@@ -38,7 +38,7 @@ Colin Simmonds <colin_simmonds at Mac.lover.org> - compile fix for Mac OS X
Damian Ivereigh <damian at cisco.com> - ac3dec uninitialized data structure fix Damian Ivereigh <damian at cisco.com> - ac3dec uninitialized data structure fix
Damien Fouilleul <damien.fouilleul at laposte.net> - DirectShow input improvements Damien Fouilleul <damien.fouilleul at laposte.net> - DirectShow input improvements
Daniel Fischer <dan at subsignal dot org> - Shoutcast output support Daniel Fischer <dan at subsignal dot org> - Shoutcast output support
Daniel Stränger <vlc at schmaller d0t de> - M3U and playlist improvements Daniel Stränger <vlc at schmaller d0t de> - M3U, xtag and playlist improvements
David Kennedy <dkennedy at tinytoad.com> - X11 fullscreen patch David Kennedy <dkennedy at tinytoad.com> - X11 fullscreen patch
Daniel Nylander <info@danielnylander at se> - Swedish translation Daniel Nylander <info@danielnylander at se> - Swedish translation
David Weber <david_weber at gmx.de> - Mac OS X interface design & graphics (v0.5.0) David Weber <david_weber at gmx.de> - Mac OS X interface design & graphics (v0.5.0)
......
...@@ -165,19 +165,28 @@ static void CatalogAdd( xml_t *p_xml, const char *psz_arg1, ...@@ -165,19 +165,28 @@ static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *s ) static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *s )
{ {
xml_reader_t *p_reader; xml_reader_t *p_reader;
char *p_buffer; char *p_buffer, *p_new;
int i_size, i_pos = 0, i_buffer = 2048; int i_size, i_pos = 0, i_buffer = 2048;
XTag *p_root; XTag *p_root;
/* Open and read file */ /* Open and read file */
p_buffer = malloc( i_buffer ); p_buffer = malloc( i_buffer );
if( p_buffer == NULL ) return NULL; if( p_buffer == NULL ) {
msg_Err( p_xml, "ENOMEM: alloc buffer" );
return NULL;
}
while( ( i_size = stream_Read( s, &p_buffer[i_pos], 2048 ) ) == 2048 ) while( ( i_size = stream_Read( s, &p_buffer[i_pos], 2048 ) ) == 2048 )
{ {
i_pos += i_size; i_pos += i_size;
i_buffer += i_size; i_buffer += i_size;
p_buffer = realloc( p_buffer, i_buffer ); p_new = realloc( p_buffer, i_buffer );
if (!p_new) {
msg_Err( p_xml, "ENOMEM: realloc buffer" );
free( p_buffer );
return NULL;
}
p_buffer = p_new;
} }
p_buffer[ i_pos + i_size ] = 0; /* 0 terminated string */ p_buffer[ i_pos + i_size ] = 0; /* 0 terminated string */
...@@ -367,6 +376,7 @@ static void xlist_free( XList *list ) ...@@ -367,6 +376,7 @@ static void xlist_free( XList *list )
#define X_SLASH 1<<6 #define X_SLASH 1<<6
#define X_QMARK 1<<7 #define X_QMARK 1<<7
#define X_DASH 1<<8 #define X_DASH 1<<8
#define X_EMARK 1<<9
static int xtag_cin( char c, int char_class ) static int xtag_cin( char c, int char_class )
{ {
...@@ -377,8 +387,9 @@ static int xtag_cin( char c, int char_class ) ...@@ -377,8 +387,9 @@ static int xtag_cin( char c, int char_class )
if( char_class & X_SQUOTE ) if( c == '\'' ) return VLC_TRUE; if( char_class & X_SQUOTE ) if( c == '\'' ) return VLC_TRUE;
if( char_class & X_EQUAL ) if( c == '=' ) return VLC_TRUE; if( char_class & X_EQUAL ) if( c == '=' ) return VLC_TRUE;
if( char_class & X_SLASH ) if( c == '/' ) return VLC_TRUE; if( char_class & X_SLASH ) if( c == '/' ) return VLC_TRUE;
if( char_class & X_QMARK ) if( c == '!' ) return VLC_TRUE; if( char_class & X_QMARK ) if( c == '?' ) return VLC_TRUE;
if( char_class & X_DASH ) if( c == '-' ) return VLC_TRUE; if( char_class & X_DASH ) if( c == '-' ) return VLC_TRUE;
if( char_class & X_EMARK ) if( c == '!' ) return VLC_TRUE;
return VLC_FALSE; return VLC_FALSE;
} }
...@@ -548,44 +559,21 @@ static XTag *xtag_parse_tag( XTagParser *parser ) ...@@ -548,44 +559,21 @@ static XTag *xtag_parse_tag( XTagParser *parser )
char *name; char *name;
char *pcdata; char *pcdata;
char *s; char *s;
int xi;
if( !parser->valid ) return NULL; if( !parser->valid ) return NULL;
#if 0 /* Do we really want all the whitespace pcdata ? */
xtag_skip_whitespace( parser );
#endif
if( (pcdata = xtag_slurp_to( parser, X_OPENTAG, X_NONE )) != NULL )
{
tag = malloc( sizeof(*tag) );
tag->name = NULL;
tag->pcdata = pcdata;
tag->parent = parser->current_tag;
tag->attributes = NULL;
tag->children = NULL;
tag->current_child = NULL;
return tag;
}
s = parser->start; s = parser->start;
/* if this starts a close tag, return NULL and let the parent take it */
if( xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_SLASH ) )
return NULL;
/* if this starts a comment tag, skip until end */ /* if this starts a comment tag, skip until end */
if( xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_QMARK ) && if( (parser->end - parser->start) > 7 &&
xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_EMARK ) &&
xtag_cin( s[2], X_DASH ) && xtag_cin( s[3], X_DASH ) ) xtag_cin( s[2], X_DASH ) && xtag_cin( s[3], X_DASH ) )
{ {
int xi;
parser->start = s = &s[4]; parser->start = s = &s[4];
while( (xi = xtag_index( parser, X_DASH )) >= 0 ) while( (xi = xtag_index( parser, X_DASH )) >= 0 )
{ {
parser->start = s = &s[xi+1]; parser->start = s = &s[xi+1];
if( xtag_cin( s[0], X_DASH ) && xtag_cin( s[1], X_CLOSETAG ) ) if( xtag_cin( s[0], X_DASH ) && xtag_cin( s[1], X_CLOSETAG ) )
{ {
parser->start = &s[2]; parser->start = &s[2];
...@@ -593,19 +581,79 @@ static XTag *xtag_parse_tag( XTagParser *parser ) ...@@ -593,19 +581,79 @@ static XTag *xtag_parse_tag( XTagParser *parser )
return xtag_parse_tag( parser ); return xtag_parse_tag( parser );
} }
} }
return NULL;
}
/* ignore processing instructions '<?' ... '?>' */
if( (parser->end - parser->start) > 4 &&
xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_QMARK ) )
{
parser->start = s = &s[2];
while ((xi = xtag_index( parser, X_QMARK )) >= 0) {
if (xtag_cin( s[xi+1], X_CLOSETAG )) {
parser->start = &s[xi+2];
xtag_skip_whitespace( parser );
return xtag_parse_tag( parser );
}
}
return NULL; return NULL;
} }
/* FIXME: if this starts a DOCTYPE tag, skip until end */ /* ignore doctype '<!DOCTYPE' ... '>' */
if( xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_QMARK ) ) if ( (parser->end - parser->start) > 8 &&
!strncmp( s, "<!DOCTYPE", 9 ) ) {
xi = xtag_index( parser, X_CLOSETAG );
if ( xi > 0 ) {
parser->start = s = &s[xi+1];
xtag_skip_whitespace( parser );
return xtag_parse_tag( parser );
}
else {
return NULL;
}
}
if( (pcdata = xtag_slurp_to( parser, X_OPENTAG, X_NONE )) != NULL )
{ {
int xi = xtag_index( parser, X_CLOSETAG ); tag = malloc( sizeof(*tag) );
if( xi <= 0 ) return NULL; tag->name = NULL;
tag->pcdata = pcdata;
tag->parent = parser->current_tag;
tag->attributes = NULL;
tag->children = NULL;
tag->current_child = NULL;
parser->start = &s[xi+1]; return tag;
xtag_skip_whitespace( parser ); }
return xtag_parse_tag( parser );
/* if this starts a close tag, return NULL and let the parent take it */
if( xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_SLASH ) )
return NULL;
/* parse CDATA content */
if ( (parser->end - parser->start) > 8 &&
!strncmp( s, "<![CDATA[", 9 ) ) {
parser->start = s = &s[9];
while (parser->end - s > 2) {
if (strncmp( s, "]]>", 3 ) == 0) {
if ( !(tag = malloc( sizeof(*tag))) ) return NULL;
if ( !(pcdata = malloc( sizeof(char)*(s - parser->start + 1))) ) return NULL;
strncpy( pcdata, parser->start, s - parser->start );
pcdata[s - parser->start]='\0';
parser->start = s = &s[3];
tag->name = NULL;
tag->pcdata = pcdata;
tag->parent = parser->current_tag;
tag->attributes = NULL;
tag->children = NULL;
tag->current_child = NULL;
return tag;
}
else {
s++;
}
}
return NULL;
} }
if( !xtag_assert_and_pass( parser, X_OPENTAG ) ) return NULL; if( !xtag_assert_and_pass( parser, X_OPENTAG ) ) return NULL;
...@@ -670,12 +718,13 @@ static XTag *xtag_parse_tag( XTagParser *parser ) ...@@ -670,12 +718,13 @@ static XTag *xtag_parse_tag( XTagParser *parser )
xtag_skip_whitespace( parser ); xtag_skip_whitespace( parser );
xtag_assert_and_pass( parser, X_CLOSETAG ); xtag_assert_and_pass( parser, X_CLOSETAG );
xtag_skip_whitespace( parser );
} }
else else
{ {
xtag_assert_and_pass( parser, X_SLASH ); xtag_assert_and_pass( parser, X_SLASH );
xtag_assert_and_pass( parser, X_CLOSETAG ); xtag_assert_and_pass( parser, X_CLOSETAG );
xtag_skip_whitespace( parser );
} }
return tag; return tag;
......
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