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_pathToZip = NULL, *psz_path = NULL, *psz_sep = NULL;
69 p_access->p_sys = p_sys = (access_sys_t*)
70 calloc( sizeof( access_sys_t ), 1 );
75 psz_path = strdup( p_access->psz_path );
76 psz_sep = strchr( psz_path, ZIP_SEP_CHAR );
81 psz_pathToZip = unescape_URI_duplicate( psz_path );
82 p_sys->psz_fileInzip = strdup( psz_sep + 1 );
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;
96 /* Open zip archive */
97 file = p_access->p_sys->zipFile = unzOpen2( psz_pathToZip, p_func );
100 msg_Err( p_access, "not a valid zip archive: '%s'", psz_pathToZip );
104 /* Open file in zip */
105 OpenFileInZip( p_access, 0 );
108 ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
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 );
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;
122 if( i_ret != VLC_SUCCESS )
126 unzCloseCurrentFile( file );
129 free( p_sys->psz_fileInzip );
130 free( p_sys->fileFunctions );
134 free( psz_pathToZip );
139 /** **************************************************************************
140 * \brief Close access: free structures
141 *****************************************************************************/
142 void AccessClose( vlc_object_t *p_this )
144 access_t *p_access = (access_t*)p_this;
145 access_sys_t *p_sys = p_access->p_sys;
148 unzFile file = p_sys->zipFile;
151 unzCloseCurrentFile( file );
154 free( p_sys->psz_fileInzip );
155 free( p_sys->fileFunctions );
160 /** **************************************************************************
161 * \brief Control access
162 *****************************************************************************/
163 static int AccessControl( access_t *p_access, int i_query, va_list args )
171 case ACCESS_CAN_SEEK:
172 case ACCESS_CAN_PAUSE:
173 case ACCESS_CAN_CONTROL_PACE:
174 pb_bool = (bool*)va_arg( args, bool* );
178 case ACCESS_CAN_FASTSEEK:
179 pb_bool = (bool*)va_arg( args, bool* );
183 case ACCESS_GET_PTS_DELAY:
184 pi_64 = (int64_t*)va_arg( args, int64_t * );
185 *pi_64 = DEFAULT_PTS_DELAY;
189 case ACCESS_SET_PAUSE_STATE:
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:
203 msg_Warn( p_access, "unimplemented query %d in control", i_query );
210 /** **************************************************************************
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 )
217 access_sys_t *p_sys = p_access->p_sys;
219 unzFile file = p_sys->zipFile;
222 msg_Err( p_access, "archive not opened !" );
227 i_read = unzReadCurrentFile( file, p_buffer, sz );
229 p_access->info.i_pos = unztell( file );
230 return ( i_read >= 0 ? i_read : VLC_EGENERIC );
233 /** **************************************************************************
234 * \brief Seek inside zip file
235 *****************************************************************************/
236 static int AccessSeek( access_t *p_access, int64_t seek_len )
238 access_sys_t *p_sys = p_access->p_sys;
240 unzFile file = p_sys->zipFile;
243 msg_Err( p_access, "archive not opened !" );
247 /* Reopen file in zip if needed */
248 if( p_access->info.i_pos != 0 )
250 OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
253 /* Read seek_len data and drop it */
256 char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
257 while( ( i_seek < seek_len ) && ( i_read > 0 ) )
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 );
264 msg_Warn( p_access, "could not seek in file" );
275 p_access->info.i_pos = unztell( file );
279 /** **************************************************************************
280 * \brief Open file in zip
281 *****************************************************************************/
282 static int OpenFileInZip( access_t *p_access, int i_pos )
284 access_sys_t *p_sys = p_access->p_sys;
285 unzFile file = p_sys->zipFile;
286 if( !p_sys->psz_fileInzip )
291 i_pos = __MIN( i_pos, 0 );
292 p_access->info.i_pos = 0;
294 unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
295 if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
297 msg_Err( p_access, "could not [re]locate file in zip: '%s'",
298 p_sys->psz_fileInzip );
301 if( unzOpenCurrentFile( file ) != UNZ_OK )
303 msg_Err( p_access, "could not [re]open file in zip: '%s'",
304 p_sys->psz_fileInzip );
308 return AccessSeek( p_access, i_pos );
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 )
318 assert(opaque != NULL);
319 assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
321 access_t *p_access = (access_t*) opaque;
323 return stream_UrlNew( p_access, file );
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 )
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 );
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 )
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);
350 /** **************************************************************************
351 * \brief I/O functions for the ioapi: tell
352 *****************************************************************************/
353 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
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;
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 )
369 //access_t *p_access = (access_t*) opaque;
370 int64_t pos = offset;
374 pos += stream_Tell( (stream_t*) stream );
379 pos += stream_Size( (stream_t*) stream );
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 */
392 /** **************************************************************************
393 * \brief I/O functions for the ioapi: close
394 *****************************************************************************/
395 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
398 stream_Delete( (stream_t*) stream );
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 )
409 //msg_Dbg( p_access, "error" );
416 # error Can not compile zip demuxer without zlib support