]> git.sesse.net Git - vlc/blob - modules/demux/playlist/zpl.c
Fix many memory leaks (and potential ones)
[vlc] / modules / demux / playlist / zpl.c
1 /*****************************************************************************
2  * zpl.c : ZPL playlist format import
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: Su Heaven <suheaven@gmail.com>
7  *          RĂ©mi Duraffort <ivoire@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 51
21  * Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_demux.h>
34
35 #include "playlist.h"
36
37 struct demux_sys_t
38 {
39     char *psz_prefix;
40 };
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int Demux( demux_t *p_demux);
46 static char* ParseTabValue(char* psz_string);
47
48 /*****************************************************************************
49  * Import_ZPL: main import function
50  *****************************************************************************/
51 int Import_ZPL( vlc_object_t *p_this )
52 {
53     demux_t *p_demux = (demux_t *)p_this;
54
55     if(! ( demux_IsPathExtension( p_demux, ".zpl" ) || demux_IsForced( p_demux,  "zpl" )))
56         return VLC_EGENERIC;
57
58     STANDARD_DEMUX_INIT_MSG( "found valid ZPL playlist" );
59     p_demux->p_sys->psz_prefix = FindPrefix( p_demux );
60
61     return VLC_SUCCESS;
62 }
63
64
65 /*****************************************************************************
66  * Deactivate: frees unused data
67  *****************************************************************************/
68 void Close_ZPL( vlc_object_t *p_this )
69 {
70     demux_t *p_demux = (demux_t *)p_this;
71     free( p_demux->p_sys->psz_prefix );
72     free( p_demux->p_sys );
73 }
74
75 static int Demux( demux_t *p_demux )
76 {
77     char       *psz_line;
78
79     mtime_t i_duration = -1;
80     char *psz_title = NULL,       *psz_genre = NULL,      *psz_tracknum = NULL,
81          *psz_language = NULL,    *psz_artist = NULL,     *psz_album = NULL,
82          *psz_date = NULL,        *psz_publisher = NULL,  *psz_encodedby = NULL,
83          *psz_description = NULL, *psz_url = NULL,        *psz_copyright = NULL,
84          *psz_mrl = NULL;
85
86     input_item_t *p_current_input = GetCurrentItem(p_demux);
87
88     psz_line = stream_ReadLine( p_demux->s );
89     char *psz_parse = psz_line;
90
91     /* Skip leading tabs and spaces */
92     while( *psz_parse == ' '  || *psz_parse == '\t' ||
93            *psz_parse == '\n' || *psz_parse == '\r' )
94         psz_parse++;
95
96     /* if the 1st line is "AC", skip it */
97     /* TODO: using this information ? */
98     if( !strncasecmp( psz_parse, "AC", strlen( "AC" ) ) )
99     {
100         free( psz_line );
101         psz_line = stream_ReadLine( p_demux->s );
102     }
103
104     input_item_node_t *p_subitems = input_item_node_Create( p_current_input );
105
106     /* Loop on all lines */
107     while( psz_line )
108     {
109         psz_parse = psz_line;
110
111         /* Skip leading tabs and spaces */
112         while( *psz_parse == ' '  || *psz_parse == '\t' ||
113                *psz_parse == '\n' || *psz_parse == '\r' )
114             psz_parse++;
115
116         /* filename */
117         if( !strncasecmp( psz_parse, "NM", strlen( "NM" ) ) )
118         {
119             char *psz_tabvalue = ParseTabValue( psz_parse );
120             if( !EMPTY_STR(psz_tabvalue) )
121             {
122                 psz_mrl = ProcessMRL( psz_tabvalue, p_demux->p_sys->psz_prefix );
123             }
124             free( psz_tabvalue );
125         }
126
127         /* duration */
128         else if( !strncasecmp( psz_parse, "DR", strlen( "DR" ) ) )
129         {
130             char *psz_tabvalue = ParseTabValue( psz_parse );
131             if( !EMPTY_STR(psz_tabvalue) )
132             {
133                 int i_parsed_duration = atoi( psz_tabvalue );
134                 if( i_parsed_duration >= 0 )
135                     i_duration = i_parsed_duration * INT64_C(1000);
136             }
137             free( psz_tabvalue );
138         }
139
140 #define PARSE(tag,variable)                                     \
141     else if( !strncasecmp( psz_parse, tag, strlen( tag ) ) )    \
142         variable = ParseTabValue( psz_parse );
143
144         PARSE( "TT", psz_title )
145         PARSE( "TG", psz_genre )
146         PARSE( "TR", psz_tracknum )
147         PARSE( "TL", psz_language )
148         PARSE( "TA", psz_artist )
149         PARSE( "TB", psz_album )
150         PARSE( "TY", psz_date )
151         PARSE( "TH", psz_publisher )
152         PARSE( "TE", psz_encodedby )
153         PARSE( "TC", psz_description )
154         PARSE( "TU", psz_url )
155         PARSE( "TO", psz_copyright )
156
157 #undef PARSE
158
159         /* force a duration ? */
160         else if( !strncasecmp( psz_parse, "FD", strlen( "FD" ) ) )
161         {}
162
163         /* end of file entry */
164         else if( !strncasecmp( psz_parse, "BR!", strlen( "BR!" ) ) )
165         {
166             /* create the input item */
167             input_item_t *p_input = input_item_NewExt( psz_mrl,
168                                         psz_title, 0, NULL, 0, i_duration );
169             input_item_node_AppendItem( p_subitems, p_input );
170             FREENULL( psz_mrl );
171             FREENULL( psz_title );
172             i_duration = -1;
173
174 #define SET(variable, type)                             \
175     if( !EMPTY_STR(variable) )                          \
176     {                                                   \
177         input_item_Set##type( p_input, variable );      \
178     }                                                   \
179     FREENULL( variable );
180             /* set the meta */
181             SET( psz_genre, Genre );
182             SET( psz_tracknum, TrackNum );
183             SET( psz_language, Language );
184             SET( psz_artist, Artist );
185             SET( psz_album, Album );
186             SET( psz_date, Date );
187             SET( psz_encodedby, EncodedBy );
188             SET( psz_description, Description );
189             SET( psz_copyright, Copyright );
190             SET( psz_url, URL );
191             SET( psz_publisher, Publisher );
192 #undef SET
193
194             vlc_gc_decref( p_input );
195         }
196         else
197             msg_Warn( p_demux, "invalid line '%s'", psz_parse );
198
199         /* Fetch another line */
200         free( psz_line );
201         psz_line = stream_ReadLine( p_demux->s );
202     }
203
204     input_item_node_PostAndDelete( p_subitems );
205
206     vlc_gc_decref(p_current_input);
207
208     // Free everything if the file is wrongly formated
209     free( psz_title );
210     free( psz_genre );
211     free( psz_tracknum );
212     free( psz_language );
213     free( psz_artist );
214     free( psz_album );
215     free( psz_date );
216     free( psz_publisher );
217     free( psz_encodedby );
218     free( psz_description );
219     free( psz_url );
220     free( psz_copyright );
221     free( psz_mrl );
222
223     var_Destroy( p_demux, "zpl-extvlcopt" );
224     return 0; /* Needed for correct operation of go back */
225 }
226
227 static char* ParseTabValue(char* psz_string)
228 {
229     int i_len = strlen( psz_string );
230     if(i_len <= 3 )
231         return NULL;
232     char* psz_value = calloc( i_len, 1 );
233     if( ! psz_value )
234         return NULL;
235
236     sscanf( psz_string,"%*[^=]=%[^\r\t\n]", psz_value );
237
238     return psz_value;
239 }