]> git.sesse.net Git - vlc/blob - modules/access/zip/zipaccess.c
Zip: use !/ as separator instead of |
[vlc] / modules / access / zip / zipaccess.c
1 /*****************************************************************************
2  * zipaccess.c: Module (access) to extract different archives, based on zlib
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
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
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
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 *, int64_t );
53 static int OpenFileInZip( access_t *p_access, int i_pos );
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( !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     p_access->p_sys = p_sys = (access_sys_t*)
109             calloc( 1, sizeof( access_sys_t ) );
110     if( !p_sys )
111         return VLC_ENOMEM;
112
113     /* Split the MRL */
114     psz_path = strdup( p_access->psz_path );
115     psz_sep = strstr( psz_path, ZIP_SEP );
116     if( !psz_sep )
117     {
118         msg_Dbg( p_access, "path does not contain separator " ZIP_SEP );
119         return VLC_EGENERIC;
120     }
121
122     *psz_sep = '\0';
123     psz_pathToZip = unescapeXml( psz_path );
124     if( !psz_pathToZip )
125     {
126         /* Maybe this was not an encoded string */
127         msg_Dbg( p_access, "this is not an encoded url. Trying file '%s'",
128                  psz_path );
129         psz_pathToZip = strdup( psz_path );
130     }
131     p_sys->psz_fileInzip = unescapeXml( psz_sep + ZIP_SEP_LEN );
132     if( !p_sys->psz_fileInzip )
133     {
134         p_sys->psz_fileInzip = strdup( psz_sep + ZIP_SEP_LEN );
135     }
136
137     /* Define IO functions */
138     zlib_filefunc_def *p_func = (zlib_filefunc_def*)
139                                     calloc( 1, sizeof( zlib_filefunc_def ) );
140     p_func->zopen_file   = ZipIO_Open;
141     p_func->zread_file   = ZipIO_Read;
142     p_func->zwrite_file  = ZipIO_Write; // see comment
143     p_func->ztell_file   = ZipIO_Tell;
144     p_func->zseek_file   = ZipIO_Seek;
145     p_func->zclose_file  = ZipIO_Close;
146     p_func->zerror_file  = ZipIO_Error;
147     p_func->opaque       = p_access;
148
149     /* Open zip archive */
150     file = p_access->p_sys->zipFile = unzOpen2( psz_pathToZip, p_func );
151     if( !file )
152     {
153         msg_Err( p_access, "not a valid zip archive: '%s'", psz_pathToZip );
154         goto exit;
155     }
156
157     /* Open file in zip */
158     OpenFileInZip( p_access, 0 );
159
160     /* Set callback */
161     ACCESS_SET_CALLBACKS( AccessRead, NULL, AccessControl, AccessSeek );
162
163     /* Get some infos about current file. Maybe we could want some more ? */
164     unz_file_info z_info;
165     unzGetCurrentFileInfo( file, &z_info, NULL, 0, NULL, 0, NULL, 0 );
166
167     /* Set access informations: size is needed for AccessSeek */
168     p_access->info.i_size = z_info.uncompressed_size;
169     p_access->info.i_pos  = 0;
170     p_access->info.b_eof  = false;
171
172     i_ret = VLC_SUCCESS;
173
174 exit:
175     if( i_ret != VLC_SUCCESS )
176     {
177         if( file )
178         {
179             unzCloseCurrentFile( file );
180             unzClose( file );
181         }
182         free( p_sys->psz_fileInzip );
183         free( p_sys->fileFunctions );
184         free( p_sys );
185     }
186
187     free( psz_pathToZip );
188     free( psz_path );
189     return i_ret;
190 }
191
192 /** **************************************************************************
193  * \brief Close access: free structures
194  *****************************************************************************/
195 void AccessClose( vlc_object_t *p_this )
196 {
197     access_t     *p_access = (access_t*)p_this;
198     access_sys_t *p_sys = p_access->p_sys;
199     if( p_sys )
200     {
201         unzFile file = p_sys->zipFile;
202         if( file )
203         {
204             unzCloseCurrentFile( file );
205             unzClose( file );
206         }
207         free( p_sys->psz_fileInzip );
208         free( p_sys->fileFunctions );
209         free( p_sys );
210     }
211 }
212
213 /** **************************************************************************
214  * \brief Control access
215  *****************************************************************************/
216 static int AccessControl( access_t *p_access, int i_query, va_list args )
217 {
218     bool         *pb_bool;
219     int64_t      *pi_64;
220
221     switch( i_query )
222     {
223         /* */
224         case ACCESS_CAN_SEEK:
225         case ACCESS_CAN_PAUSE:
226         case ACCESS_CAN_CONTROL_PACE:
227             pb_bool = (bool*)va_arg( args, bool* );
228             *pb_bool = true;
229             break;
230
231         case ACCESS_CAN_FASTSEEK:
232             pb_bool = (bool*)va_arg( args, bool* );
233             *pb_bool = false;
234             break;
235
236         case ACCESS_GET_PTS_DELAY:
237             pi_64 = (int64_t*)va_arg( args, int64_t * );
238             *pi_64 = DEFAULT_PTS_DELAY;
239             break;
240
241         /* */
242         case ACCESS_SET_PAUSE_STATE:
243             /* Nothing to do */
244             break;
245
246         case ACCESS_GET_TITLE_INFO:
247         case ACCESS_SET_TITLE:
248         case ACCESS_SET_SEEKPOINT:
249         case ACCESS_SET_PRIVATE_ID_STATE:
250         case ACCESS_GET_META:
251         case ACCESS_GET_PRIVATE_ID_STATE:
252         case ACCESS_GET_CONTENT_TYPE:
253             return VLC_EGENERIC;
254
255         default:
256             msg_Warn( p_access, "unimplemented query %d in control", i_query );
257             return VLC_EGENERIC;
258
259     }
260     return VLC_SUCCESS;
261 }
262
263 /** **************************************************************************
264  * \brief Read access
265  * Reads current opened file in zip. This does not open the file in zip.
266  * Return -1 if no data yet, 0 if no more data, else real data read
267  *****************************************************************************/
268 static ssize_t AccessRead( access_t *p_access, uint8_t *p_buffer, size_t sz )
269 {
270     access_sys_t *p_sys = p_access->p_sys;
271     assert( p_sys );
272     unzFile file = p_sys->zipFile;
273     if( !file )
274     {
275         msg_Err( p_access, "archive not opened !" );
276         return VLC_EGENERIC;
277     }
278
279     int i_read = 0;
280     i_read = unzReadCurrentFile( file, p_buffer, sz );
281
282     p_access->info.i_pos = unztell( file );
283     return ( i_read >= 0 ? i_read : VLC_EGENERIC );
284 }
285
286 /** **************************************************************************
287  * \brief Seek inside zip file
288  *****************************************************************************/
289 static int AccessSeek( access_t *p_access, int64_t seek_len )
290 {
291     access_sys_t *p_sys = p_access->p_sys;
292     assert( p_sys );
293     unzFile file = p_sys->zipFile;
294     if( !file )
295     {
296         msg_Err( p_access, "archive not opened !" );
297         return VLC_EGENERIC;
298     }
299
300     /* Reopen file in zip if needed */
301     if( p_access->info.i_pos != 0 )
302     {
303         OpenFileInZip( p_access, p_access->info.i_pos + seek_len );
304     }
305
306     /* Read seek_len data and drop it */
307     int i_seek = 0;
308     int i_read = 1;
309     char *p_buffer = ( char* ) calloc( 1, ZIP_BUFFER_LEN );
310     while( ( i_seek < seek_len ) && ( i_read > 0 ) )
311     {
312         i_read = ( seek_len - i_seek < ZIP_BUFFER_LEN )
313                ? ( seek_len - i_seek ) : ZIP_BUFFER_LEN;
314         i_read = unzReadCurrentFile( file, p_buffer, i_read );
315         if( i_read < 0 )
316         {
317             msg_Warn( p_access, "could not seek in file" );
318             free( p_buffer );
319             return VLC_EGENERIC;
320         }
321         else
322         {
323             i_seek += i_read;
324         }
325     }
326     free( p_buffer );
327
328     p_access->info.i_pos = unztell( file );
329     return VLC_SUCCESS;
330 }
331
332 /** **************************************************************************
333  * \brief Open file in zip
334  *****************************************************************************/
335 static int OpenFileInZip( access_t *p_access, int i_pos )
336 {
337     access_sys_t *p_sys = p_access->p_sys;
338     unzFile file = p_sys->zipFile;
339     if( !p_sys->psz_fileInzip )
340     {
341         return VLC_EGENERIC;
342     }
343
344     i_pos = __MIN( i_pos, 0 );
345     p_access->info.i_pos = 0;
346
347     unzCloseCurrentFile( file ); /* returns UNZ_PARAMERROR if file not opened */
348     if( unzLocateFile( file, p_sys->psz_fileInzip, 0 ) != UNZ_OK )
349     {
350         msg_Err( p_access, "could not [re]locate file in zip: '%s'",
351                  p_sys->psz_fileInzip );
352         return VLC_EGENERIC;
353     }
354     if( unzOpenCurrentFile( file ) != UNZ_OK )
355     {
356         msg_Err( p_access, "could not [re]open file in zip: '%s'",
357                  p_sys->psz_fileInzip );
358         return VLC_EGENERIC;
359     }
360     if( i_pos > 0 )
361         return AccessSeek( p_access, i_pos );
362     else
363         return VLC_SUCCESS;
364 }
365
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 )
370 {
371     assert(opaque != NULL);
372     assert(mode == (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING));
373
374     access_t *p_access = (access_t*) opaque;
375
376     return stream_UrlNew( p_access, file );
377 }
378
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 )
384 {
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 );
389 }
390
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 )
396 {
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;
401 }
402
403 /** **************************************************************************
404  * \brief I/O functions for the ioapi: tell
405  *****************************************************************************/
406 static long ZCALLBACK ZipIO_Tell( void* opaque, void* stream )
407 {
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;
413 }
414
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 )
420 {
421     (void)opaque;
422     //access_t *p_access = (access_t*) opaque;
423     int64_t pos = offset;
424     switch( origin )
425     {
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;
436     }
437     //msg_Dbg( p_access, "seek (%d,%d): %" PRIu64, offset, origin, pos );
438     stream_Seek( (stream_t*) stream, pos );
439     /* Note: in unzip.c, unzlocal_SearchCentralDir seeks to the end of
440              the stream, which is doable but returns an error in VLC.
441              That's why we always assume this was OK. FIXME */
442     return 0;
443 }
444
445 /** **************************************************************************
446  * \brief I/O functions for the ioapi: close
447  *****************************************************************************/
448 static int ZCALLBACK ZipIO_Close( void* opaque, void* stream )
449 {
450     (void)opaque;
451     stream_Delete( (stream_t*) stream );
452     return 0;
453 }
454
455 /** **************************************************************************
456  * \brief I/O functions for the ioapi: test error (man 3 ferror)
457  *****************************************************************************/
458 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
459 {
460     (void)opaque;
461     (void)stream;
462     //msg_Dbg( p_access, "error" );
463     return 0;
464 }