A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / access / zip / zipaccess.c
blob2f2b4dc167550efb656fe38ee77f5317383b84d6
1 /*****************************************************************************
2 * zipaccess.c: Module (access) to extract different archives, based on zlib
3 *****************************************************************************
4 * Copyright (C) 2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Jean-Philippe André <jpeg@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /** @todo:
25 * - implement crypto (using url zip://user:password@path-to-archive!/file)
26 * - read files in zip with long name (use unz_file_info.size_filename)
27 * - multi-volume archive support ?
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "zip.h"
35 #include <vlc_access.h>
37 /** **************************************************************************
38 * This is our own access_sys_t for zip files
39 *****************************************************************************/
40 struct access_sys_t
42 /* zlib / unzip members */
43 unzFile zipFile;
44 zlib_filefunc_def *fileFunctions;
46 /* file in zip information */
47 char *psz_fileInzip;
50 static int AccessControl( access_t *p_access, int i_query, va_list args );
51 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
52 static int AccessSeek( access_t *, uint64_t );
53 static int OpenFileInZip( access_t *p_access );
54 static char *unescapeXml( const char *psz_text );
56 /** **************************************************************************
57 * \brief Unescape valid XML string
58 * The exact reverse of escapeToXml (zipstream.c)
59 *****************************************************************************/
60 static char *unescapeXml( const char *psz_text )
62 char *psz_ret = malloc( strlen( psz_text ) + 1 );
63 if( unlikely( !psz_ret ) ) return NULL;
65 char *psz_tmp = psz_ret;
66 for( char *psz_iter = (char*) psz_text; *psz_iter; ++psz_iter, ++psz_tmp )
68 if( *psz_iter == '?' )
70 int i_value;
71 if( !sscanf( ++psz_iter, "%02x", &i_value ) )
73 /* Invalid number: URL incorrectly encoded */
74 free( psz_ret );
75 return NULL;
77 *psz_tmp = (char) i_value;
78 psz_iter++;
80 else if( isAllowedChar( *psz_iter ) )
82 *psz_tmp = *psz_iter;
84 else
86 /* Invalid character encoding for the URL */
87 free( psz_ret );
88 return NULL;
91 *psz_tmp = '\0';
93 return psz_ret;
96 /** **************************************************************************
97 * \brief Open access
98 *****************************************************************************/
99 int AccessOpen( vlc_object_t *p_this )
101 access_t *p_access = (access_t*)p_this;
102 access_sys_t *p_sys;
103 int i_ret = VLC_EGENERIC;
105 char *psz_pathToZip = NULL, *psz_path = NULL, *psz_sep = NULL;
107 if( !strstr( p_access->psz_location, ZIP_SEP ) )
109 msg_Dbg( p_access, "location does not contain separator " ZIP_SEP );
110 return VLC_EGENERIC;
113 p_access->p_sys = p_sys = (access_sys_t*)
114 calloc( 1, sizeof( access_sys_t ) );
115 if( unlikely( !p_sys ) )
116 return VLC_ENOMEM;
118 /* Split the MRL */
119 psz_path = xstrdup( p_access->psz_location );
120 psz_sep = strstr( psz_path, ZIP_SEP );
122 *psz_sep = '\0';
123 psz_pathToZip = unescapeXml( psz_path );
124 if( !psz_pathToZip )
126 /* Maybe this was not an encoded string */
127 msg_Dbg( p_access, "not an encoded URL Trying file '%s'",
128 psz_path );
129 psz_pathToZip = strdup( psz_path );
130 if( unlikely( !psz_pathToZip ) )
132 i_ret = VLC_ENOMEM;
133 goto exit;
136 p_sys->psz_fileInzip = unescapeXml( psz_sep + ZIP_SEP_LEN );
137 if( unlikely( !p_sys->psz_fileInzip ) )
139 p_sys->psz_fileInzip = strdup( psz_sep + ZIP_SEP_LEN );
140 if( unlikely( !p_sys->psz_fileInzip ) )
142 i_ret = VLC_ENOMEM;
143 goto exit;
147 /* Define IO functions */
148 zlib_filefunc_def func;
149 func.zopen_file = ZipIO_Open;
150 func.zread_file = ZipIO_Read;
151 func.zwrite_file = ZipIO_Write; // see comment
152 func.ztell_file = ZipIO_Tell;
153 func.zseek_file = ZipIO_Seek;
154 func.zclose_file = ZipIO_Close;
155 func.zerror_file = ZipIO_Error;
156 func.opaque = p_access;
158 /* Open zip archive */
159 p_access->p_sys->zipFile = unzOpen2( psz_pathToZip, &func );
160 if( !p_access->p_sys->zipFile )
162 msg_Err( p_access, "not a valid zip archive: '%s'", psz_pathToZip );
163 i_ret = VLC_EGENERIC;
164 goto exit;
167 /* Open file in zip */
168 if( ( i_ret = OpenFileInZip( p_access ) ) != VLC_SUCCESS )
169 goto exit;
171 /* Set callback */
172 ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
174 p_access->info.i_pos = 0;
175 p_access->info.b_eof = false;
177 i_ret = VLC_SUCCESS;
179 exit:
180 if( i_ret != VLC_SUCCESS )
182 if( p_access->p_sys->zipFile )
184 unzCloseCurrentFile( p_access->p_sys->zipFile );
185 unzClose( p_access->p_sys->zipFile );
187 free( p_sys->psz_fileInzip );
188 free( p_sys->fileFunctions );
189 free( p_sys );
192 free( psz_pathToZip );
193 free( psz_path );
194 return i_ret;
197 /** **************************************************************************
198 * \brief Close access: free structures
199 *****************************************************************************/
200 void AccessClose( vlc_object_t *p_this )
202 access_t *p_access = (access_t*)p_this;
203 access_sys_t *p_sys = p_access->p_sys;
204 if( p_sys )
206 unzFile file = p_sys->zipFile;
207 if( file )
209 unzCloseCurrentFile( file );
210 unzClose( file );
212 free( p_sys->psz_fileInzip );
213 free( p_sys->fileFunctions );
214 free( p_sys );
218 /** **************************************************************************
219 * \brief Control access
220 *****************************************************************************/
221 static int AccessControl( access_t *p_access, int i_query, va_list args )
223 bool *pb_bool;
224 int64_t *pi_64;
226 switch( i_query )
228 case ACCESS_CAN_SEEK:
229 case ACCESS_CAN_PAUSE:
230 case ACCESS_CAN_CONTROL_PACE:
231 pb_bool = (bool*)va_arg( args, bool* );
232 *pb_bool = true;
233 break;
235 case ACCESS_CAN_FASTSEEK:
236 pb_bool = (bool*)va_arg( args, bool* );
237 *pb_bool = false;
238 break;
240 case ACCESS_GET_SIZE:
242 unz_file_info z_info;
244 unzGetCurrentFileInfo( p_access->p_sys->zipFile, &z_info,
245 NULL, 0, NULL, 0, NULL, 0 );
246 *va_arg( args, uint64_t * ) = z_info.uncompressed_size;
247 break;
250 case ACCESS_GET_PTS_DELAY:
251 pi_64 = (int64_t*)va_arg( args, int64_t * );
252 *pi_64 = DEFAULT_PTS_DELAY;
253 break;
255 case ACCESS_SET_PAUSE_STATE:
256 /* Nothing to do */
257 break;
259 default:
260 return VLC_EGENERIC;
262 return VLC_SUCCESS;
265 /** **************************************************************************
266 * \brief Read access
267 * Reads current opened file in zip. This does not open the file in zip.
268 * Return -1 if no data yet, 0 if no more data, else real data read
269 *****************************************************************************/
270 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
272 access_sys_t *p_sys = p_access->p_sys;
273 assert( p_sys );
274 unzFile file = p_sys->zipFile;
275 if( !file )
277 msg_Err( p_access, "archive not opened !" );
278 return VLC_EGENERIC;
281 int i_read = 0;
282 i_read = unzReadCurrentFile( file, p_buffer, sz );
284 p_access->info.i_pos = unztell( file );
285 return ( i_read >= 0 ? i_read : VLC_EGENERIC );
288 /** **************************************************************************
289 * \brief Seek inside zip file
290 *****************************************************************************/
291 static int AccessSeek( access_t *p_access, uint64_t seek_len )
293 access_sys_t *p_sys = p_access->p_sys;
294 assert( p_sys );
295 unzFile file = p_sys->zipFile;
297 if( !file )
299 msg_Err( p_access, "archive not opened !" );
300 return VLC_EGENERIC;
303 /* Reopen file in zip if needed */
304 if( p_access->info.i_pos > seek_len )
306 OpenFileInZip( p_access );
309 /* Read seek_len data and drop it */
310 unsigned i_seek = 0;
311 int i_read = 1;
312 char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
313 if( unlikely( !p_buffer ) )
314 return VLC_EGENERIC;
315 while( ( i_seek < seek_len ) && ( i_read > 0 ) )
317 i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
318 ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
319 i_read = unzReadCurrentFile( file, p_buffer, i_read );
320 if( i_read < 0 )
322 msg_Warn( p_access, "could not seek in file" );
323 free( p_buffer );
324 return VLC_EGENERIC;
326 else
328 i_seek += i_read;
331 free( p_buffer );
333 p_access->info.i_pos = unztell( file );
334 return VLC_SUCCESS;
337 /** **************************************************************************
338 * \brief Open file in zip
339 *****************************************************************************/
340 static int OpenFileInZip( access_t *p_access )
342 access_sys_t *p_sys = p_access->p_sys;
343 unzFile file = p_sys->zipFile;
344 if( !p_sys->psz_fileInzip )
346 return VLC_EGENERIC;
349 p_access->info.i_pos = 0;
351 unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
352 if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
354 msg_Err( p_access, "could not [re]locate file in zip: '%s'",
355 p_sys->psz_fileInzip );
356 return VLC_EGENERIC;
358 if( unzOpenCurrentFile( file ) != UNZ_OK )
360 msg_Err( p_access, "could not [re]open file in zip: '%s'",
361 p_sys->psz_fileInzip );
362 return VLC_EGENERIC;
365 return VLC_SUCCESS;
368 /** **************************************************************************
369 * \brief I/O functions for the ioapi: open (read only)
370 *****************************************************************************/
371 static void* ZCALLBACK ZipIO_Open( void* opaque, const char* file, int mode )
373 assert(opaque != NULL);
374 assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
376 access_t *p_access = (access_t*) opaque;
378 char *fileUri = malloc( strlen(file) + 8 );
379 if( unlikely( !fileUri ) )
380 return NULL;
381 if( !strstr( file, "://" ) )
383 strcpy( fileUri, "file://" );
384 strcat( fileUri, file );
386 else
388 strcpy( fileUri, file );
391 stream_t *s = stream_UrlNew( p_access, fileUri );
392 free( fileUri );
393 return s;
396 /** **************************************************************************
397 * \brief I/O functions for the ioapi: read
398 *****************************************************************************/
399 static uLong ZCALLBACK ZipIO_Read( void* opaque, void* stream,
400 void* buf, uLong size )
402 (void)opaque;
403 //access_t *p_access = (access_t*) opaque;
404 //msg_Dbg(p_access, "read %d", size);
405 return stream_Read( (stream_t*) stream, buf, size );
408 /** **************************************************************************
409 * \brief I/O functions for the ioapi: write (assert insteadof segfault)
410 *****************************************************************************/
411 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
412 const void* buf, uLong size )
414 (void)opaque; (void)stream; (void)buf; (void)size;
415 int zip_access_cannot_write_this_should_not_happen = 0;
416 assert(zip_access_cannot_write_this_should_not_happen);
417 return 0;
420 /** **************************************************************************
421 * \brief I/O functions for the ioapi: tell
422 *****************************************************************************/
423 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
425 (void)opaque;
426 int64_t i64_tell = stream_Tell( (stream_t*) stream );
427 //access_t *p_access = (access_t*) opaque;
428 //msg_Dbg(p_access, "tell %" PRIu64, i64_tell);
429 return (long)i64_tell;
432 /** **************************************************************************
433 * \brief I/O functions for the ioapi: seek
434 *****************************************************************************/
435 static long ZCALLBACK ZipIO_Seek( void* opaque, void* stream,
436 uLong offset, int origin )
438 (void)opaque;
439 int64_t pos = offset;
440 switch( origin )
442 case SEEK_CUR:
443 pos += stream_Tell( (stream_t*) stream );
444 break;
445 case SEEK_SET:
446 break;
447 case SEEK_END:
448 pos += stream_Size( (stream_t*) stream );
449 break;
450 default:
451 return -1;
453 if( pos < 0 )
454 return -1;
455 stream_Seek( (stream_t*) stream, pos );
456 /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
457 the stream, which is doable but returns an error in VLC.
458 That's why we always assume this was OK. FIXME */
459 return 0;
462 /** **************************************************************************
463 * \brief I/O functions for the ioapi: close
464 *****************************************************************************/
465 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
467 (void)opaque;
468 stream_Delete( (stream_t*) stream );
469 return 0;
472 /** **************************************************************************
473 * \brief I/O functions for the ioapi: test error (man 3 ferror)
474 *****************************************************************************/
475 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
477 (void)opaque;
478 (void)stream;
479 //msg_Dbg( p_access, "error" );
480 return 0;