Rename access_t.psz_path to psz_location
[vlc/asuraparaju-public.git] / modules / access / zip / zipaccess.c
blobf862b0cdd398092c92523b1134b26f99318c1d17
1 /*****************************************************************************
2 * zipaccess.c: Module (access) to extract different archives, based on zlib
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
5 * $Id$
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 *****************************************************************************/
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, uint64_t i_pos );
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( !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;
104 unzFile file = 0;
106 char *psz_pathToZip = NULL, *psz_path = NULL, *psz_sep = NULL;
108 if( !strstr( p_access->psz_location, ZIP_SEP ) )
110 msg_Dbg( p_access, "location does not contain separator " ZIP_SEP );
111 return VLC_EGENERIC;
114 p_access->p_sys = p_sys = (access_sys_t*)
115 calloc( 1, sizeof( access_sys_t ) );
116 if( !p_sys )
117 return VLC_ENOMEM;
119 /* Split the MRL */
120 psz_path = strdup( p_access->psz_location );
121 psz_sep = strstr( psz_path, ZIP_SEP );
123 *psz_sep = '\0';
124 psz_pathToZip = unescapeXml( psz_path );
125 if( !psz_pathToZip )
127 /* Maybe this was not an encoded string */
128 msg_Dbg( p_access, "not an encoded URL Trying file '%s'",
129 psz_path );
130 psz_pathToZip = strdup( psz_path );
132 p_sys->psz_fileInzip = unescapeXml( psz_sep + ZIP_SEP_LEN );
133 if( !p_sys->psz_fileInzip )
135 p_sys->psz_fileInzip = strdup( psz_sep + ZIP_SEP_LEN );
138 /* Define IO functions */
139 zlib_filefunc_def *p_func = (zlib_filefunc_def*)
140 calloc( 1, sizeof( zlib_filefunc_def ) );
141 p_func->zopen_file = ZipIO_Open;
142 p_func->zread_file = ZipIO_Read;
143 p_func->zwrite_file = ZipIO_Write; // see comment
144 p_func->ztell_file = ZipIO_Tell;
145 p_func->zseek_file = ZipIO_Seek;
146 p_func->zclose_file = ZipIO_Close;
147 p_func->zerror_file = ZipIO_Error;
148 p_func->opaque = p_access;
150 /* Open zip archive */
151 file = p_access->p_sys->zipFile = unzOpen2( psz_pathToZip, p_func );
152 if( !file )
154 msg_Err( p_access, "not a valid zip archive: '%s'", psz_pathToZip );
155 goto exit;
158 /* Open file in zip */
159 OpenFileInZip( p_access, 0 );
161 /* Set callback */
162 ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
164 /* Get some infos about current file. Maybe we could want some more ? */
165 unz_file_info z_info;
166 unzGetCurrentFileInfo( file, &z_info, NULL, 0, NULL, 0, NULL, 0 );
168 /* Set access informations: size is needed for AccessSeek */
169 p_access->info.i_size = z_info.uncompressed_size;
170 p_access->info.i_pos = 0;
171 p_access->info.b_eof = false;
173 i_ret = VLC_SUCCESS;
175 exit:
176 if( i_ret != VLC_SUCCESS )
178 if( file )
180 unzCloseCurrentFile( file );
181 unzClose( file );
183 free( p_sys->psz_fileInzip );
184 free( p_sys->fileFunctions );
185 free( p_sys );
188 free( psz_pathToZip );
189 free( psz_path );
190 return i_ret;
193 /** **************************************************************************
194 * \brief Close access: free structures
195 *****************************************************************************/
196 void AccessClose( vlc_object_t *p_this )
198 access_t *p_access = (access_t*)p_this;
199 access_sys_t *p_sys = p_access->p_sys;
200 if( p_sys )
202 unzFile file = p_sys->zipFile;
203 if( file )
205 unzCloseCurrentFile( file );
206 unzClose( file );
208 free( p_sys->psz_fileInzip );
209 free( p_sys->fileFunctions );
210 free( p_sys );
214 /** **************************************************************************
215 * \brief Control access
216 *****************************************************************************/
217 static int AccessControl( access_t *p_access, int i_query, va_list args )
219 bool *pb_bool;
220 int64_t *pi_64;
222 switch( i_query )
224 /* */
225 case ACCESS_CAN_SEEK:
226 case ACCESS_CAN_PAUSE:
227 case ACCESS_CAN_CONTROL_PACE:
228 pb_bool = (bool*)va_arg( args, bool* );
229 *pb_bool = true;
230 break;
232 case ACCESS_CAN_FASTSEEK:
233 pb_bool = (bool*)va_arg( args, bool* );
234 *pb_bool = false;
235 break;
237 case ACCESS_GET_PTS_DELAY:
238 pi_64 = (int64_t*)va_arg( args, int64_t * );
239 *pi_64 = DEFAULT_PTS_DELAY;
240 break;
242 /* */
243 case ACCESS_SET_PAUSE_STATE:
244 /* Nothing to do */
245 break;
247 case ACCESS_GET_TITLE_INFO:
248 case ACCESS_SET_TITLE:
249 case ACCESS_SET_SEEKPOINT:
250 case ACCESS_SET_PRIVATE_ID_STATE:
251 case ACCESS_GET_META:
252 case ACCESS_GET_PRIVATE_ID_STATE:
253 case ACCESS_GET_CONTENT_TYPE:
254 return VLC_EGENERIC;
256 default:
257 msg_Warn( p_access, "unimplemented query %d in control", i_query );
258 return VLC_EGENERIC;
261 return VLC_SUCCESS;
264 /** **************************************************************************
265 * \brief Read access
266 * Reads current opened file in zip. This does not open the file in zip.
267 * Return -1 if no data yet, 0 if no more data, else real data read
268 *****************************************************************************/
269 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
271 access_sys_t *p_sys = p_access->p_sys;
272 assert( p_sys );
273 unzFile file = p_sys->zipFile;
274 if( !file )
276 msg_Err( p_access, "archive not opened !" );
277 return VLC_EGENERIC;
280 int i_read = 0;
281 i_read = unzReadCurrentFile( file, p_buffer, sz );
283 p_access->info.i_pos = unztell( file );
284 return ( i_read >= 0 ? i_read : VLC_EGENERIC );
287 /** **************************************************************************
288 * \brief Seek inside zip file
289 *****************************************************************************/
290 static int AccessSeek( access_t *p_access, uint64_t seek_len )
292 access_sys_t *p_sys = p_access->p_sys;
293 assert( p_sys );
294 unzFile file = p_sys->zipFile;
295 if( !file )
297 msg_Err( p_access, "archive not opened !" );
298 return VLC_EGENERIC;
301 /* Reopen file in zip if needed */
302 if( p_access->info.i_pos != 0 )
304 OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
307 /* Read seek_len data and drop it */
308 unsigned i_seek = 0;
309 int i_read = 1;
310 char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
311 while( ( i_seek < seek_len ) && ( i_read > 0 ) )
313 i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
314 ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
315 i_read = unzReadCurrentFile( file, p_buffer, i_read );
316 if( i_read < 0 )
318 msg_Warn( p_access, "could not seek in file" );
319 free( p_buffer );
320 return VLC_EGENERIC;
322 else
324 i_seek += i_read;
327 free( p_buffer );
329 p_access->info.i_pos = unztell( file );
330 return VLC_SUCCESS;
333 /** **************************************************************************
334 * \brief Open file in zip
335 *****************************************************************************/
336 static int OpenFileInZip( access_t *p_access, uint64_t i_pos )
338 access_sys_t *p_sys = p_access->p_sys;
339 unzFile file = p_sys->zipFile;
340 if( !p_sys->psz_fileInzip )
342 return VLC_EGENERIC;
345 p_access->info.i_pos = 0;
347 unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
348 if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
350 msg_Err( p_access, "could not [re]locate file in zip: '%s'",
351 p_sys->psz_fileInzip );
352 return VLC_EGENERIC;
354 if( unzOpenCurrentFile( file ) != UNZ_OK )
356 msg_Err( p_access, "could not [re]open file in zip: '%s'",
357 p_sys->psz_fileInzip );
358 return VLC_EGENERIC;
360 if( i_pos > 0 )
361 return AccessSeek( p_access, i_pos );
362 else
363 return VLC_SUCCESS;
366 /** **************************************************************************
367 * \brief I/O functions for the ioapi: open (read only)
368 *****************************************************************************/
369 static void* ZCALLBACK ZipIO_Open( void* opaque, const char* file, int mode )
371 assert(opaque != NULL);
372 assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
374 access_t *p_access = (access_t*) opaque;
376 return stream_UrlNew( p_access, file );
379 /** **************************************************************************
380 * \brief I/O functions for the ioapi: read
381 *****************************************************************************/
382 static uLong ZCALLBACK ZipIO_Read( void* opaque, void* stream,
383 void* buf, uLong size )
385 (void)opaque;
386 //access_t *p_access = (access_t*) opaque;
387 //msg_Dbg(p_access, "read %d", size);
388 return stream_Read( (stream_t*) stream, buf, size );
391 /** **************************************************************************
392 * \brief I/O functions for the ioapi: write (assert insteadof segfault)
393 *****************************************************************************/
394 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
395 const void* buf, uLong size )
397 (void)opaque; (void)stream; (void)buf; (void)size;
398 int zip_access_cannot_write_this_should_not_happen = 0;
399 assert(zip_access_cannot_write_this_should_not_happen);
400 return 0;
403 /** **************************************************************************
404 * \brief I/O functions for the ioapi: tell
405 *****************************************************************************/
406 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
408 (void)opaque;
409 int64_t i64_tell = stream_Tell( (stream_t*) stream );
410 //access_t *p_access = (access_t*) opaque;
411 //msg_Dbg(p_access, "tell %" PRIu64, i64_tell);
412 return (long)i64_tell;
415 /** **************************************************************************
416 * \brief I/O functions for the ioapi: seek
417 *****************************************************************************/
418 static long ZCALLBACK ZipIO_Seek( void* opaque, void* stream,
419 uLong offset, int origin )
421 (void)opaque;
422 //access_t *p_access = (access_t*) opaque;
423 int64_t pos = offset;
424 switch( origin )
426 case SEEK_CUR:
427 pos += stream_Tell( (stream_t*) stream );
428 break;
429 case SEEK_SET:
430 break;
431 case SEEK_END:
432 pos += stream_Size( (stream_t*) stream );
433 break;
434 default:
435 return -1;
437 if( pos < 0 )
438 return -1;
439 //msg_Dbg( p_access, "seek (%d,%d): %" PRIu64, offset, origin, pos );
440 stream_Seek( (stream_t*) stream, pos );
441 /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
442 the stream, which is doable but returns an error in VLC.
443 That's why we always assume this was OK. FIXME */
444 return 0;
447 /** **************************************************************************
448 * \brief I/O functions for the ioapi: close
449 *****************************************************************************/
450 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
452 (void)opaque;
453 stream_Delete( (stream_t*) stream );
454 return 0;
457 /** **************************************************************************
458 * \brief I/O functions for the ioapi: test error (man 3 ferror)
459 *****************************************************************************/
460 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
462 (void)opaque;
463 (void)stream;
464 //msg_Dbg( p_access, "error" );
465 return 0;