1 /*****************************************************************************
2 * zipaccess.c: Module (access) to extract different archives, based on zlib
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: Jean-Philippe André <jpeg@videolan.org>
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.
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.
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 *****************************************************************************/
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 ?
37 #include <vlc_access.h>
39 /** **************************************************************************
40 * This is our own access_sys_t for zip files
41 *****************************************************************************/
44 /* zlib / unzip members */
46 zlib_filefunc_def *fileFunctions;
48 /* file in zip information */
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 );
57 /** **************************************************************************
59 *****************************************************************************/
60 int AccessOpen( vlc_object_t *p_this )
62 access_t *p_access = (access_t*)p_this;
64 int i_ret = VLC_EGENERIC;
67 char *psz_path = NULL, *psz_sep = NULL;
69 p_access->p_sys = p_sys = (access_sys_t*)
70 calloc( 1, sizeof( access_sys_t ) );
75 psz_path = strdup( p_access->psz_path );
76 psz_sep = strchr( psz_path, ZIP_SEP_CHAR );
81 p_sys->psz_fileInzip = strdup( psz_sep + 1 );
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;
95 /* Open zip archive */
96 file = p_access->p_sys->zipFile = unzOpen2( psz_path, p_func );
99 msg_Err( p_access, "not a valid zip archive: '%s'", psz_path );
103 /* Open file in zip */
104 OpenFileInZip( p_access, 0 );
107 ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
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 );
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;
121 if( i_ret != VLC_SUCCESS )
125 unzCloseCurrentFile( file );
128 free( p_sys->psz_fileInzip );
129 free( p_sys->fileFunctions );
137 /** **************************************************************************
138 * \brief Close access: free structures
139 *****************************************************************************/
140 void AccessClose( vlc_object_t *p_this )
142 access_t *p_access = (access_t*)p_this;
143 access_sys_t *p_sys = p_access->p_sys;
146 unzFile file = p_sys->zipFile;
149 unzCloseCurrentFile( file );
152 free( p_sys->psz_fileInzip );
153 free( p_sys->fileFunctions );
158 /** **************************************************************************
159 * \brief Control access
160 *****************************************************************************/
161 static int AccessControl( access_t *p_access, int i_query, va_list args )
169 case ACCESS_CAN_SEEK:
170 case ACCESS_CAN_PAUSE:
171 case ACCESS_CAN_CONTROL_PACE:
172 pb_bool = (bool*)va_arg( args, bool* );
176 case ACCESS_CAN_FASTSEEK:
177 pb_bool = (bool*)va_arg( args, bool* );
181 case ACCESS_GET_PTS_DELAY:
182 pi_64 = (int64_t*)va_arg( args, int64_t * );
183 *pi_64 = DEFAULT_PTS_DELAY;
187 case ACCESS_SET_PAUSE_STATE:
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:
201 msg_Warn( p_access, "unimplemented query %d in control", i_query );
208 /** **************************************************************************
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 )
215 access_sys_t *p_sys = p_access->p_sys;
217 unzFile file = p_sys->zipFile;
220 msg_Err( p_access, "archive not opened !" );
225 i_read = unzReadCurrentFile( file, p_buffer, sz );
227 p_access->info.i_pos = unztell( file );
228 return ( i_read >= 0 ? i_read : VLC_EGENERIC );
231 /** **************************************************************************
232 * \brief Seek inside zip file
233 *****************************************************************************/
234 static int AccessSeek( access_t *p_access, int64_t seek_len )
236 access_sys_t *p_sys = p_access->p_sys;
238 unzFile file = p_sys->zipFile;
241 msg_Err( p_access, "archive not opened !" );
245 /* Reopen file in zip if needed */
246 if( p_access->info.i_pos != 0 )
248 OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
251 /* Read seek_len data and drop it */
254 char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
255 while( ( i_seek < seek_len ) && ( i_read > 0 ) )
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 );
262 msg_Warn( p_access, "could not seek in file" );
273 p_access->info.i_pos = unztell( file );
277 /** **************************************************************************
278 * \brief Open file in zip
279 *****************************************************************************/
280 static int OpenFileInZip( access_t *p_access, int i_pos )
282 access_sys_t *p_sys = p_access->p_sys;
283 unzFile file = p_sys->zipFile;
284 if( !p_sys->psz_fileInzip )
289 i_pos = __MIN( i_pos, 0 );
290 p_access->info.i_pos = 0;
292 unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
293 if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
295 msg_Err( p_access, "could not [re]locate file in zip: '%s'",
296 p_sys->psz_fileInzip );
299 if( unzOpenCurrentFile( file ) != UNZ_OK )
301 msg_Err( p_access, "could not [re]open file in zip: '%s'",
302 p_sys->psz_fileInzip );
306 return AccessSeek( p_access, i_pos );
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 )
316 assert(opaque != NULL);
317 assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
319 access_t *p_access = (access_t*) opaque;
321 return stream_UrlNew( p_access, file );
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 )
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 );
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 )
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);
348 /** **************************************************************************
349 * \brief I/O functions for the ioapi: tell
350 *****************************************************************************/
351 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
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;
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 )
367 //access_t *p_access = (access_t*) opaque;
368 int64_t pos = offset;
372 pos += stream_Tell( (stream_t*) stream );
377 pos += stream_Size( (stream_t*) stream );
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 */
390 /** **************************************************************************
391 * \brief I/O functions for the ioapi: close
392 *****************************************************************************/
393 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
396 stream_Delete( (stream_t*) stream );
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 )
407 //msg_Dbg( p_access, "error" );
414 # error Can not compile zip demuxer without zlib support