]> git.sesse.net Git - vlc/blob - modules/access/zip/zipaccess.c
Zip: add a stream_filter and an access
[vlc] / modules / access / zip / zipaccess.c
1 /*****************************************************************************
2  * zipaccess.c: Module (access) to extract different archives, based on zlib
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Philippe AndrĂ© <jpeg@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /** @todo:
25  * - implement crypto (using url zip://user:password@path-to-archive#ZIP#file
26  * - read files in zip with long name (use unz_file_info.size_filename)
27  * - multi-volume archive support ?
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_ZLIB_H
35
36 #include "zip.h"
37 #include <vlc_access.h>
38
39 /** **************************************************************************
40  * This is our own access_sys_t for zip files
41  *****************************************************************************/
42 struct access_sys_t
43 {
44     /* zlib / unzip members */
45     unzFile            zipFile;
46     zlib_filefunc_def *fileFunctions;
47
48     /* file in zip information */
49     char              *psz_fileInzip;
50 };
51
52 static int AccessControl( access_t *p_access, int i_query, va_list args );
53 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
54 static int AccessSeek( access_t *, int64_t );
55 static int OpenFileInZip( access_t *p_access, int i_pos );
56
57 /** **************************************************************************
58  * \brief Open access
59  *****************************************************************************/
60 int AccessOpen( vlc_object_t *p_this )
61 {
62     access_t     *p_access = (access_t*)p_this;
63     access_sys_t *p_sys;
64     int i_ret              = VLC_EGENERIC;
65     unzFile file           = 0;
66
67     char *psz_pathToZip = NULL, *psz_path = NULL, *psz_sep = NULL;
68
69     p_access->p_sys = p_sys = (access_sys_t*)
70             calloc( sizeof( access_sys_t ), 1 );
71     if( !p_sys )
72         return VLC_ENOMEM;
73
74     /* Split the MRL */
75     psz_path = strdup( p_access->psz_path );
76     psz_sep = strchr( psz_path, ZIP_SEP_CHAR );
77     if( !psz_sep )
78         return VLC_EGENERIC;
79
80     *psz_sep = '\0';
81     psz_pathToZip = unescape_URI_duplicate( psz_path );
82     p_sys->psz_fileInzip = strdup( psz_sep + 1 );
83
84     /* Define IO functions */
85     zlib_filefunc_def *p_func = (zlib_filefunc_def*)
86                                     calloc( 1, sizeof( zlib_filefunc_def ) );
87     p_func->zopen_file   = ZipIO_Open;
88     p_func->zread_file   = ZipIO_Read;
89     p_func->zwrite_file  = ZipIO_Write; // see comment
90     p_func->ztell_file   = ZipIO_Tell;
91     p_func->zseek_file   = ZipIO_Seek;
92     p_func->zclose_file  = ZipIO_Close;
93     p_func->zerror_file  = ZipIO_Error;
94     p_func->opaque       = p_access;
95
96     /* Open zip archive */
97     file = p_access->p_sys->zipFile = unzOpen2( psz_pathToZip, p_func );
98     if( !file )
99     {
100         msg_Err( p_access, "not a valid zip archive: '%s'", psz_pathToZip );
101         goto exit;
102     }
103
104     /* Open file in zip */
105     OpenFileInZip( p_access, 0 );
106
107     /* Set callback */
108     ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
109
110     /* Get some infos about current file. Maybe we could want some more ? */
111     unz_file_info z_info;
112     unzGetCurrentFileInfo( file, &z_info, NULL, 0, NULL, 0, NULL, 0 );
113
114     /* Set access informations: size is needed for AccessSeek */
115     p_access->info.i_size = z_info.uncompressed_size;
116     p_access->info.i_pos  = 0;
117     p_access->info.b_eof  = false;
118
119     i_ret = VLC_SUCCESS;
120
121 exit:
122     if( i_ret != VLC_SUCCESS )
123     {
124         if( file )
125         {
126             unzCloseCurrentFile( file );
127             unzClose( file );
128         }
129         free( p_sys->psz_fileInzip );
130         free( p_sys->fileFunctions );
131         free( p_sys );
132     }
133
134     free( psz_pathToZip );
135     free( psz_path );
136     return i_ret;
137 }
138
139 /** **************************************************************************
140  * \brief Close access: free structures
141  *****************************************************************************/
142 void AccessClose( vlc_object_t *p_this )
143 {
144     access_t     *p_access = (access_t*)p_this;
145     access_sys_t *p_sys = p_access->p_sys;
146     if( p_sys )
147     {
148         unzFile file = p_sys->zipFile;
149         if( file )
150         {
151             unzCloseCurrentFile( file );
152             unzClose( file );
153         }
154         free( p_sys->psz_fileInzip );
155         free( p_sys->fileFunctions );
156         free( p_sys );
157     }
158     var_Destroy( p_access, "zip-no-xspf" );
159 }
160
161 /** **************************************************************************
162  * \brief Control access
163  *****************************************************************************/
164 static int AccessControl( access_t *p_access, int i_query, va_list args )
165 {
166     bool         *pb_bool;
167     int          *pi_int;
168     int64_t      *pi_64;
169
170     switch( i_query )
171     {
172         /* */
173         case ACCESS_CAN_SEEK:
174         case ACCESS_CAN_PAUSE:
175         case ACCESS_CAN_CONTROL_PACE:
176             pb_bool = (bool*)va_arg( args, bool* );
177             *pb_bool = true;
178             break;
179
180         case ACCESS_CAN_FASTSEEK:
181             pb_bool = (bool*)va_arg( args, bool* );
182             *pb_bool = false;
183             break;
184
185         /* */
186         case ACCESS_GET_MTU:
187             pi_int = (int*)va_arg( args, int * );
188             *pi_int = 0;
189             break;
190
191         case ACCESS_GET_PTS_DELAY:
192             pi_64 = (int64_t*)va_arg( args, int64_t * );
193             *pi_64 = DEFAULT_PTS_DELAY;
194             break;
195
196         /* */
197         case ACCESS_SET_PAUSE_STATE:
198             /* Nothing to do */
199             break;
200
201         case ACCESS_GET_TITLE_INFO:
202         case ACCESS_SET_TITLE:
203         case ACCESS_SET_SEEKPOINT:
204         case ACCESS_SET_PRIVATE_ID_STATE:
205         case ACCESS_GET_META:
206         case ACCESS_GET_PRIVATE_ID_STATE:
207         case ACCESS_GET_CONTENT_TYPE:
208             return VLC_EGENERIC;
209
210         default:
211             msg_Warn( p_access, "unimplemented query %d in control", i_query );
212             return VLC_EGENERIC;
213
214     }
215     return VLC_SUCCESS;
216 }
217
218 /** **************************************************************************
219  * \brief Read access
220  * Reads current opened file in zip. This does not open the file in zip.
221  * Return -1 if no data yet, 0 if no more data, else real data read
222  *****************************************************************************/
223 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
224 {
225     access_sys_t *p_sys = p_access->p_sys;
226     assert( p_sys );
227     unzFile file = p_sys->zipFile;
228     if( !file )
229     {
230         msg_Err( p_access, "archive not opened !" );
231         return VLC_EGENERIC;
232     }
233
234     int i_read = 0;
235     i_read = unzReadCurrentFile( file, p_buffer, sz );
236
237     p_access->info.i_pos = unztell( file );
238     return ( i_read >= 0 ? i_read : VLC_EGENERIC );
239 }
240
241 /** **************************************************************************
242  * \brief Seek inside zip file
243  *****************************************************************************/
244 static int AccessSeek( access_t *p_access, int64_t seek_len )
245 {
246     access_sys_t *p_sys = p_access->p_sys;
247     assert( p_sys );
248     unzFile file = p_sys->zipFile;
249     if( !file )
250     {
251         msg_Err( p_access, "archive not opened !" );
252         return VLC_EGENERIC;
253     }
254
255     /* Reopen file in zip if needed */
256     if( p_access->info.i_pos != 0 )
257     {
258         OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
259     }
260
261     /* Read seek_len data and drop it */
262     int i_seek = 0;
263     int i_read = 1;
264     char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
265     while( ( i_seek < seek_len ) && ( i_read > 0 ) )
266     {
267         i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
268                ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
269         i_read = unzReadCurrentFile( file, p_buffer, i_read );
270         if( i_read < 0 )
271         {
272             msg_Warn( p_access, "could not seek in file" );
273             free( p_buffer );
274             return VLC_EGENERIC;
275         }
276         else
277         {
278             i_seek += i_read;
279         }
280     }
281     free( p_buffer );
282
283     p_access->info.i_pos = unztell( file );
284     return VLC_SUCCESS;
285 }
286
287 /** **************************************************************************
288  * \brief Open file in zip
289  *****************************************************************************/
290 static int OpenFileInZip( access_t *p_access, int i_pos )
291 {
292     access_sys_t *p_sys = p_access->p_sys;
293     unzFile file = p_sys->zipFile;
294     if( !p_sys->psz_fileInzip )
295     {
296         return VLC_EGENERIC;
297     }
298
299     i_pos = __MIN( i_pos, 0 );
300     p_access->info.i_pos = 0;
301
302     unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
303     if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
304     {
305         msg_Err( p_access, "could not [re]locate file in zip: '%s'",
306                  p_sys->psz_fileInzip );
307         return VLC_EGENERIC;
308     }
309     if( unzOpenCurrentFile( file ) != UNZ_OK )
310     {
311         msg_Err( p_access, "could not [re]open file in zip: '%s'",
312                  p_sys->psz_fileInzip );
313         return VLC_EGENERIC;
314     }
315     if( i_pos > 0 )
316         return AccessSeek( p_access, i_pos );
317     else
318         return VLC_SUCCESS;
319 }
320
321 /** **************************************************************************
322  * \brief I/O functions for the ioapi: open (read only)
323  *****************************************************************************/
324 static void* ZCALLBACK ZipIO_Open( void* opaque, const char* file, int mode )
325 {
326     assert(opaque != NULL);
327     assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
328
329     access_t *p_access = (access_t*) opaque;
330
331     return stream_UrlNew( p_access, file );
332 }
333
334 /** **************************************************************************
335  * \brief I/O functions for the ioapi: read
336  *****************************************************************************/
337 static uLong ZCALLBACK ZipIO_Read( void* opaque, void* stream,
338                                    void* buf, uLong size )
339 {
340     (void)opaque;
341     //access_t *p_access = (access_t*) opaque;
342     //msg_Dbg(p_access, "read %d", size);
343     return stream_Read( (stream_t*) stream, buf, size );
344 }
345
346 /** **************************************************************************
347  * \brief I/O functions for the ioapi: write (assert insteadof segfault)
348  *****************************************************************************/
349 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
350                                     const void* buf, uLong size )
351 {
352     (void)opaque; (void)stream; (void)buf; (void)size;
353     int zip_access_cannot_write_this_should_not_happen = 0;
354     assert(zip_access_cannot_write_this_should_not_happen);
355     return 0;
356 }
357
358 /** **************************************************************************
359  * \brief I/O functions for the ioapi: tell
360  *****************************************************************************/
361 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
362 {
363     (void)opaque;
364     int64_t i64_tell = stream_Tell( (stream_t*) stream );
365     //access_t *p_access = (access_t*) opaque;
366     //msg_Dbg(p_access, "tell %" PRIu64, i64_tell);
367     return (long)i64_tell;
368 }
369
370 /** **************************************************************************
371  * \brief I/O functions for the ioapi: seek
372  *****************************************************************************/
373 static long ZCALLBACK ZipIO_Seek( void* opaque, void* stream,
374                                   uLong offset, int origin )
375 {
376     (void)opaque;
377     //access_t *p_access = (access_t*) opaque;
378     int64_t pos = offset;
379     switch( origin )
380     {
381         case SEEK_CUR:
382             pos += stream_Tell( (stream_t*) stream );
383             break;
384         case SEEK_SET:
385             break;
386         case SEEK_END:
387             pos += stream_Size( (stream_t*) stream );
388             break;
389         default:
390             return -1;
391     }
392     //msg_Dbg( p_access, "seek (%d,%d): %" PRIu64, offset, origin, pos );
393     stream_Seek( (stream_t*) stream, pos );
394     /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
395              the stream, which is doable but returns an error in VLC.
396              That's why we always assume this was OK. FIXME */
397     return 0;
398 }
399
400 /** **************************************************************************
401  * \brief I/O functions for the ioapi: close
402  *****************************************************************************/
403 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
404 {
405     (void)opaque;
406     stream_Delete( (stream_t*) stream );
407     return 0;
408 }
409
410 /** **************************************************************************
411  * \brief I/O functions for the ioapi: test error (man 3 ferror)
412  *****************************************************************************/
413 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
414 {
415     (void)opaque;
416     (void)stream;
417     //msg_Dbg( p_access, "error" );
418     return 0;
419 }
420
421
422
423 #else
424 # error Can not compile zip demuxer without zlib support
425 #endif