]> git.sesse.net Git - vlc/blob - modules/access/zip/zipaccess.c
zip access: fix #2467. Now it's able to open files with non ASCII names, but can...
[vlc] / modules / access / zip / zipaccess.c
1 /*****************************************************************************
2  * zipaccess.c: Module (access) to extract different archives, based on zlib
3  *****************************************************************************
4  * Copyright (C) 2009 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_path = NULL, *psz_sep = NULL;
68
69     p_access->p_sys = p_sys = (access_sys_t*)
70             calloc( 1, sizeof( access_sys_t ) );
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     p_sys->psz_fileInzip = strdup( psz_sep + 1 );
82
83     /* Define IO functions */
84     zlib_filefunc_def *p_func = (zlib_filefunc_def*)
85                                     calloc( 1, sizeof( zlib_filefunc_def ) );
86     p_func->zopen_file   = ZipIO_Open;
87     p_func->zread_file   = ZipIO_Read;
88     p_func->zwrite_file  = ZipIO_Write; // see comment
89     p_func->ztell_file   = ZipIO_Tell;
90     p_func->zseek_file   = ZipIO_Seek;
91     p_func->zclose_file  = ZipIO_Close;
92     p_func->zerror_file  = ZipIO_Error;
93     p_func->opaque       = p_access;
94
95     /* Open zip archive */
96     file = p_access->p_sys->zipFile = unzOpen2( psz_path, p_func );
97     if( !file )
98     {
99         msg_Err( p_access, "not a valid zip archive: '%s'", psz_path );
100         goto exit;
101     }
102
103     /* Open file in zip */
104     OpenFileInZip( p_access, 0 );
105
106     /* Set callback */
107     ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
108
109     /* Get some infos about current file. Maybe we could want some more ? */
110     unz_file_info z_info;
111     unzGetCurrentFileInfo( file, &z_info, NULL, 0, NULL, 0, NULL, 0 );
112
113     /* Set access informations: size is needed for AccessSeek */
114     p_access->info.i_size = z_info.uncompressed_size;
115     p_access->info.i_pos  = 0;
116     p_access->info.b_eof  = false;
117
118     i_ret = VLC_SUCCESS;
119
120 exit:
121     if( i_ret != VLC_SUCCESS )
122     {
123         if( file )
124         {
125             unzCloseCurrentFile( file );
126             unzClose( file );
127         }
128         free( p_sys->psz_fileInzip );
129         free( p_sys->fileFunctions );
130         free( p_sys );
131     }
132
133     free( psz_path );
134     return i_ret;
135 }
136
137 /** **************************************************************************
138  * \brief Close access: free structures
139  *****************************************************************************/
140 void AccessClose( vlc_object_t *p_this )
141 {
142     access_t     *p_access = (access_t*)p_this;
143     access_sys_t *p_sys = p_access->p_sys;
144     if( p_sys )
145     {
146         unzFile file = p_sys->zipFile;
147         if( file )
148         {
149             unzCloseCurrentFile( file );
150             unzClose( file );
151         }
152         free( p_sys->psz_fileInzip );
153         free( p_sys->fileFunctions );
154         free( p_sys );
155     }
156 }
157
158 /** **************************************************************************
159  * \brief Control access
160  *****************************************************************************/
161 static int AccessControl( access_t *p_access, int i_query, va_list args )
162 {
163     bool         *pb_bool;
164     int64_t      *pi_64;
165
166     switch( i_query )
167     {
168         /* */
169         case ACCESS_CAN_SEEK:
170         case ACCESS_CAN_PAUSE:
171         case ACCESS_CAN_CONTROL_PACE:
172             pb_bool = (bool*)va_arg( args, bool* );
173             *pb_bool = true;
174             break;
175
176         case ACCESS_CAN_FASTSEEK:
177             pb_bool = (bool*)va_arg( args, bool* );
178             *pb_bool = false;
179             break;
180
181         case ACCESS_GET_PTS_DELAY:
182             pi_64 = (int64_t*)va_arg( args, int64_t * );
183             *pi_64 = DEFAULT_PTS_DELAY;
184             break;
185
186         /* */
187         case ACCESS_SET_PAUSE_STATE:
188             /* Nothing to do */
189             break;
190
191         case ACCESS_GET_TITLE_INFO:
192         case ACCESS_SET_TITLE:
193         case ACCESS_SET_SEEKPOINT:
194         case ACCESS_SET_PRIVATE_ID_STATE:
195         case ACCESS_GET_META:
196         case ACCESS_GET_PRIVATE_ID_STATE:
197         case ACCESS_GET_CONTENT_TYPE:
198             return VLC_EGENERIC;
199
200         default:
201             msg_Warn( p_access, "unimplemented query %d in control", i_query );
202             return VLC_EGENERIC;
203
204     }
205     return VLC_SUCCESS;
206 }
207
208 /** **************************************************************************
209  * \brief Read access
210  * Reads current opened file in zip. This does not open the file in zip.
211  * Return -1 if no data yet, 0 if no more data, else real data read
212  *****************************************************************************/
213 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
214 {
215     access_sys_t *p_sys = p_access->p_sys;
216     assert( p_sys );
217     unzFile file = p_sys->zipFile;
218     if( !file )
219     {
220         msg_Err( p_access, "archive not opened !" );
221         return VLC_EGENERIC;
222     }
223
224     int i_read = 0;
225     i_read = unzReadCurrentFile( file, p_buffer, sz );
226
227     p_access->info.i_pos = unztell( file );
228     return ( i_read >= 0 ? i_read : VLC_EGENERIC );
229 }
230
231 /** **************************************************************************
232  * \brief Seek inside zip file
233  *****************************************************************************/
234 static int AccessSeek( access_t *p_access, int64_t seek_len )
235 {
236     access_sys_t *p_sys = p_access->p_sys;
237     assert( p_sys );
238     unzFile file = p_sys->zipFile;
239     if( !file )
240     {
241         msg_Err( p_access, "archive not opened !" );
242         return VLC_EGENERIC;
243     }
244
245     /* Reopen file in zip if needed */
246     if( p_access->info.i_pos != 0 )
247     {
248         OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
249     }
250
251     /* Read seek_len data and drop it */
252     int i_seek = 0;
253     int i_read = 1;
254     char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
255     while( ( i_seek < seek_len ) && ( i_read > 0 ) )
256     {
257         i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
258                ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
259         i_read = unzReadCurrentFile( file, p_buffer, i_read );
260         if( i_read < 0 )
261         {
262             msg_Warn( p_access, "could not seek in file" );
263             free( p_buffer );
264             return VLC_EGENERIC;
265         }
266         else
267         {
268             i_seek += i_read;
269         }
270     }
271     free( p_buffer );
272
273     p_access->info.i_pos = unztell( file );
274     return VLC_SUCCESS;
275 }
276
277 /** **************************************************************************
278  * \brief Open file in zip
279  *****************************************************************************/
280 static int OpenFileInZip( access_t *p_access, int i_pos )
281 {
282     access_sys_t *p_sys = p_access->p_sys;
283     unzFile file = p_sys->zipFile;
284     if( !p_sys->psz_fileInzip )
285     {
286         return VLC_EGENERIC;
287     }
288
289     i_pos = __MIN( i_pos, 0 );
290     p_access->info.i_pos = 0;
291
292     unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
293     if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
294     {
295         msg_Err( p_access, "could not [re]locate file in zip: '%s'",
296                  p_sys->psz_fileInzip );
297         return VLC_EGENERIC;
298     }
299     if( unzOpenCurrentFile( file ) != UNZ_OK )
300     {
301         msg_Err( p_access, "could not [re]open file in zip: '%s'",
302                  p_sys->psz_fileInzip );
303         return VLC_EGENERIC;
304     }
305     if( i_pos > 0 )
306         return AccessSeek( p_access, i_pos );
307     else
308         return VLC_SUCCESS;
309 }
310
311 /** **************************************************************************
312  * \brief I/O functions for the ioapi: open (read only)
313  *****************************************************************************/
314 static void* ZCALLBACK ZipIO_Open( void* opaque, const char* file, int mode )
315 {
316     assert(opaque != NULL);
317     assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
318
319     access_t *p_access = (access_t*) opaque;
320
321     return stream_UrlNew( p_access, file );
322 }
323
324 /** **************************************************************************
325  * \brief I/O functions for the ioapi: read
326  *****************************************************************************/
327 static uLong ZCALLBACK ZipIO_Read( void* opaque, void* stream,
328                                    void* buf, uLong size )
329 {
330     (void)opaque;
331     //access_t *p_access = (access_t*) opaque;
332     //msg_Dbg(p_access, "read %d", size);
333     return stream_Read( (stream_t*) stream, buf, size );
334 }
335
336 /** **************************************************************************
337  * \brief I/O functions for the ioapi: write (assert insteadof segfault)
338  *****************************************************************************/
339 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
340                                     const void* buf, uLong size )
341 {
342     (void)opaque; (void)stream; (void)buf; (void)size;
343     int zip_access_cannot_write_this_should_not_happen = 0;
344     assert(zip_access_cannot_write_this_should_not_happen);
345     return 0;
346 }
347
348 /** **************************************************************************
349  * \brief I/O functions for the ioapi: tell
350  *****************************************************************************/
351 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
352 {
353     (void)opaque;
354     int64_t i64_tell = stream_Tell( (stream_t*) stream );
355     //access_t *p_access = (access_t*) opaque;
356     //msg_Dbg(p_access, "tell %" PRIu64, i64_tell);
357     return (long)i64_tell;
358 }
359
360 /** **************************************************************************
361  * \brief I/O functions for the ioapi: seek
362  *****************************************************************************/
363 static long ZCALLBACK ZipIO_Seek( void* opaque, void* stream,
364                                   uLong offset, int origin )
365 {
366     (void)opaque;
367     //access_t *p_access = (access_t*) opaque;
368     int64_t pos = offset;
369     switch( origin )
370     {
371         case SEEK_CUR:
372             pos += stream_Tell( (stream_t*) stream );
373             break;
374         case SEEK_SET:
375             break;
376         case SEEK_END:
377             pos += stream_Size( (stream_t*) stream );
378             break;
379         default:
380             return -1;
381     }
382     //msg_Dbg( p_access, "seek (%d,%d): %" PRIu64, offset, origin, pos );
383     stream_Seek( (stream_t*) stream, pos );
384     /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
385              the stream, which is doable but returns an error in VLC.
386              That's why we always assume this was OK. FIXME */
387     return 0;
388 }
389
390 /** **************************************************************************
391  * \brief I/O functions for the ioapi: close
392  *****************************************************************************/
393 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
394 {
395     (void)opaque;
396     stream_Delete( (stream_t*) stream );
397     return 0;
398 }
399
400 /** **************************************************************************
401  * \brief I/O functions for the ioapi: test error (man 3 ferror)
402  *****************************************************************************/
403 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
404 {
405     (void)opaque;
406     (void)stream;
407     //msg_Dbg( p_access, "error" );
408     return 0;
409 }
410
411
412
413 #else
414 # error Can not compile zip demuxer without zlib support
415 #endif