]> git.sesse.net Git - vlc/blob - modules/demux/playlist/xspf.c
xspf: silently skip unknown extension nodes (fix #3696)
[vlc] / modules / demux / playlist / xspf.c
1 /*******************************************************************************
2  * xspf.c : XSPF playlist import functions
3  *******************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Daniel Stränger <vlc at schmaller dot de>
8  *          Yoann Peronneau <yoann@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  ******************************************************************************/
24 /**
25  * \file modules/demux/playlist/xspf.c
26  * \brief XSPF playlist import functions
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_demux.h>
35
36 #include <vlc_xml.h>
37 #include <vlc_strings.h>
38 #include <vlc_url.h>
39 #include "xspf.h"
40 #include "playlist.h"
41
42 struct demux_sys_t
43 {
44     input_item_t **pp_tracklist;
45     int i_tracklist_entries;
46     int i_track_id;
47     char * psz_base;
48 };
49
50 static int Control( demux_t *, int, va_list );
51 static int Demux( demux_t * );
52
53 /**
54  * \brief XSPF submodule initialization function
55  */
56 int Import_xspf( vlc_object_t *p_this )
57 {
58     DEMUX_BY_EXTENSION_OR_FORCED_MSG( ".xspf", "xspf-open",
59                                       "using XSPF playlist reader" );
60     return VLC_SUCCESS;
61 }
62
63 void Close_xspf( vlc_object_t *p_this )
64 {
65     demux_t *p_demux = (demux_t *)p_this;
66     demux_sys_t *p_sys = p_demux->p_sys;
67     for( int i = 0; i < p_sys->i_tracklist_entries; i++ )
68     {
69         if( p_sys->pp_tracklist[i] )
70             vlc_gc_decref( p_sys->pp_tracklist[i] );
71     }
72     free( p_sys->pp_tracklist );
73     free( p_sys->psz_base );
74     free( p_sys );
75 }
76
77 /**
78  * \brief demuxer function for XSPF parsing
79  */
80 int Demux( demux_t *p_demux )
81 {
82     int i_ret = -1;
83     xml_t *p_xml = NULL;
84     xml_reader_t *p_xml_reader = NULL;
85     char *psz_name = NULL;
86     input_item_t *p_current_input = GetCurrentItem(p_demux);
87     p_demux->p_sys->pp_tracklist = NULL;
88     p_demux->p_sys->i_tracklist_entries = 0;
89     p_demux->p_sys->i_track_id = -1;
90     p_demux->p_sys->psz_base = NULL;
91
92     /* create new xml parser from stream */
93     p_xml = xml_Create( p_demux );
94     if( !p_xml )
95         goto end;
96
97     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
98     if( !p_xml_reader )
99         goto end;
100
101     /* locating the root node */
102     do
103     {
104         if( xml_ReaderRead( p_xml_reader ) != 1 )
105         {
106             msg_Err( p_demux, "can't read xml stream" );
107             goto end;
108         }
109     } while( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM );
110
111     /* checking root node name */
112     psz_name = xml_ReaderName( p_xml_reader );
113     if( !psz_name || strcmp( psz_name, "playlist" ) )
114     {
115         msg_Err( p_demux, "invalid root node name: %s", psz_name );
116         free( psz_name );
117         goto end;
118     }
119     free( psz_name );
120
121     input_item_node_t *p_subitems =
122         input_item_node_Create( p_current_input );
123
124     i_ret = parse_playlist_node( p_demux, p_subitems,
125                                  p_xml_reader, "playlist" ) ? 0 : -1;
126
127     for( int i = 0 ; i < p_demux->p_sys->i_tracklist_entries ; i++ )
128     {
129         input_item_t *p_new_input = p_demux->p_sys->pp_tracklist[i];
130         if( p_new_input )
131         {
132             input_item_node_AppendItem( p_subitems, p_new_input );
133         }
134     }
135
136     input_item_node_PostAndDelete( p_subitems );
137
138 end:
139     vlc_gc_decref(p_current_input);
140     if( p_xml_reader )
141         xml_ReaderDelete( p_xml, p_xml_reader );
142     if( p_xml )
143         xml_Delete( p_xml );
144     return i_ret; /* Needed for correct operation of go back */
145 }
146
147 /** \brief dummy function for demux callback interface */
148 static int Control( demux_t *p_demux, int i_query, va_list args )
149 {
150     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
151     return VLC_EGENERIC;
152 }
153
154 /**
155  * \brief parse the root node of a XSPF playlist
156  * \param p_demux demuxer instance
157  * \param p_input_item current input item
158  * \param p_xml_reader xml reader instance
159  * \param psz_element name of element to parse
160  */
161 static bool parse_playlist_node COMPLEX_INTERFACE
162 {
163     input_item_t *p_input_item = p_input_node->p_item;
164     char *psz_name = NULL;
165     char *psz_value = NULL;
166     bool b_version_found = false;
167     int i_node;
168     bool b_ret = false;
169     xml_elem_hnd_t *p_handler = NULL;
170
171     xml_elem_hnd_t pl_elements[] =
172         { {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
173           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
174           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
175           {"info",         SIMPLE_CONTENT,  {NULL} },
176           {"location",     SIMPLE_CONTENT,  {NULL} },
177           {"identifier",   SIMPLE_CONTENT,  {NULL} },
178           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
179           {"date",         SIMPLE_CONTENT,  {NULL} },
180           {"license",      SIMPLE_CONTENT,  {NULL} },
181           {"attribution",  COMPLEX_CONTENT, {.cmplx = skip_element} },
182           {"link",         SIMPLE_CONTENT,  {NULL} },
183           {"meta",         SIMPLE_CONTENT,  {NULL} },
184           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
185           {"trackList",    COMPLEX_CONTENT, {.cmplx = parse_tracklist_node} },
186           {NULL,           UNKNOWN_CONTENT, {NULL} }
187         };
188
189     /* read all playlist attributes */
190     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
191     {
192         psz_name = xml_ReaderName( p_xml_reader );
193         psz_value = xml_ReaderValue( p_xml_reader );
194         if( !psz_name || !psz_value )
195         {
196             msg_Err( p_demux, "invalid xml stream @ <playlist>" );
197             goto end;
198         }
199         /* attribute: version */
200         if( !strcmp( psz_name, "version" ) )
201         {
202             b_version_found = true;
203             if( strcmp( psz_value, "0" ) && strcmp( psz_value, "1" ) )
204                 msg_Warn( p_demux, "unsupported XSPF version" );
205         }
206         /* attribute: xmlns */
207         else if( !strcmp( psz_name, "xmlns" ) || !strcmp( psz_name, "xmlns:vlc" ) )
208             ;
209         else if( !strcmp( psz_name, "xml:base" ) )
210         {
211             p_demux->p_sys->psz_base = strdup( psz_value );
212         }
213         /* unknown attribute */
214         else
215             msg_Warn( p_demux, "invalid <playlist> attribute:\"%s\"", psz_name);
216
217         free( psz_name );
218         free( psz_value );
219     }
220     /* attribute version is mandatory !!! */
221     if( !b_version_found )
222         msg_Warn( p_demux, "<playlist> requires \"version\" attribute" );
223
224     /* parse the child elements - we only take care of <trackList> */
225     psz_name = NULL;
226     psz_value = NULL;
227     while( xml_ReaderRead( p_xml_reader ) == 1 )
228     {
229         i_node = xml_ReaderNodeType( p_xml_reader );
230         switch( i_node )
231         {
232             case XML_READER_NONE:
233                 break;
234             case XML_READER_STARTELEM:
235                 /*  element start tag  */
236                 psz_name = xml_ReaderName( p_xml_reader );
237                 if( !psz_name || !*psz_name )
238                 {
239                     msg_Err( p_demux, "invalid xml stream" );
240                     goto end;
241                 }
242                 /* choose handler */
243                 for( p_handler = pl_elements;
244                      p_handler->name && strcmp( psz_name, p_handler->name );
245                      p_handler++ );
246                 if( !p_handler->name )
247                 {
248                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
249                     goto end;
250                 }
251                 FREE_NAME();
252                 /* complex content is parsed in a separate function */
253                 if( p_handler->type == COMPLEX_CONTENT )
254                 {
255                     FREE_VALUE();
256                     if( p_handler->pf_handler.cmplx( p_demux,
257                                                      p_input_node,
258                                                      p_xml_reader,
259                                                      p_handler->name ) )
260                     {
261                         p_handler = NULL;
262                     }
263                     else
264                     {
265                         return false;
266                     }
267                 }
268                 break;
269
270             case XML_READER_TEXT:
271                 /* simple element content */
272                 free( psz_value );
273                 psz_value = xml_ReaderValue( p_xml_reader );
274                 if( !psz_value )
275                 {
276                     msg_Err( p_demux, "invalid xml stream" );
277                     goto end;
278                 }
279                 break;
280
281             case XML_READER_ENDELEM:
282                 /* element end tag */
283                 psz_name = xml_ReaderName( p_xml_reader );
284                 if( !psz_name )
285                 {
286                     msg_Err( p_demux, "invalid xml stream" );
287                     goto end;
288                 }
289                 /* leave if the current parent node <playlist> is terminated */
290                 if( !strcmp( psz_name, psz_element ) )
291                 {
292                     b_ret = true;
293                     goto end;
294                 }
295                 /* there MUST have been a start tag for that element name */
296                 if( !p_handler || !p_handler->name
297                     || strcmp( p_handler->name, psz_name ))
298                 {
299                     msg_Err( p_demux, "there's no open element left for <%s>",
300                              psz_name );
301                     goto end;
302                 }
303
304                 if( p_handler->pf_handler.smpl )
305                 {
306                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
307                                                 psz_value );
308                 }
309                 FREE_ATT();
310                 p_handler = NULL;
311                 break;
312
313             default:
314                 /* unknown/unexpected xml node */
315                 msg_Err( p_demux, "unexpected xml node %i", i_node );
316                 goto end;
317         }
318     }
319
320 end:
321     free( psz_name );
322     free( psz_value );
323     return b_ret;
324 }
325
326 /**
327  * \brief parses the tracklist node which only may contain <track>s
328  */
329 static bool parse_tracklist_node COMPLEX_INTERFACE
330 {
331     VLC_UNUSED(psz_element);
332     char *psz_name;
333     int i_ntracks = 0;
334
335     /* now parse the <track>s */
336     while( xml_ReaderRead( p_xml_reader ) == 1 )
337     {
338         int i_node = xml_ReaderNodeType( p_xml_reader );
339         if( i_node == XML_READER_STARTELEM )
340         {
341             char *psz_eltname = xml_ReaderName( p_xml_reader );
342             if( !psz_eltname )
343             {
344                 msg_Err( p_demux, "unexpected end of xml data" );
345                 free( psz_eltname );
346                 return false;
347             }
348             if( strcmp( psz_eltname, "track") )
349             {
350                 msg_Err( p_demux, "unexpected child of <trackList>: <%s>",
351                          psz_eltname );
352                 free( psz_eltname );
353                 return false;
354             }
355             free( psz_eltname );
356
357             /* parse the track data in a separate function */
358             if( parse_track_node( p_demux, p_input_node,
359                                   p_xml_reader, "track" ) )
360                 i_ntracks++;
361         }
362         else if( i_node == XML_READER_ENDELEM )
363             break;
364     }
365
366     /* the <trackList> has to be terminated */
367     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_ENDELEM )
368     {
369         msg_Err( p_demux, "there's a missing </trackList>" );
370         return false;
371     }
372     psz_name = xml_ReaderName( p_xml_reader );
373     if( !psz_name || strcmp( psz_name, "trackList" ) )
374     {
375         msg_Err( p_demux, "expected: </trackList>, found: </%s>", psz_name );
376         free( psz_name );
377         return false;
378     }
379     free( psz_name );
380
381     msg_Dbg( p_demux, "parsed %i tracks successfully", i_ntracks );
382     return true;
383 }
384
385 /**
386  * \brief parse one track element
387  * \param COMPLEX_INTERFACE
388  */
389 static bool parse_track_node COMPLEX_INTERFACE
390 {
391     input_item_t *p_input_item = p_input_node->p_item;
392     char *psz_name = NULL;
393     char *psz_value = NULL;
394     xml_elem_hnd_t *p_handler = NULL;
395     demux_sys_t *p_sys = p_demux->p_sys;
396     bool b_ret = false;
397
398     xml_elem_hnd_t track_elements[] =
399         { {"location",     SIMPLE_CONTENT,  {NULL} },
400           {"identifier",   SIMPLE_CONTENT,  {NULL} },
401           {"title",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
402           {"creator",      SIMPLE_CONTENT,  {.smpl = set_item_info} },
403           {"annotation",   SIMPLE_CONTENT,  {.smpl = set_item_info} },
404           {"info",         SIMPLE_CONTENT,  {NULL} },
405           {"image",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
406           {"album",        SIMPLE_CONTENT,  {.smpl = set_item_info} },
407           {"trackNum",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
408           {"duration",     SIMPLE_CONTENT,  {.smpl = set_item_info} },
409           {"link",         SIMPLE_CONTENT,  {NULL} },
410           {"meta",         SIMPLE_CONTENT,  {NULL} },
411           {"extension",    COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
412           {NULL,           UNKNOWN_CONTENT, {NULL} }
413         };
414
415     input_item_t *p_new_input = input_item_New( p_demux, NULL, NULL );
416     input_item_node_t *p_new_node = input_item_node_Create( p_new_input );
417
418     if( !p_new_input )
419     {
420         /* malloc has failed for input_item_New, so bailout early */
421         return false;
422     }
423
424     /* reset i_track_id */
425     p_sys->i_track_id = -1;
426
427     while( xml_ReaderRead( p_xml_reader ) == 1 )
428     {
429         int i_node = xml_ReaderNodeType( p_xml_reader );
430         switch( i_node )
431         {
432             case XML_READER_NONE:
433                 break;
434
435             case XML_READER_STARTELEM:
436                 /*  element start tag  */
437                 psz_name = xml_ReaderName( p_xml_reader );
438                 if( !psz_name || !*psz_name )
439                 {
440                     msg_Err( p_demux, "invalid xml stream" );
441                     goto end;
442                 }
443                 /* choose handler */
444                 for( p_handler = track_elements;
445                      p_handler->name && strcmp( psz_name, p_handler->name );
446                      p_handler++ );
447                 if( !p_handler->name )
448                 {
449                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
450                     goto end;
451                 }
452                 FREE_NAME();
453                 /* complex content is parsed in a separate function */
454                 if( p_handler->type == COMPLEX_CONTENT )
455                 {
456                     FREE_VALUE();
457
458                     bool b_res = p_handler->pf_handler.cmplx( p_demux,
459                                                         p_new_node,
460                                                         p_xml_reader,
461                                                         p_handler->name );
462                     if( b_res )
463                     {
464                         p_handler = NULL;
465                     }
466                     else
467                     {
468                         return false;
469                     }
470                 }
471                 break;
472
473             case XML_READER_TEXT:
474                 /* simple element content */
475                 free( psz_value );
476                 psz_value = xml_ReaderValue( p_xml_reader );
477                 if( !psz_value )
478                 {
479                     msg_Err( p_demux, "invalid xml stream" );
480                     goto end;
481                 }
482                 break;
483
484             case XML_READER_ENDELEM:
485                 /* element end tag */
486                 psz_name = xml_ReaderName( p_xml_reader );
487                 if( !psz_name )
488                 {
489                     msg_Err( p_demux, "invalid xml stream" );
490                     goto end;
491                 }
492
493                 /* leave if the current parent node <track> is terminated */
494                 if( !strcmp( psz_name, psz_element ) )
495                 {
496                     free( psz_name );
497                     free( psz_value );
498
499                     /* Make sure we have a URI */
500                     char *psz_uri = input_item_GetURI( p_new_input );
501                     if( !psz_uri )
502                     {
503                         input_item_SetURI( p_new_input, "vlc://nop" );
504                     }
505                     free( psz_uri );
506
507                     if( p_sys->i_track_id < 0 )
508                     {
509                         input_item_node_AppendNode( p_input_node, p_new_node );
510                         vlc_gc_decref( p_new_input );
511                         return true;
512                     }
513
514                     if( p_sys->i_track_id >= p_sys->i_tracklist_entries )
515                     {
516                         input_item_t **pp;
517                         pp = realloc( p_sys->pp_tracklist,
518                             (p_sys->i_track_id + 1) * sizeof(*pp) );
519                         if( !pp )
520                             return false;
521                         p_sys->pp_tracklist = pp;
522                         while( p_sys->i_track_id >= p_sys->i_tracklist_entries )
523                             pp[p_sys->i_tracklist_entries++] = NULL;
524                     }
525
526                     p_sys->pp_tracklist[ p_sys->i_track_id ] = p_new_input;
527                     return true;
528                 }
529                 /* there MUST have been a start tag for that element name */
530                 if( !p_handler || !p_handler->name
531                     || strcmp( p_handler->name, psz_name ))
532                 {
533                     msg_Err( p_demux, "there's no open element left for <%s>",
534                              psz_name );
535                     goto end;
536                 }
537
538                 /* special case: location */
539                 if( !strcmp( p_handler->name, "location" ) )
540                 {
541                     if( psz_value == NULL )
542                         input_item_SetURI( p_new_input, "vlc://nop" );
543                     else
544                     /* FIXME: This is broken. Scheme-relative (//...) locations
545                      * and anchors (#...) are not resolved correctly. Also,
546                      * host-relative (/...) and directory-relative locations
547                      * ("relative path" in vernacular) should be resolved.
548                      * Last, psz_base should default to the XSPF resource
549                      * location if missing (not the current working directory).
550                      * -- Courmisch */
551                     if( p_sys->psz_base && !strstr( psz_value, "://" ) )
552                     {
553                         char* psz_tmp;
554                         if( asprintf( &psz_tmp, "%s%s", p_sys->psz_base,
555                                       psz_value ) == -1 )
556                         {
557                             goto end;
558                         }
559                         input_item_SetURI( p_new_input, psz_tmp );
560                         free( psz_tmp );
561                     }
562                     else
563                         input_item_SetURI( p_new_input, psz_value );
564                     input_item_CopyOptions( p_input_item, p_new_input );
565                 }
566                 else
567                 {
568                     /* there MUST be an item */
569                     if( p_handler->pf_handler.smpl )
570                     {
571                         p_handler->pf_handler.smpl( p_new_input,
572                                                     p_handler->name,
573                                                     psz_value );
574                         FREE_ATT();
575                     }
576                 }
577                 FREE_ATT();
578                 p_handler = NULL;
579                 break;
580
581             default:
582                 /* unknown/unexpected xml node */
583                 msg_Err( p_demux, "unexpected xml node %i", i_node );
584                 goto end;
585         }
586     }
587     msg_Err( p_demux, "unexpected end of xml data" );
588
589 end:
590     free( psz_name );
591     free( psz_value );
592     return b_ret;
593 }
594
595 /**
596  * \brief handles the supported <track> sub-elements
597  */
598 static bool set_item_info SIMPLE_INTERFACE
599 {
600     /* exit if setting is impossible */
601     if( !psz_name || !psz_value || !p_input )
602         return false;
603
604     /* re-convert xml special characters inside psz_value */
605     resolve_xml_special_chars( psz_value );
606
607     /* handle each info element in a separate "if" clause */
608     if( !strcmp( psz_name, "title" ) )
609     {
610         input_item_SetTitle( p_input, psz_value );
611     }
612     else if( !strcmp( psz_name, "creator" ) )
613     {
614         input_item_SetArtist( p_input, psz_value );
615     }
616     else if( !strcmp( psz_name, "album" ) )
617     {
618         input_item_SetAlbum( p_input, psz_value );
619
620     }
621     else if( !strcmp( psz_name, "trackNum" ) )
622     {
623         input_item_SetTrackNum( p_input, psz_value );
624     }
625     else if( !strcmp( psz_name, "duration" ) )
626     {
627         long i_num = atol( psz_value );
628         input_item_SetDuration( p_input, (mtime_t) i_num*1000 );
629     }
630     else if( !strcmp( psz_name, "annotation" ) )
631     {
632         input_item_SetDescription( p_input, psz_value );
633     }
634     else if( !strcmp( psz_name, "image" ) )
635     {
636         input_item_SetArtURL( p_input, psz_value );
637     }
638     return true;
639 }
640
641 /**
642  * \brief handles the <vlc:option> elements
643  */
644 static bool set_option SIMPLE_INTERFACE
645 {
646     /* exit if setting is impossible */
647     if( !psz_name || !psz_value || !p_input )
648         return false;
649
650     /* re-convert xml special characters inside psz_value */
651     resolve_xml_special_chars( psz_value );
652
653     input_item_AddOption( p_input, psz_value, 0 );
654
655     return true;
656 }
657
658 /**
659  * \brief parse the extension node of a XSPF playlist
660  */
661 static bool parse_extension_node COMPLEX_INTERFACE
662 {
663     input_item_t *p_input_item = p_input_node->p_item;
664     char *psz_name = NULL;
665     char *psz_value = NULL;
666     char *psz_title = NULL;
667     char *psz_application = NULL;
668     int i_node;
669     bool b_release_input_item = false;
670     xml_elem_hnd_t *p_handler = NULL;
671     input_item_t *p_new_input = NULL;
672
673     xml_elem_hnd_t pl_elements[] =
674         { {"vlc:node",   COMPLEX_CONTENT, {.cmplx = parse_extension_node} },
675           {"vlc:item",   COMPLEX_CONTENT, {.cmplx = parse_extitem_node} },
676           {"vlc:id",     SIMPLE_CONTENT, {NULL} },
677           {"vlc:option", SIMPLE_CONTENT, {.smpl = set_option} },
678           {NULL,    UNKNOWN_CONTENT, {NULL} }
679         };
680
681     /* read all extension node attributes */
682     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
683     {
684         psz_name = xml_ReaderName( p_xml_reader );
685         psz_value = xml_ReaderValue( p_xml_reader );
686         if( !psz_name || !psz_value )
687         {
688             msg_Err( p_demux, "invalid xml stream @ <vlc:node>" );
689             FREE_ATT();
690             return false;
691         }
692         /* attribute: title */
693         if( !strcmp( psz_name, "title" ) )
694         {
695             resolve_xml_special_chars( psz_value );
696             psz_title = psz_value;
697         }
698         /* extension attribute: application */
699         else if( !strcmp( psz_name, "application" ) )
700         {
701             psz_application = psz_value;
702         }
703         /* unknown attribute */
704         else
705         {
706             msg_Warn( p_demux, "invalid <%s> attribute:\"%s\"", psz_element,
707                       psz_name );
708             FREE_VALUE();
709         }
710         FREE_NAME();
711         psz_value = NULL;
712     }
713
714     /* attribute title is mandatory except for <extension> */
715     if( !strcmp( psz_element, "vlc:node" ) )
716     {
717         if( !psz_title )
718         {
719             msg_Warn( p_demux, "<vlc:node> requires \"title\" attribute" );
720             return false;
721         }
722         p_new_input = input_item_NewWithType( VLC_OBJECT( p_demux ),
723                           "vlc://nop", psz_title, 0, NULL, 0, -1,
724                           ITEM_TYPE_DIRECTORY );
725         if( p_new_input )
726         {
727             p_input_node =
728                 input_item_node_AppendItem( p_input_node, p_new_input );
729             p_input_item = p_new_input;
730             b_release_input_item = true;
731         }
732         free( psz_title );
733     }
734     else if( !strcmp( psz_element, "extension" ) )
735     {
736         if( !psz_application )
737         {
738             msg_Warn( p_demux, "<extension> requires \"application\" attribute" );
739             return false;
740         }
741         /* Skip the extension if the application is not vlc
742            This will skip all children of the current node */
743         else if( strcmp( psz_application, "http://www.videolan.org/vlc/playlist/0" ) )
744         {
745             msg_Dbg( p_demux, "Skipping \"%s\" extension tag", psz_application );
746             free( psz_application );
747             /* Skip all children */
748             while( xml_ReaderRead( p_xml_reader ) == 1 )
749             {
750                 if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
751                 {
752                     char *psz_name = xml_ReaderName( p_xml_reader );
753                     if( !strcmp( psz_name, "extension" ) )
754                     {
755                         free( psz_name );
756                         break;
757                     }
758                     msg_Dbg( p_demux, "\tskipping \"%s\" extension child", psz_name );
759                     free( psz_name );
760                 }
761             }
762             return true;
763         }
764     }
765     free( psz_application );
766
767
768     /* parse the child elements */
769     while( xml_ReaderRead( p_xml_reader ) == 1 )
770     {
771         i_node = xml_ReaderNodeType( p_xml_reader );
772         switch( i_node )
773         {
774             case XML_READER_NONE:
775                 break;
776             case XML_READER_STARTELEM:
777                 /*  element start tag  */
778                 psz_name = xml_ReaderName( p_xml_reader );
779                 if( !psz_name || !*psz_name )
780                 {
781                     msg_Err( p_demux, "invalid xml stream" );
782                     FREE_ATT();
783                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
784                     return false;
785                 }
786                 /* choose handler */
787                 for( p_handler = pl_elements;
788                      p_handler->name && strcmp( psz_name, p_handler->name );
789                      p_handler++ );
790                 if( !p_handler->name )
791                 {
792                     msg_Err( p_demux, "unexpected element <%s>", psz_name );
793                     FREE_ATT();
794                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
795                     return false;
796                 }
797                 FREE_NAME();
798                 /* complex content is parsed in a separate function */
799                 if( p_handler->type == COMPLEX_CONTENT )
800                 {
801                     if( p_handler->pf_handler.cmplx( p_demux,
802                                                      p_input_node,
803                                                      p_xml_reader,
804                                                      p_handler->name ) )
805                     {
806                         p_handler = NULL;
807                         FREE_ATT();
808                     }
809                     else
810                     {
811                         FREE_ATT();
812                         if( b_release_input_item ) vlc_gc_decref( p_new_input );
813                         return false;
814                     }
815                 }
816                 break;
817
818             case XML_READER_TEXT:
819                 /* simple element content */
820                 FREE_ATT();
821                 psz_value = xml_ReaderValue( p_xml_reader );
822                 if( !psz_value )
823                 {
824                     msg_Err( p_demux, "invalid xml stream" );
825                     FREE_ATT();
826                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
827                     return false;
828                 }
829                 break;
830
831             case XML_READER_ENDELEM:
832                 /* element end tag */
833                 psz_name = xml_ReaderName( p_xml_reader );
834                 if( !psz_name )
835                 {
836                     msg_Err( p_demux, "invalid xml stream" );
837                     FREE_ATT();
838                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
839                     return false;
840                 }
841                 /* leave if the current parent node is terminated */
842                 if( !strcmp( psz_name, psz_element ) )
843                 {
844                     FREE_ATT();
845                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
846                     return true;
847                 }
848                 /* there MUST have been a start tag for that element name */
849                 if( !p_handler || !p_handler->name
850                     || strcmp( p_handler->name, psz_name ))
851                 {
852                     msg_Err( p_demux, "there's no open element left for <%s>",
853                              psz_name );
854                     FREE_ATT();
855                     if( b_release_input_item ) vlc_gc_decref( p_new_input );
856                     return false;
857                 }
858
859                 /* special tag <vlc:id> */
860                 if( !strcmp( p_handler->name, "vlc:id" ) )
861                 {
862                     p_demux->p_sys->i_track_id = atoi( psz_value );
863                 }
864                 else if( p_handler->pf_handler.smpl )
865                 {
866                     p_handler->pf_handler.smpl( p_input_item, p_handler->name,
867                                                 psz_value );
868                 }
869                 FREE_ATT();
870                 p_handler = NULL;
871                 break;
872
873             default:
874                 /* unknown/unexpected xml node */
875                 msg_Err( p_demux, "unexpected xml node %i", i_node );
876                 FREE_ATT();
877                 if( b_release_input_item ) vlc_gc_decref( p_new_input );
878                 return false;
879         }
880         FREE_NAME();
881     }
882     if( b_release_input_item ) vlc_gc_decref( p_new_input );
883     return false;
884 }
885
886 /**
887  * \brief parse the extension item node of a XSPF playlist
888  */
889 static bool parse_extitem_node COMPLEX_INTERFACE
890 {
891     VLC_UNUSED(psz_element);
892     input_item_t *p_new_input = NULL;
893     int i_tid = -1;
894
895     /* read all extension item attributes */
896     while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
897     {
898         char *psz_name = xml_ReaderName( p_xml_reader );
899         char *psz_value = xml_ReaderValue( p_xml_reader );
900         if( !psz_name || !psz_value )
901         {
902             msg_Err( p_demux, "invalid xml stream @ <vlc:item>" );
903             free( psz_name );
904             free( psz_value );
905             return false;
906         }
907         /* attribute: href */
908         if( !strcmp( psz_name, "tid" ) )
909         {
910             i_tid = atoi( psz_value );
911         }
912         /* unknown attribute */
913         else
914             msg_Warn( p_demux, "invalid <vlc:item> attribute:\"%s\"", psz_name);
915
916         free( psz_name );
917         free( psz_value );
918     }
919
920     /* attribute href is mandatory */
921     if( i_tid < 0 )
922     {
923         msg_Warn( p_demux, "<vlc:item> requires \"tid\" attribute" );
924         return false;
925     }
926
927     if( i_tid >= p_demux->p_sys->i_tracklist_entries )
928     {
929         msg_Warn( p_demux, "invalid \"tid\" attribute" );
930         return false;
931     }
932
933     p_new_input = p_demux->p_sys->pp_tracklist[ i_tid ];
934     if( p_new_input )
935     {
936         input_item_node_AppendItem( p_input_node, p_new_input );
937         vlc_gc_decref( p_new_input );
938         p_demux->p_sys->pp_tracklist[i_tid] = NULL;
939     }
940
941     /* kludge for #1293 - XTAG sends ENDELEM for self closing tag */
942     /* (libxml sends NONE) */
943     xml_ReaderRead( p_xml_reader );
944
945     return true;
946 }
947
948 /**
949  * \brief skips complex element content that we can't manage
950  */
951 static bool skip_element COMPLEX_INTERFACE
952 {
953     VLC_UNUSED(p_demux); VLC_UNUSED(p_input_node);
954
955     while( xml_ReaderRead( p_xml_reader ) == 1 )
956     {
957         if( xml_ReaderNodeType( p_xml_reader ) == XML_READER_ENDELEM )
958         {
959             char *psz_endname = xml_ReaderName( p_xml_reader );
960             if( !psz_endname )
961                 return false;
962             if( !strcmp( psz_element, psz_endname ) )
963             {
964                 free( psz_endname );
965                 return true;
966             }
967             else
968                 free( psz_endname );
969         }
970     }
971     return false;
972 }