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