]> git.sesse.net Git - vlc/blob - modules/demux/playlist/asx.c
Fix a bunch of gcc warnings
[vlc] / modules / demux / playlist / asx.c
1 /*****************************************************************************
2  * asx.c : ASX playlist format import
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /* See also: http://msdn.microsoft.com/library/en-us/wmplay10/mmp_sdk/windowsmediametafilereference.asp
25  */
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #define _GNU_SOURCE
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <ctype.h>                                              /* isspace() */
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36
37 #include <errno.h>                                                 /* ENOMEM */
38 #include "charset.h"
39 #include "playlist.h"
40 #include "vlc_meta.h"
41
42 struct demux_sys_t
43 {
44     char    *psz_prefix;
45     char    *psz_data;
46     int64_t i_data_len;
47     vlc_bool_t b_utf8;
48 };
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int Demux( demux_t *p_demux);
54 static int Control( demux_t *p_demux, int i_query, va_list args );
55
56 static int StoreString( demux_t *p_demux, char **ppsz_string, char *psz_source_start, char *psz_source_end )
57 {
58     demux_sys_t *p_sys = p_demux->p_sys;
59     int i_strlen = psz_source_end-psz_source_start;
60     if( i_strlen < 1 )
61         return VLC_EGENERIC;
62
63     if( *ppsz_string ) free( *ppsz_string );
64     *ppsz_string = malloc( i_strlen*sizeof( char ) +1);
65     memcpy( *ppsz_string, psz_source_start, i_strlen );
66     (*ppsz_string)[i_strlen] = '\0';
67
68     if( p_sys->b_utf8 )
69         EnsureUTF8( *ppsz_string );
70     else
71     {
72         char *psz_temp;
73         psz_temp = FromLocaleDup( *ppsz_string );
74         if( psz_temp )
75         {
76             free( *ppsz_string );
77             *ppsz_string = psz_temp;
78         } else EnsureUTF8( *ppsz_string );
79     }
80     return VLC_SUCCESS;
81 }
82
83 /*****************************************************************************
84  * Import_ASX: main import function
85  *****************************************************************************/
86 int E_(Import_ASX)( vlc_object_t *p_this )
87 {
88     demux_t *p_demux = (demux_t *)p_this;
89     uint8_t *p_peek, *p_peek_stop;
90     CHECK_PEEK( p_peek, 10 );
91
92     p_peek_stop = p_peek+6;
93
94     // skip over possible leading empty lines
95     while( (p_peek < p_peek_stop) && (*p_peek == '\n' || *p_peek == '\r')) ++p_peek;
96
97     if( POKE( p_peek, "<asx", 4 ) || isExtension( p_demux, ".asx" ) ||
98         isExtension( p_demux, ".wax" ) || isExtension( p_demux, ".wvx" ) ||
99         isDemux( p_demux, "asx-open" ) )
100     {
101         ;
102     }
103     else
104         return VLC_EGENERIC;
105     
106     STANDARD_DEMUX_INIT_MSG( "found valid ASX playlist" );
107     p_demux->p_sys->psz_prefix = E_(FindPrefix)( p_demux );
108     p_demux->p_sys->psz_data = NULL;
109     p_demux->p_sys->i_data_len = -1;
110     p_demux->p_sys->b_utf8 = VLC_FALSE;
111     
112     return VLC_SUCCESS;
113 }
114
115 /*****************************************************************************
116  * Deactivate: frees unused data
117  *****************************************************************************/
118 void E_(Close_ASX)( vlc_object_t *p_this )
119 {
120     demux_t *p_demux = (demux_t *)p_this;
121     demux_sys_t *p_sys = p_demux->p_sys;
122
123     if( p_sys->psz_prefix ) free( p_sys->psz_prefix );
124     if( p_sys->psz_data ) free( p_sys->psz_data );
125     free( p_sys );
126 }
127
128 static int Demux( demux_t *p_demux )
129 {
130     demux_sys_t *p_sys = p_demux->p_sys;
131     char        *psz_parse = NULL;
132     char        *psz_backup = NULL;
133     vlc_bool_t  b_entry = VLC_FALSE;
134
135     INIT_PLAYLIST_STUFF;
136
137     /* init txt */
138     if( p_sys->i_data_len < 0 )
139     {
140         int64_t i_pos = 0;
141         p_sys->i_data_len = stream_Size( p_demux->s ) +1; /* This is a cheat to prevent unnecessary realloc */
142         if( p_sys->i_data_len <= 0 && p_sys->i_data_len < 16384 ) p_sys->i_data_len = 1024;
143         p_sys->psz_data = malloc( p_sys->i_data_len * sizeof(char) +1);
144         
145         /* load the complete file */
146         for( ;; )
147         {
148             int i_read = stream_Read( p_demux->s, &p_sys->psz_data[i_pos], p_sys->i_data_len - i_pos );
149             p_sys->psz_data[i_read] = '\0';
150            
151             if( i_read < p_sys->i_data_len - i_pos ) break; /* Done */
152             
153             i_pos += i_read;
154             p_sys->i_data_len += 1024;
155             p_sys->psz_data = realloc( p_sys->psz_data, p_sys->i_data_len * sizeof( char * ) +1 );
156         }
157         if( p_sys->i_data_len <= 0 ) return VLC_EGENERIC;
158     }
159
160     psz_parse = p_sys->psz_data;
161     /* Find first element */
162     if( ( psz_parse = strcasestr( psz_parse, "<ASX" ) ) )
163     {
164         /* ASX element */
165         char *psz_string = NULL;
166         int i_strlen = 0;
167
168         char *psz_base_asx = NULL;
169         char *psz_title_asx = NULL;
170         char *psz_artist_asx = NULL;
171         char *psz_copyright_asx = NULL;
172         char *psz_moreinfo_asx = NULL;
173         char *psz_abstract_asx = NULL;
174         
175         char *psz_base_entry = NULL;
176         char *psz_title_entry = NULL;
177         char *psz_artist_entry = NULL;
178         char *psz_copyright_entry = NULL;
179         char *psz_moreinfo_entry = NULL;
180         char *psz_abstract_entry = NULL;
181         int i_entry_count = 0;
182     
183         psz_parse = strcasestr( psz_parse, ">" );
184
185         while( ( psz_parse = strcasestr( psz_parse, "<" ) ) && psz_parse && *psz_parse )
186         {
187             if( !strncasecmp( psz_parse, "<!--", 4 ) )
188             {
189                 /* this is a comment */
190                 if( ( psz_parse = strcasestr( psz_parse, "-->" ) ) )
191                     psz_parse+=3;
192                 else continue;
193             }
194             else if( !strncasecmp( psz_parse, "<PARAM ", 7 ) )
195             {
196                 vlc_bool_t b_encoding_flag = VLC_FALSE;
197                 psz_parse+=7;
198                 if( !strncasecmp( psz_parse, "name", 4 ) )
199                 {
200                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
201                     {
202                         psz_backup = ++psz_parse;
203                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
204                         {
205                             i_strlen = psz_parse-psz_backup;
206                             if( i_strlen < 1 ) continue;
207                             msg_Dbg( p_demux, "param name strlen: %d", i_strlen);
208                             psz_string = malloc( i_strlen *sizeof( char ) +1);
209                             memcpy( psz_string, psz_backup, i_strlen );
210                             psz_string[i_strlen] = '\0';
211                             msg_Dbg( p_demux, "param name: %s", psz_string);
212                             b_encoding_flag = !strcasecmp( psz_string, "encoding" );
213                             free( psz_string );
214                         }
215                         else continue;
216                     }
217                     else continue;
218                 }
219                 psz_parse++;
220                 if( !strncasecmp( psz_parse, "value", 5 ) )
221                 {
222                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
223                     {
224                         psz_backup = ++psz_parse;
225                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
226                         {
227                             i_strlen = psz_parse-psz_backup;
228                             if( i_strlen < 1 ) continue;
229                             msg_Dbg( p_demux, "param value strlen: %d", i_strlen);
230                             psz_string = malloc( i_strlen *sizeof( char ) +1);
231                             memcpy( psz_string, psz_backup, i_strlen );
232                             psz_string[i_strlen] = '\0';
233                             msg_Dbg( p_demux, "param value: %s", psz_string);
234                             if( b_encoding_flag && !strcasecmp( psz_string, "utf-8" ) ) p_sys->b_utf8 = VLC_TRUE;
235                             free( psz_string );
236                         }
237                         else continue;
238                     }
239                     else continue;
240                 }
241                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
242                     psz_parse += 2;
243                 else continue;
244             }
245             else if( !strncasecmp( psz_parse, "<BANNER", 7 ) )
246             {
247                 /* We skip this element */
248                 if( ( psz_parse = strcasestr( psz_parse, "</BANNER>" ) ) )
249                     psz_parse += 9;
250                 else continue;
251             }
252             else if( !strncasecmp( psz_parse, "<PREVIEWDURATION", 16 ) ||
253                      !strncasecmp( psz_parse, "<LOGURL", 7 ) ||
254                      !strncasecmp( psz_parse, "<Skin", 5 ) )
255             {
256                 /* We skip this element */
257                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
258                     psz_parse += 2;
259                 else continue;
260             }
261             else if( !strncasecmp( psz_parse, "<BASE ", 6 ) )
262             {
263                 psz_parse+=6;
264                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
265                 {
266                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
267                     {
268                         psz_backup = ++psz_parse;
269                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
270                         {
271                             StoreString( p_demux, (b_entry ? &psz_base_entry : &psz_base_asx), psz_backup, psz_parse );
272                         }
273                         else continue;
274                     }
275                     else continue;
276                 }
277                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
278                     psz_parse += 2;
279                 else continue;
280             }
281             else if( !strncasecmp( psz_parse, "<TITLE>", 7 ) )
282             {
283                 psz_backup = psz_parse+=7;
284                 if( ( psz_parse = strcasestr( psz_parse, "</TITLE>" ) ) )
285                 {
286                     StoreString( p_demux, (b_entry ? &psz_title_entry : &psz_title_asx), psz_backup, psz_parse );
287                     psz_parse += 8;
288                 }
289                 else continue;
290             }
291             else if( !strncasecmp( psz_parse, "<Author>", 8 ) )
292             {
293                 psz_backup = psz_parse+=8;
294                 if( ( psz_parse = strcasestr( psz_parse, "</Author>" ) ) )
295                 {
296                     StoreString( p_demux, (b_entry ? &psz_artist_entry : &psz_artist_asx), psz_backup, psz_parse );
297                     psz_parse += 9;
298                 }
299                 else continue;
300             }
301             else if( !strncasecmp( psz_parse, "<Copyright", 10 ) )
302             {
303                 psz_backup = psz_parse+=11;
304                 if( ( psz_parse = strcasestr( psz_parse, "</Copyright>" ) ) )
305                 {
306                     StoreString( p_demux, (b_entry ? &psz_copyright_entry : &psz_copyright_asx), psz_backup, psz_parse );
307                     psz_parse += 12;
308                 }
309                 else continue;
310             }
311             else if( !strncasecmp( psz_parse, "<MoreInfo ", 10 ) )
312             {
313                 psz_parse+=10;
314                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
315                 {
316                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
317                     {
318                         psz_backup = ++psz_parse;
319                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
320                         {
321                             StoreString( p_demux, (b_entry ? &psz_moreinfo_entry : &psz_moreinfo_asx), psz_backup, psz_parse );
322                         }
323                         else continue;
324                     }
325                     else continue;
326                 }
327                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
328                     psz_parse += 2;
329                 else continue;
330             }
331             else if( !strncasecmp( psz_parse, "<ABSTRACT>", 10 ) )
332             {
333                 psz_backup = psz_parse+=10;
334                 if( ( psz_parse = strcasestr( psz_parse, "</ABSTRACT>" ) ) )
335                 {
336                     StoreString( p_demux, (b_entry ? &psz_abstract_entry : &psz_abstract_asx), psz_backup, psz_parse );
337                     psz_parse += 11;
338                 }
339                 else continue;
340             }
341             else if( !strncasecmp( psz_parse, "<EntryRef ", 10 ) )
342             {
343                 psz_parse+=10;
344                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
345                 {
346                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
347                     {
348                         psz_backup = ++psz_parse;
349                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
350                         {
351                             i_strlen = psz_parse-psz_backup;
352                             if( i_strlen < 1 ) continue;
353                             psz_string = malloc( i_strlen*sizeof( char ) +1);
354                             memcpy( psz_string, psz_backup, i_strlen );
355                             psz_string[i_strlen] = '\0';
356                             p_input = input_ItemNew( p_playlist, psz_string, psz_title_asx );
357                             input_ItemCopyOptions( p_current->p_input, p_input );
358                             playlist_AddWhereverNeeded( p_playlist, p_input, p_current,
359                                  p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
360                                  PLAYLIST_APPEND );
361                             free( psz_string );
362                         }
363                         else continue;
364                     }
365                     else continue;
366                 }
367                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
368                     psz_parse += 2;
369                 else continue;
370             }
371             else if( !strncasecmp( psz_parse, "</Entry>", 8 ) )
372             {
373                 /* add a new entry */
374                 psz_parse+=8;
375                 if( !b_entry )
376                 {
377                     msg_Err( p_demux, "end of entry without start?" );
378                     continue;
379                 }
380                 /* cleanup entry */
381                 FREENULL( psz_title_entry )
382                 FREENULL( psz_base_entry )
383                 FREENULL( psz_artist_entry )
384                 FREENULL( psz_copyright_entry )
385                 FREENULL( psz_moreinfo_entry )
386                 FREENULL( psz_abstract_entry )
387                 b_entry = VLC_FALSE;
388             }
389             else if( !strncasecmp( psz_parse, "<Entry", 6 ) )
390             {
391                 psz_parse+=6;
392                 if( b_entry )
393                 {
394                     msg_Err( p_demux, "We already are in an entry section" );
395                     continue;
396                 }
397                 i_entry_count += 1;
398                 b_entry = VLC_TRUE;
399                 psz_parse = strcasestr( psz_parse, ">" );
400             }
401             else if( !strncasecmp( psz_parse, "<Ref ", 5 ) )
402             {
403                 psz_parse+=5;
404                 if( !b_entry )
405                 {
406                     msg_Err( p_demux, "A ref outside an entry section" );
407                     continue;
408                 }
409                 
410                 if( !strncasecmp( psz_parse, "HREF", 4 ) )
411                 {
412                     if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
413                     {
414                         psz_backup = ++psz_parse;
415                         if( ( psz_parse = strcasestr( psz_parse, "\"" ) ) )
416                         {
417                             input_item_t *p_entry = NULL;
418                             char *psz_name = NULL;
419                             i_strlen = psz_parse-psz_backup;
420                             if( i_strlen < 1 ) continue;
421                             psz_string = malloc( i_strlen*sizeof( char ) +1);
422                             memcpy( psz_string, psz_backup, i_strlen );
423                             psz_string[i_strlen] = '\0';
424
425                             /* create the new entry */
426                             asprintf( &psz_name, "%d %s", i_entry_count, ( psz_title_entry ? psz_title_entry : p_current->p_input->psz_name ) );
427                             p_entry = input_ItemNew( p_playlist, psz_string, psz_name );
428                             FREENULL( psz_name );
429                             
430                             input_ItemCopyOptions( p_current->p_input, p_entry );
431                             p_entry->p_meta = vlc_meta_New();
432                             if( psz_title_entry ) vlc_meta_SetTitle( p_entry->p_meta, psz_title_entry );
433                             if( psz_artist_entry ) vlc_meta_SetArtist( p_entry->p_meta, psz_artist_entry );
434                             if( psz_copyright_entry ) vlc_meta_SetCopyright( p_entry->p_meta, psz_copyright_entry );
435                             if( psz_moreinfo_entry ) vlc_meta_SetURL( p_entry->p_meta, psz_moreinfo_entry );
436                             if( psz_abstract_entry ) vlc_meta_SetDescription( p_entry->p_meta, psz_abstract_entry );
437                             
438                             playlist_AddWhereverNeeded( p_playlist, p_entry, p_current,
439                                 p_item_in_category, (i_parent_id > 0 )? VLC_TRUE : VLC_FALSE,
440                                 PLAYLIST_APPEND );
441                             free( psz_string );
442                         }
443                         else continue;
444                     }
445                     else continue;
446                 }
447                 if( ( psz_parse = strcasestr( psz_parse, "/>" ) ) )
448                     psz_parse += 2;
449                 else continue;
450             }
451             else if( !strncasecmp( psz_parse, "</ASX", 5 ) )
452             {
453                 vlc_mutex_lock( &p_current->p_input->lock );
454                 if( !p_current->p_input->p_meta ) p_current->p_input->p_meta = vlc_meta_New();
455                 if( psz_title_asx ) vlc_meta_SetTitle( p_current->p_input->p_meta, psz_title_asx );
456                 if( psz_artist_asx ) vlc_meta_SetArtist( p_current->p_input->p_meta, psz_artist_asx );
457                 if( psz_copyright_asx ) vlc_meta_SetCopyright( p_current->p_input->p_meta, psz_copyright_asx );
458                 if( psz_moreinfo_asx ) vlc_meta_SetURL( p_current->p_input->p_meta, psz_moreinfo_asx );
459                 if( psz_abstract_asx ) vlc_meta_SetDescription( p_current->p_input->p_meta, psz_abstract_asx );
460                 vlc_mutex_unlock( &p_current->p_input->lock );
461                 FREENULL( psz_base_asx );
462                 FREENULL( psz_title_asx );
463                 FREENULL( psz_artist_asx );
464                 FREENULL( psz_copyright_asx );
465                 FREENULL( psz_moreinfo_asx );
466                 FREENULL( psz_abstract_asx );
467                 psz_parse++;
468             }
469             else psz_parse++;
470         }
471 #if 0
472 /* FIXME Unsupported elements */
473             PARAM
474             EVENT
475             REPEAT
476             DURATION
477             ENDMARK
478             STARTMARK
479             STARTTIME
480 #endif
481     }
482     HANDLE_PLAY_AND_RELEASE;
483     return VLC_SUCCESS;
484 }
485
486 static int Control( demux_t *p_demux, int i_query, va_list args )
487 {
488     return VLC_EGENERIC;
489 }