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