+ psz_bol = strchr( psz_bol, ':' );
+ if ( !psz_bol ) return 0;
+ psz_bol++;
+
+ strcpy( psz_data , psz_bol );
+ return 3;
+ }
+ }
+ /* If we don't have a comment, the line is directly the URI */
+ }
+ else if( p_m3u->i_type == TYPE_PLS )
+ {
+ /* We are dealing with .pls files from shoutcast
+ * We are looking for lines like "File1=http://..." */
+ if( !strncasecmp( psz_bol, "File", sizeof("File") - 1 ) )
+ {
+ psz_bol += sizeof("File") - 1;
+ psz_bol = strchr( psz_bol, '=' );
+ if ( !psz_bol ) return 0;
+ psz_bol++;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ else if( p_m3u->i_type == TYPE_WMP )
+ {
+ /* We are dealing with some weird WMP stream playlist format
+ * Hurray for idiotic M$. Lines look like: "Ref1=http://..." */
+ if( !strncasecmp( psz_bol, "Ref", sizeof("Ref") - 1 ) )
+ {
+ psz_bol += sizeof("Ref") - 1;
+ psz_bol = strchr( psz_bol, '=' );
+ if ( !psz_bol ) return 0;
+ psz_bol++;
+ if( !strncasecmp( psz_bol, "http://", sizeof("http://") -1 ) )
+ {
+ psz_bol[0] = 'm'; psz_bol[1] = 'm'; psz_bol[2] = 's'; psz_bol[3] = 'h';
+ }
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ else if( p_m3u->i_type == TYPE_ASX )
+ {
+ /* We are dealing with ASX files.
+ * We are looking for "<ref href=" xml markups that
+ * begins with "mms://", "http://" or "file://" */
+ char *psz_eol;
+
+ while( *psz_bol &&
+ strncasecmp( psz_bol, "ref", sizeof("ref") - 1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ while( *psz_bol &&
+ strncasecmp( psz_bol, "href", sizeof("href") - 1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ while( *psz_bol &&
+ strncasecmp( psz_bol, "mms://",
+ sizeof("mms://") - 1 ) &&
+ strncasecmp( psz_bol, "mmsu://",
+ sizeof("mmsu://") - 1 ) &&
+ strncasecmp( psz_bol, "mmst://",
+ sizeof("mmst://") - 1 ) &&
+ strncasecmp( psz_bol, "http://",
+ sizeof("http://") - 1 ) &&
+ strncasecmp( psz_bol, "file://",
+ sizeof("file://") - 1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ psz_eol = strchr( psz_bol, '"');
+ if( !psz_eol )
+ return 0;
+
+ *psz_eol = '\0';
+ }
+ else if( p_m3u->i_type == TYPE_HTML )
+ {
+ /* We are dealing with a html file with embedded
+ * video. We are looking for "<param name="filename"
+ * value=" html markups that begin with "http://" */
+ char *psz_eol;
+
+ while( *psz_bol &&
+ strncasecmp( psz_bol, "param", sizeof("param") - 1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ while( *psz_bol &&
+ strncasecmp( psz_bol, "filename", sizeof("filename") - 1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ while( *psz_bol &&
+ strncasecmp( psz_bol, "http://",
+ sizeof("http://") - 1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ psz_eol = strchr( psz_bol, '"');
+ if( !psz_eol )
+ return 0;
+
+ *psz_eol = '\0';
+
+ }
+ else if( p_m3u->i_type == TYPE_B4S )
+ {
+
+ char *psz_eol;
+
+ msg_Dbg( p_demux, "b4s line=%s", psz_line );
+ /* We are dealing with a B4S file from Winamp 3 */
+
+ /* First, search for name *
+ * <Name>Blabla</Name> */
+
+ if( strstr ( psz_bol, "<Name>" ) )
+ {
+ /* We have a name */
+ while ( *psz_bol &&
+ strncasecmp( psz_bol,"Name",sizeof("Name") -1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ psz_bol = psz_bol + 5 ;
+ /* We are now at the beginning of the name */
+
+ if( !psz_bol ) return 0;
+
+
+ psz_eol = strchr(psz_bol, '<' );
+ if( !psz_eol) return 0;
+
+ *psz_eol='\0';
+
+ XMLSpecialChars( psz_bol );
+
+ strcpy( psz_data, psz_bol );
+ return 2;
+ }
+ else if( strstr( psz_bol, "</entry>" ) || strstr( psz_bol, "</Entry>" ))
+ {
+ *pb_next = VLC_TRUE;
+ return 0;
+ }
+
+ /* We are looking for <entry Playstring="blabla"> */
+
+
+ while ( *psz_bol &&
+ strncasecmp( psz_bol,"Playstring",sizeof("Playstring") -1 ) )
+ psz_bol++;
+
+ if( !*psz_bol ) return 0;
+
+ psz_bol = strchr( psz_bol, '=' );
+ if ( !psz_bol ) return 0;
+
+ psz_bol += 2;
+
+ psz_eol= strchr(psz_bol, '"');
+ if( !psz_eol ) return 0;
+
+ *psz_eol= '\0';
+
+ /* Handle the XML special characters */
+ XMLSpecialChars( psz_bol );
+ }
+ else if( p_m3u->i_type == TYPE_RTSP )
+ {
+ /* We are dealing with rtsptext reference files
+ * Ignore anthying that doesn't start with rtsp://..." */
+ if( strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
+ /* ignore */ return 0;
+ }
+ else
+ {
+ msg_Warn( p_demux, "unknown file type" );
+ return 0;
+ }
+
+ /* empty line */
+ if ( !*psz_bol ) return 0;
+
+ /*
+ * From now on, we know we've got a meaningful line
+ */
+
+ /* check for a protocol name */
+ /* for URL, we should look for "://"
+ * for MRL (Media Resource Locator) ([[<access>][/<demux>]:][<source>]),
+ * we should look for ":"
+ * so we end up looking simply for ":"*/
+ /* PB: on some file systems, ':' are valid characters though*/
+ psz_name = psz_bol;
+ while( *psz_name && *psz_name!=':' )
+ {
+ psz_name++;
+ }
+#ifdef WIN32
+ if ( *psz_name && ( psz_name == psz_bol + 1 ) )
+ {
+ /* if it is not an URL,
+ * as it is unlikely to be an MRL (PB: if it is ?)
+ * it should be an absolute file name with the drive letter */
+ if ( *(psz_name+1) == '/' )/* "*:/" */
+ {
+ if ( *(psz_name+2) != '/' )/* not "*://" */
+ while ( *psz_name ) *psz_name++;/* so now (*psz_name==0) */
+ }
+ else while ( *psz_name ) *psz_name++;/* "*:*"*/
+ }
+#endif
+
+ /* if the line doesn't specify a protocol name,
+ * check if the line has an absolute or relative path */
+#ifndef WIN32
+ if( !*psz_name && *psz_bol != '/' )
+ /* If this line doesn't begin with a '/' */
+#else
+ if( !*psz_name
+ && *psz_bol!='/'
+ && *psz_bol!='\\'
+ && *(psz_bol+1)!=':' )
+ /* if this line doesn't begin with
+ * "/" or "\" or "*:" or "*:\" or "*:/" or "\\" */
+#endif
+ {
+ /* assume the path is relative to the path of the m3u file. */
+ char *psz_path = strdup( p_demux->psz_path );
+
+#ifndef WIN32
+ psz_name = strrchr( psz_path, '/' );
+#else
+ psz_name = strrchr( psz_path, '\\' );
+ if ( ! psz_name ) psz_name = strrchr( psz_path, '/' );
+#endif
+ if( psz_name ) *psz_name = '\0';
+ else *psz_path = '\0';
+#ifndef WIN32
+ psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
+ sprintf( psz_name, "%s/%s", psz_path, psz_bol );
+#else
+ if ( *psz_path != '\0' )
+ {
+ psz_name = malloc( strlen(psz_path) + strlen(psz_bol) + 2 );
+ sprintf( psz_name, "%s\\%s", psz_path, psz_bol );
+ }
+ else psz_name = strdup( psz_bol );
+#endif
+ free( psz_path );
+ }
+ else
+ {
+ psz_name = strdup( psz_bol );
+ }
+
+ strcpy(psz_data, psz_name ) ;
+
+ free( psz_name );
+
+ if( p_m3u->i_type != TYPE_B4S )
+ {
+ *pb_next = VLC_TRUE;
+ }
+
+ return 1;
+}
+
+static void ProcessLine ( demux_t *p_demux, playlist_t *p_playlist,
+ char *psz_line, char **ppsz_uri, char **ppsz_name,
+ int *pi_options, char ***pppsz_options,
+ int *pi_position )
+{
+ char psz_data[MAX_LINE];
+ vlc_bool_t b_next;
+
+ switch( ParseLine( p_demux, psz_line, psz_data, &b_next ) )
+ {
+ case 1:
+ if( *ppsz_uri )
+ {
+ free( *ppsz_uri );
+ }
+ *ppsz_uri = strdup( psz_data );
+ break;
+ case 2:
+ if( *ppsz_name )
+ {
+ free( *ppsz_name );
+ }
+ *ppsz_name = strdup( psz_data );
+ break;
+ case 3:
+ (*pi_options)++;
+ *pppsz_options = realloc( *pppsz_options,
+ sizeof(char *) * *pi_options );
+ (*pppsz_options)[*pi_options - 1] = strdup( psz_data );
+ break;
+ case 0:
+ default:
+ break;
+ }
+
+ if( b_next && *ppsz_uri )
+ {
+ playlist_AddExt( p_playlist, *ppsz_uri, *ppsz_name,
+ PLAYLIST_INSERT, *pi_position,
+ -1, (const char **)*pppsz_options, *pi_options );
+
+ (*pi_position)++;
+ if( *ppsz_name ) free( *ppsz_name ); *ppsz_name = NULL;
+ free( *ppsz_uri ); *ppsz_uri = NULL;
+
+ for( ; *pi_options; (*pi_options)-- )
+ {
+ free( (*pppsz_options)[*pi_options - 1] );
+ if( *pi_options == 1 ) free( *pppsz_options );
+ }
+ *pppsz_options = NULL;
+ }
+}
+
+/*****************************************************************************
+ * Demux: reads and demuxes data packets
+ *****************************************************************************
+ * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
+ *****************************************************************************/
+static int Demux( demux_t *p_demux )
+{
+ demux_sys_t *p_m3u = p_demux->p_sys;
+
+ char psz_line[MAX_LINE];
+ char p_buf[MAX_LINE], eol_tok;
+ int i_size, i_bufpos, i_linepos = 0;
+ playlist_t *p_playlist;
+ vlc_bool_t b_discard = VLC_FALSE;
+
+ char *psz_name = NULL;
+ char *psz_uri = NULL;
+ int i_options = 0;
+ char **ppsz_options = NULL;
+
+ int i_position;
+
+ p_playlist = (playlist_t *) vlc_object_find( p_demux, VLC_OBJECT_PLAYLIST,
+ FIND_ANYWHERE );
+ if( !p_playlist )
+ {
+ msg_Err( p_demux, "can't find playlist" );
+ return -1;
+ }
+
+ p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
+ i_position = p_playlist->i_index + 1;
+
+ /* Depending on wether we are dealing with an m3u/asf file, the end of
+ * line token will be different */
+ if( p_m3u->i_type == TYPE_ASX || p_m3u->i_type == TYPE_HTML )
+ eol_tok = '>';
+ else
+ eol_tok = '\n';
+
+ while( ( i_size = stream_Read( p_demux->s, p_buf, MAX_LINE ) ) )
+ {
+ i_bufpos = 0;
+
+ while( i_size )
+ {
+ /* Build a line < MAX_LINE */
+ while( p_buf[i_bufpos] != eol_tok && i_size )
+ {
+ if( i_linepos == MAX_LINE || b_discard == VLC_TRUE )