]> git.sesse.net Git - vlc/blob - modules/access/zip/zipaccess.c
Qt: Make fullscreen bar thinner in one line interface profile.
[vlc] / modules / access / zip / zipaccess.c
1 /*****************************************************************************
2  * zipaccess.c: Module (access) to extract different archives, based on zlib
3  *****************************************************************************
4  * Copyright (C) 2009 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
23
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 ?
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include "zip.h"
35 #include <vlc_access.h>
36
37 /** **************************************************************************
38  * This is our own access_sys_t for zip files
39  *****************************************************************************/
40 struct access_sys_t
41 {
42     /* zlib / unzip members */
43     unzFile            zipFile;
44     zlib_filefunc_def *fileFunctions;
45
46     /* file in zip information */
47     char              *psz_fileInzip;
48 };
49
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 );
55
56 /** **************************************************************************
57  * \brief Unescape valid XML string
58  * The exact reverse of escapeToXml (zipstream.c)
59  *****************************************************************************/
60 static char *unescapeXml( const char *psz_text )
61 {
62     char *psz_ret = malloc( strlen( psz_text ) + 1 );
63     if( unlikely( !psz_ret ) ) return NULL;
64
65     char *psz_tmp = psz_ret;
66     for( char *psz_iter = (char*) psz_text; *psz_iter; ++psz_iter, ++psz_tmp )
67     {
68         if( *psz_iter == '?' )
69         {
70             int i_value;
71             if( !sscanf( ++psz_iter, "%02x", &i_value ) )
72             {
73                 /* Invalid number: URL incorrectly encoded */
74                 free( psz_ret );
75                 return NULL;
76             }
77             *psz_tmp = (char) i_value;
78             psz_iter++;
79         }
80         else if( isAllowedChar( *psz_iter ) )
81         {
82             *psz_tmp = *psz_iter;
83         }
84         else
85         {
86             /* Invalid character encoding for the URL */
87             free( psz_ret );
88             return NULL;
89         }
90     }
91     *psz_tmp = '\0';
92
93     return psz_ret;
94 }
95
96 /** **************************************************************************
97  * \brief Open access
98  *****************************************************************************/
99 int AccessOpen( vlc_object_t *p_this )
100 {
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;
105
106     char *psz_pathToZip = NULL, *psz_path = NULL, *psz_sep = NULL;
107
108     if( !strstr( p_access->psz_location, ZIP_SEP ) )
109     {
110         msg_Dbg( p_access, "location does not contain separator " ZIP_SEP );
111         return VLC_EGENERIC;
112     }
113
114     p_access->p_sys = p_sys = (access_sys_t*)
115             calloc( 1, sizeof( access_sys_t ) );
116     if( unlikely( !p_sys ) )
117         return VLC_ENOMEM;
118
119     /* Split the MRL */
120     psz_path = strdup( p_access->psz_location );
121     psz_sep = strstr( psz_path, ZIP_SEP );
122
123     *psz_sep = '\0';
124     psz_pathToZip = unescapeXml( psz_path );
125     if( !psz_pathToZip )
126     {
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 );
131         if( unlikely( !psz_pathToZip ) )
132         {
133             i_ret = VLC_ENOMEM;
134             goto exit;
135         }
136     }
137     p_sys->psz_fileInzip = unescapeXml( psz_sep + ZIP_SEP_LEN );
138     if( unlikely( !p_sys->psz_fileInzip ) )
139     {
140         p_sys->psz_fileInzip = strdup( psz_sep + ZIP_SEP_LEN );
141         if( unlikely( !p_sys->psz_fileInzip ) )
142         {
143             i_ret = VLC_ENOMEM;
144             goto exit;
145         }
146     }
147
148     /* Define IO functions */
149     zlib_filefunc_def *p_func = (zlib_filefunc_def*)
150                                     calloc( 1, sizeof( zlib_filefunc_def ) );
151     if( unlikely( !p_func ) )
152     {
153         i_ret = VLC_ENOMEM;
154         goto exit;
155     }
156     p_func->zopen_file   = ZipIO_Open;
157     p_func->zread_file   = ZipIO_Read;
158     p_func->zwrite_file  = ZipIO_Write; // see comment
159     p_func->ztell_file   = ZipIO_Tell;
160     p_func->zseek_file   = ZipIO_Seek;
161     p_func->zclose_file  = ZipIO_Close;
162     p_func->zerror_file  = ZipIO_Error;
163     p_func->opaque       = p_access;
164
165     /* Open zip archive */
166     file = p_access->p_sys->zipFile = unzOpen2( psz_pathToZip, p_func );
167     if( !file )
168     {
169         msg_Err( p_access, "not a valid zip archive: '%s'", psz_pathToZip );
170         i_ret = VLC_EGENERIC;
171         goto exit;
172     }
173
174     /* Open file in zip */
175     if( ( i_ret = OpenFileInZip( p_access ) ) != VLC_SUCCESS )
176         goto exit;
177
178     /* Set callback */
179     ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
180
181     /* Get some infos about current file. Maybe we could want some more ? */
182     unz_file_info z_info;
183     unzGetCurrentFileInfo( file, &z_info, NULL, 0, NULL, 0, NULL, 0 );
184
185     /* Set access information: size is needed for AccessSeek */
186     p_access->info.i_size = z_info.uncompressed_size;
187     p_access->info.i_pos  = 0;
188     p_access->info.b_eof  = false;
189
190     i_ret = VLC_SUCCESS;
191
192 exit:
193     if( i_ret != VLC_SUCCESS )
194     {
195         if( file )
196         {
197             unzCloseCurrentFile( file );
198             unzClose( file );
199         }
200         free( p_sys->psz_fileInzip );
201         free( p_sys->fileFunctions );
202         free( p_sys );
203     }
204
205     free( psz_pathToZip );
206     free( psz_path );
207     return i_ret;
208 }
209
210 /** **************************************************************************
211  * \brief Close access: free structures
212  *****************************************************************************/
213 void AccessClose( vlc_object_t *p_this )
214 {
215     access_t     *p_access = (access_t*)p_this;
216     access_sys_t *p_sys = p_access->p_sys;
217     if( p_sys )
218     {
219         unzFile file = p_sys->zipFile;
220         if( file )
221         {
222             unzCloseCurrentFile( file );
223             unzClose( file );
224         }
225         free( p_sys->psz_fileInzip );
226         free( p_sys->fileFunctions );
227         free( p_sys );
228     }
229 }
230
231 /** **************************************************************************
232  * \brief Control access
233  *****************************************************************************/
234 static int AccessControl( access_t *p_access, int i_query, va_list args )
235 {
236     bool         *pb_bool;
237     int64_t      *pi_64;
238
239     switch( i_query )
240     {
241         /* */
242         case ACCESS_CAN_SEEK:
243         case ACCESS_CAN_PAUSE:
244         case ACCESS_CAN_CONTROL_PACE:
245             pb_bool = (bool*)va_arg( args, bool* );
246             *pb_bool = true;
247             break;
248
249         case ACCESS_CAN_FASTSEEK:
250             pb_bool = (bool*)va_arg( args, bool* );
251             *pb_bool = false;
252             break;
253
254         case ACCESS_GET_PTS_DELAY:
255             pi_64 = (int64_t*)va_arg( args, int64_t * );
256             *pi_64 = DEFAULT_PTS_DELAY;
257             break;
258
259         /* */
260         case ACCESS_SET_PAUSE_STATE:
261             /* Nothing to do */
262             break;
263
264         case ACCESS_GET_TITLE_INFO:
265         case ACCESS_SET_TITLE:
266         case ACCESS_SET_SEEKPOINT:
267         case ACCESS_SET_PRIVATE_ID_STATE:
268         case ACCESS_GET_META:
269         case ACCESS_GET_PRIVATE_ID_STATE:
270         case ACCESS_GET_CONTENT_TYPE:
271             return VLC_EGENERIC;
272
273         default:
274             msg_Warn( p_access, "unimplemented query %d in control", i_query );
275             return VLC_EGENERIC;
276
277     }
278     return VLC_SUCCESS;
279 }
280
281 /** **************************************************************************
282  * \brief Read access
283  * Reads current opened file in zip. This does not open the file in zip.
284  * Return -1 if no data yet, 0 if no more data, else real data read
285  *****************************************************************************/
286 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
287 {
288     access_sys_t *p_sys = p_access->p_sys;
289     assert( p_sys );
290     unzFile file = p_sys->zipFile;
291     if( !file )
292     {
293         msg_Err( p_access, "archive not opened !" );
294         return VLC_EGENERIC;
295     }
296
297     int i_read = 0;
298     i_read = unzReadCurrentFile( file, p_buffer, sz );
299
300     p_access->info.i_pos = unztell( file );
301     return ( i_read >= 0 ? i_read : VLC_EGENERIC );
302 }
303
304 /** **************************************************************************
305  * \brief Seek inside zip file
306  *****************************************************************************/
307 static int AccessSeek( access_t *p_access, uint64_t seek_len )
308 {
309     access_sys_t *p_sys = p_access->p_sys;
310     assert( p_sys );
311     unzFile file = p_sys->zipFile;
312
313     if( !file )
314     {
315         msg_Err( p_access, "archive not opened !" );
316         return VLC_EGENERIC;
317     }
318
319     /* Reopen file in zip if needed */
320     if( p_access->info.i_pos > seek_len )
321     {
322         OpenFileInZip( p_access );
323     }
324
325     /* Read seek_len data and drop it */
326     unsigned i_seek = 0;
327     int i_read = 1;
328     char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
329     if( unlikely( !p_buffer ) )
330         return VLC_EGENERIC;
331     while( ( i_seek < seek_len ) && ( i_read > 0 ) )
332     {
333         i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
334                ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
335         i_read = unzReadCurrentFile( file, p_buffer, i_read );
336         if( i_read < 0 )
337         {
338             msg_Warn( p_access, "could not seek in file" );
339             free( p_buffer );
340             return VLC_EGENERIC;
341         }
342         else
343         {
344             i_seek += i_read;
345         }
346     }
347     free( p_buffer );
348
349     p_access->info.i_pos = unztell( file );
350     return VLC_SUCCESS;
351 }
352
353 /** **************************************************************************
354  * \brief Open file in zip
355  *****************************************************************************/
356 static int OpenFileInZip( access_t *p_access )
357 {
358     access_sys_t *p_sys = p_access->p_sys;
359     unzFile file = p_sys->zipFile;
360     if( !p_sys->psz_fileInzip )
361     {
362         return VLC_EGENERIC;
363     }
364
365     p_access->info.i_pos = 0;
366
367     unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
368     if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
369     {
370         msg_Err( p_access, "could not [re]locate file in zip: '%s'",
371                  p_sys->psz_fileInzip );
372         return VLC_EGENERIC;
373     }
374     if( unzOpenCurrentFile( file ) != UNZ_OK )
375     {
376         msg_Err( p_access, "could not [re]open file in zip: '%s'",
377                  p_sys->psz_fileInzip );
378         return VLC_EGENERIC;
379     }
380
381     return VLC_SUCCESS;
382 }
383
384 /** **************************************************************************
385  * \brief I/O functions for the ioapi: open (read only)
386  *****************************************************************************/
387 static void* ZCALLBACK ZipIO_Open( void* opaque, const char* file, int mode )
388 {
389     assert(opaque != NULL);
390     assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
391
392     access_t *p_access = (access_t*) opaque;
393
394     char *fileUri = malloc( strlen(file) + 8 );
395     if( unlikely( !fileUri ) )
396         return NULL;
397     if( !strstr( file, "://" ) )
398     {
399         strcpy( fileUri, "file://" );
400         strcat( fileUri, file );
401     }
402     else
403     {
404         strcpy( fileUri, file );
405     }
406
407     stream_t *s = stream_UrlNew( p_access, fileUri );
408     free( fileUri );
409     return s;
410 }
411
412 /** **************************************************************************
413  * \brief I/O functions for the ioapi: read
414  *****************************************************************************/
415 static uLong ZCALLBACK ZipIO_Read( void* opaque, void* stream,
416                                    void* buf, uLong size )
417 {
418     (void)opaque;
419     //access_t *p_access = (access_t*) opaque;
420     //msg_Dbg(p_access, "read %d", size);
421     return stream_Read( (stream_t*) stream, buf, size );
422 }
423
424 /** **************************************************************************
425  * \brief I/O functions for the ioapi: write (assert insteadof segfault)
426  *****************************************************************************/
427 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
428                                     const void* buf, uLong size )
429 {
430     (void)opaque; (void)stream; (void)buf; (void)size;
431     int zip_access_cannot_write_this_should_not_happen = 0;
432     assert(zip_access_cannot_write_this_should_not_happen);
433     return 0;
434 }
435
436 /** **************************************************************************
437  * \brief I/O functions for the ioapi: tell
438  *****************************************************************************/
439 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
440 {
441     (void)opaque;
442     int64_t i64_tell = stream_Tell( (stream_t*) stream );
443     //access_t *p_access = (access_t*) opaque;
444     //msg_Dbg(p_access, "tell %" PRIu64, i64_tell);
445     return (long)i64_tell;
446 }
447
448 /** **************************************************************************
449  * \brief I/O functions for the ioapi: seek
450  *****************************************************************************/
451 static long ZCALLBACK ZipIO_Seek( void* opaque, void* stream,
452                                   uLong offset, int origin )
453 {
454     (void)opaque;
455     int64_t pos = offset;
456     switch( origin )
457     {
458         case SEEK_CUR:
459             pos += stream_Tell( (stream_t*) stream );
460             break;
461         case SEEK_SET:
462             break;
463         case SEEK_END:
464             pos += stream_Size( (stream_t*) stream );
465             break;
466         default:
467             return -1;
468     }
469     if( pos < 0 )
470         return -1;
471     stream_Seek( (stream_t*) stream, pos );
472     /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
473              the stream, which is doable but returns an error in VLC.
474              That's why we always assume this was OK. FIXME */
475     return 0;
476 }
477
478 /** **************************************************************************
479  * \brief I/O functions for the ioapi: close
480  *****************************************************************************/
481 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
482 {
483     (void)opaque;
484     stream_Delete( (stream_t*) stream );
485     return 0;
486 }
487
488 /** **************************************************************************
489  * \brief I/O functions for the ioapi: test error (man 3 ferror)
490  *****************************************************************************/
491 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
492 {
493     (void)opaque;
494     (void)stream;
495     //msg_Dbg( p_access, "error" );
496     return 0;
497 }