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