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