]> git.sesse.net Git - vlc/blob - modules/access/zip/zipaccess.c
Revert "zip access: fix #2467. Now it's able to open files with non ASCII names,...
[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_pathToZip = NULL, *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     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 }
159
160 /** **************************************************************************
161  * \brief Control access
162  *****************************************************************************/
163 static int AccessControl( access_t *p_access, int i_query, va_list args )
164 {
165     bool         *pb_bool;
166     int64_t      *pi_64;
167
168     switch( i_query )
169     {
170         /* */
171         case ACCESS_CAN_SEEK:
172         case ACCESS_CAN_PAUSE:
173         case ACCESS_CAN_CONTROL_PACE:
174             pb_bool = (bool*)va_arg( args, bool* );
175             *pb_bool = true;
176             break;
177
178         case ACCESS_CAN_FASTSEEK:
179             pb_bool = (bool*)va_arg( args, bool* );
180             *pb_bool = false;
181             break;
182
183         case ACCESS_GET_PTS_DELAY:
184             pi_64 = (int64_t*)va_arg( args, int64_t * );
185             *pi_64 = DEFAULT_PTS_DELAY;
186             break;
187
188         /* */
189         case ACCESS_SET_PAUSE_STATE:
190             /* Nothing to do */
191             break;
192
193         case ACCESS_GET_TITLE_INFO:
194         case ACCESS_SET_TITLE:
195         case ACCESS_SET_SEEKPOINT:
196         case ACCESS_SET_PRIVATE_ID_STATE:
197         case ACCESS_GET_META:
198         case ACCESS_GET_PRIVATE_ID_STATE:
199         case ACCESS_GET_CONTENT_TYPE:
200             return VLC_EGENERIC;
201
202         default:
203             msg_Warn( p_access, "unimplemented query %d in control", i_query );
204             return VLC_EGENERIC;
205
206     }
207     return VLC_SUCCESS;
208 }
209
210 /** **************************************************************************
211  * \brief Read access
212  * Reads current opened file in zip. This does not open the file in zip.
213  * Return -1 if no data yet, 0 if no more data, else real data read
214  *****************************************************************************/
215 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
216 {
217     access_sys_t *p_sys = p_access->p_sys;
218     assert( p_sys );
219     unzFile file = p_sys->zipFile;
220     if( !file )
221     {
222         msg_Err( p_access, "archive not opened !" );
223         return VLC_EGENERIC;
224     }
225
226     int i_read = 0;
227     i_read = unzReadCurrentFile( file, p_buffer, sz );
228
229     p_access->info.i_pos = unztell( file );
230     return ( i_read >= 0 ? i_read : VLC_EGENERIC );
231 }
232
233 /** **************************************************************************
234  * \brief Seek inside zip file
235  *****************************************************************************/
236 static int AccessSeek( access_t *p_access, int64_t seek_len )
237 {
238     access_sys_t *p_sys = p_access->p_sys;
239     assert( p_sys );
240     unzFile file = p_sys->zipFile;
241     if( !file )
242     {
243         msg_Err( p_access, "archive not opened !" );
244         return VLC_EGENERIC;
245     }
246
247     /* Reopen file in zip if needed */
248     if( p_access->info.i_pos != 0 )
249     {
250         OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
251     }
252
253     /* Read seek_len data and drop it */
254     int i_seek = 0;
255     int i_read = 1;
256     char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
257     while( ( i_seek < seek_len ) && ( i_read > 0 ) )
258     {
259         i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
260                ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
261         i_read = unzReadCurrentFile( file, p_buffer, i_read );
262         if( i_read < 0 )
263         {
264             msg_Warn( p_access, "could not seek in file" );
265             free( p_buffer );
266             return VLC_EGENERIC;
267         }
268         else
269         {
270             i_seek += i_read;
271         }
272     }
273     free( p_buffer );
274
275     p_access->info.i_pos = unztell( file );
276     return VLC_SUCCESS;
277 }
278
279 /** **************************************************************************
280  * \brief Open file in zip
281  *****************************************************************************/
282 static int OpenFileInZip( access_t *p_access, int i_pos )
283 {
284     access_sys_t *p_sys = p_access->p_sys;
285     unzFile file = p_sys->zipFile;
286     if( !p_sys->psz_fileInzip )
287     {
288         return VLC_EGENERIC;
289     }
290
291     i_pos = __MIN( i_pos, 0 );
292     p_access->info.i_pos = 0;
293
294     unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
295     if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
296     {
297         msg_Err( p_access, "could not [re]locate file in zip: '%s'",
298                  p_sys->psz_fileInzip );
299         return VLC_EGENERIC;
300     }
301     if( unzOpenCurrentFile( file ) != UNZ_OK )
302     {
303         msg_Err( p_access, "could not [re]open file in zip: '%s'",
304                  p_sys->psz_fileInzip );
305         return VLC_EGENERIC;
306     }
307     if( i_pos > 0 )
308         return AccessSeek( p_access, i_pos );
309     else
310         return VLC_SUCCESS;
311 }
312
313 /** **************************************************************************
314  * \brief I/O functions for the ioapi: open (read only)
315  *****************************************************************************/
316 static void* ZCALLBACK ZipIO_Open( void* opaque, const char* file, int mode )
317 {
318     assert(opaque != NULL);
319     assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
320
321     access_t *p_access = (access_t*) opaque;
322
323     return stream_UrlNew( p_access, file );
324 }
325
326 /** **************************************************************************
327  * \brief I/O functions for the ioapi: read
328  *****************************************************************************/
329 static uLong ZCALLBACK ZipIO_Read( void* opaque, void* stream,
330                                    void* buf, uLong size )
331 {
332     (void)opaque;
333     //access_t *p_access = (access_t*) opaque;
334     //msg_Dbg(p_access, "read %d", size);
335     return stream_Read( (stream_t*) stream, buf, size );
336 }
337
338 /** **************************************************************************
339  * \brief I/O functions for the ioapi: write (assert insteadof segfault)
340  *****************************************************************************/
341 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
342                                     const void* buf, uLong size )
343 {
344     (void)opaque; (void)stream; (void)buf; (void)size;
345     int zip_access_cannot_write_this_should_not_happen = 0;
346     assert(zip_access_cannot_write_this_should_not_happen);
347     return 0;
348 }
349
350 /** **************************************************************************
351  * \brief I/O functions for the ioapi: tell
352  *****************************************************************************/
353 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
354 {
355     (void)opaque;
356     int64_t i64_tell = stream_Tell( (stream_t*) stream );
357     //access_t *p_access = (access_t*) opaque;
358     //msg_Dbg(p_access, "tell %" PRIu64, i64_tell);
359     return (long)i64_tell;
360 }
361
362 /** **************************************************************************
363  * \brief I/O functions for the ioapi: seek
364  *****************************************************************************/
365 static long ZCALLBACK ZipIO_Seek( void* opaque, void* stream,
366                                   uLong offset, int origin )
367 {
368     (void)opaque;
369     //access_t *p_access = (access_t*) opaque;
370     int64_t pos = offset;
371     switch( origin )
372     {
373         case SEEK_CUR:
374             pos += stream_Tell( (stream_t*) stream );
375             break;
376         case SEEK_SET:
377             break;
378         case SEEK_END:
379             pos += stream_Size( (stream_t*) stream );
380             break;
381         default:
382             return -1;
383     }
384     //msg_Dbg( p_access, "seek (%d,%d): %" PRIu64, offset, origin, pos );
385     stream_Seek( (stream_t*) stream, pos );
386     /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
387              the stream, which is doable but returns an error in VLC.
388              That's why we always assume this was OK. FIXME */
389     return 0;
390 }
391
392 /** **************************************************************************
393  * \brief I/O functions for the ioapi: close
394  *****************************************************************************/
395 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
396 {
397     (void)opaque;
398     stream_Delete( (stream_t*) stream );
399     return 0;
400 }
401
402 /** **************************************************************************
403  * \brief I/O functions for the ioapi: test error (man 3 ferror)
404  *****************************************************************************/
405 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
406 {
407     (void)opaque;
408     (void)stream;
409     //msg_Dbg( p_access, "error" );
410     return 0;
411 }
412
413
414
415 #else
416 # error Can not compile zip demuxer without zlib support
417 #endif