1 /*****************************************************************************
2 * zipstream.c: stream_filter that creates a XSPF playlist from a Zip archive
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
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 /** **************************************************************************
26 *****************************************************************************/
36 #include <vlc_input.h>
38 #define FILENAME_TEXT N_( "Media in Zip" )
39 #define FILENAME_LONGTEXT N_( "Path to the media in the Zip archive" )
41 /** **************************************************************************
43 *****************************************************************************/
45 set_shortname( "Zip" )
46 set_category( CAT_INPUT )
47 set_subcategory( SUBCAT_INPUT_STREAM_FILTER )
48 set_description( N_( "Zip files filter" ) )
49 set_capability( "stream_filter", 1 )
50 set_callbacks( StreamOpen, StreamClose )
52 set_subcategory( SUBCAT_INPUT_ACCESS )
53 set_description( N_( "Zip access" ) )
54 set_capability( "access", 0 )
55 add_shortcut( "unzip" )
57 set_callbacks( AccessOpen, AccessClose )
60 /** *************************************************************************
62 ****************************************************************************/
63 static int Read ( stream_t *, void *p_read, unsigned int i_read );
64 static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
65 static int Control( stream_t *, int i_query, va_list );
67 typedef struct node node;
68 typedef struct item item;
70 static int CreatePlaylist( stream_t *s, char **pp_buffer );
71 static int GetFilesInZip( stream_t*, unzFile, vlc_array_t*, vlc_array_t* );
72 static node* findOrCreateParentNode( node *root, const char *fullpath );
73 static int WriteXSPF( char **pp_buffer, vlc_array_t *p_filenames,
74 const char *psz_zippath );
75 static int nodeToXSPF( char **pp_buffer, node *n, bool b_root );
76 static node* findOrCreateParentNode( node *root, const char *fullpath );
78 /** **************************************************************************
80 *****************************************************************************/
83 /* zlib / unzip members */
85 zlib_filefunc_def *fileFunctions;
106 /** **************************************************************************
108 *****************************************************************************/
110 inline static node* new_node( char *name )
112 node *n = (node*) calloc( 1, sizeof(node) );
113 n->name = convert_xml_special_chars( name );
117 inline static item* new_item( int id )
119 item *media = (item*) calloc( 1, sizeof(item) );
124 inline static void free_all_node( node *root )
128 free_all_node( root->child );
130 node *tmp = root->next;
136 /* Allocate strcat and format */
137 static int astrcatf( char **ppsz_dest, const char *psz_fmt_src, ... )
140 va_start( args, psz_fmt_src );
143 int i_ret = vasprintf( &psz_tmp, psz_fmt_src, args );
144 if( i_ret == -1 ) return -1;
148 int i_len = strlen( *ppsz_dest ) + strlen( psz_tmp ) + 1;
149 char *psz_out = realloc( *ppsz_dest, i_len );
150 if( !psz_out ) return -1;
152 strcat( psz_out, psz_tmp );
155 *ppsz_dest = psz_out;
159 /** **************************************************************************
160 * Zip file identifier
161 *****************************************************************************/
162 static const uint8_t p_zip_marker[] = { 0x50, 0x4b, 0x03, 0x04 }; // "PK^C^D"
163 static const int i_zip_marker = 4;
166 /** **************************************************************************
168 *****************************************************************************/
169 int StreamOpen( vlc_object_t *p_this )
171 stream_t *s = (stream_t*) p_this;
174 /* Verify file format */
175 const uint8_t *p_peek;
176 if( stream_Peek( s->p_source, &p_peek, i_zip_marker ) < i_zip_marker )
178 if( memcmp( p_peek, p_zip_marker, i_zip_marker ) )
181 s->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
187 s->pf_control = Control;
189 p_sys->fileFunctions = ( zlib_filefunc_def * )
190 calloc( 1, sizeof( zlib_filefunc_def ) );
191 if( !p_sys->fileFunctions )
196 p_sys->fileFunctions->zopen_file = ZipIO_Open;
197 p_sys->fileFunctions->zread_file = ZipIO_Read;
198 p_sys->fileFunctions->zwrite_file = ZipIO_Write;
199 p_sys->fileFunctions->ztell_file = ZipIO_Tell;
200 p_sys->fileFunctions->zseek_file = ZipIO_Seek;
201 p_sys->fileFunctions->zclose_file = ZipIO_Close;
202 p_sys->fileFunctions->zerror_file = ZipIO_Error;
203 p_sys->fileFunctions->opaque = ( void * ) s;
204 p_sys->zipFile = unzOpen2( NULL /* path */, p_sys->fileFunctions );
205 if( !p_sys->zipFile )
207 msg_Warn( s, "unable to open file" );
208 free( p_sys->fileFunctions );
213 /* Find the stream uri */
215 if( asprintf( &psz_tmp, "%s.xspf", s->psz_path ) == -1 )
217 free( p_sys->fileFunctions );
221 p_sys->psz_path = s->psz_path;
222 s->psz_path = psz_tmp;
227 /** *************************************************************************
229 ****************************************************************************/
230 void StreamClose( vlc_object_t *p_this )
232 stream_t *s = (stream_t*)p_this;
233 stream_sys_t *p_sys = s->p_sys;
235 free( p_sys->fileFunctions );
236 free( p_sys->psz_xspf );
237 free( p_sys->psz_path );
241 /** *************************************************************************
242 * Stream filters functions
243 ****************************************************************************/
245 /** *************************************************************************
247 ****************************************************************************/
248 static int Read( stream_t *s, void *p_read, unsigned int i_read )
250 stream_sys_t *p_sys = s->p_sys;
252 if( !p_read ) return 0;
254 /* Fill the buffer */
255 if( p_sys->psz_xspf == NULL )
257 int i_ret = CreatePlaylist( s, &p_sys->psz_xspf );
260 p_sys->i_len = strlen( p_sys->psz_xspf );
264 /* Read the buffer */
265 int i_len = __MIN( i_read, p_sys->i_len - p_sys->i_pos );
266 memcpy( p_read, p_sys->psz_xspf + p_sys->i_pos, i_len );
267 p_sys->i_pos += i_len;
272 /** *************************************************************************
274 ****************************************************************************/
275 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
277 stream_sys_t *p_sys = s->p_sys;
279 /* Fill the buffer */
280 if( p_sys->psz_xspf == NULL )
282 int i_ret = CreatePlaylist( s, &p_sys->psz_xspf );
285 p_sys->i_len = strlen( p_sys->psz_xspf );
290 /* Point to the buffer */
291 int i_len = __MIN( i_peek, p_sys->i_len - p_sys->i_pos );
292 *pp_peek = (uint8_t*) p_sys->psz_xspf + p_sys->i_pos;
297 /** *************************************************************************
299 ****************************************************************************/
300 static int Control( stream_t *s, int i_query, va_list args )
302 stream_sys_t *p_sys = s->p_sys;
306 case STREAM_SET_POSITION:
308 uint64_t i_position = va_arg( args, uint64_t );
309 if( i_position >= p_sys->i_len )
313 p_sys->i_pos = (size_t) i_position;
318 case STREAM_GET_POSITION:
320 uint64_t *pi_position = va_arg( args, uint64_t* );
321 *pi_position = p_sys->i_pos;
325 case STREAM_GET_SIZE:
327 uint64_t *pi_size = va_arg( args, uint64_t* );
328 *pi_size = p_sys->i_len;
332 case STREAM_GET_CONTENT_TYPE:
335 case STREAM_UPDATE_SIZE:
336 case STREAM_CONTROL_ACCESS:
337 case STREAM_CAN_SEEK:
338 case STREAM_CAN_FASTSEEK:
339 case STREAM_SET_RECORD_STATE:
340 return stream_vaControl( s->p_source, i_query, args );
347 static int CreatePlaylist( stream_t *s, char **pp_buffer )
349 /* Get some infos about zip archive */
351 unzFile file = s->p_sys->zipFile;
352 vlc_array_t *p_filenames = vlc_array_new(); /* Will contain char* */
354 /* List all file names in Zip archive */
355 i_ret = GetFilesInZip( s, file, p_filenames, NULL );
365 s->p_sys->zipFile = NULL;
367 /* Construct the xspf playlist */
368 i_ret = WriteXSPF( pp_buffer, p_filenames, s->p_sys->psz_path );
375 for( int i = 0; i < vlc_array_count( p_filenames ); i++ )
377 free( vlc_array_item_at_index( p_filenames, i ) );
379 vlc_array_destroy( p_filenames );
384 /** **************************************************************************
385 * Zip utility functions
386 *****************************************************************************/
388 /** **************************************************************************
389 * \brief List files in zip and append their names to p_array
391 * \param file Opened zip file
392 * \param p_array vlc_array_t which will receive all filenames
394 * In case of error, returns VLC_EGENERIC.
395 * In case of success, returns number of files found, and goes back to first file.
396 *****************************************************************************/
397 static int GetFilesInZip( stream_t *p_this, unzFile file,
398 vlc_array_t *p_filenames, vlc_array_t *p_fileinfos )
400 if( !p_filenames || !p_this )
405 /* Get global info */
406 unz_global_info info;
408 if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
410 msg_Warn( p_this, "this is not a valid zip archive" );
414 /* Go to first file in archive */
415 unzGoToFirstFile( file );
417 /* Get info about each file */
418 for( unsigned long i = 0; i < info.number_entry; i++ )
420 char *psz_fileName = calloc( ZIP_FILENAME_LEN, 1 );
421 unz_file_info *p_fileInfo = calloc( 1, sizeof( unz_file_info ) );
423 if( !p_fileInfo || !psz_fileName )
425 free( psz_fileName );
430 if( unzGetCurrentFileInfo( file, p_fileInfo, psz_fileName,
431 ZIP_FILENAME_LEN, NULL, 0, NULL, 0 )
434 msg_Warn( p_this, "can't get info about file in zip" );
439 vlc_array_append( p_filenames, strdup( psz_fileName ) );
440 free( psz_fileName );
443 vlc_array_append( p_fileinfos, p_fileInfo );
447 if( i < ( info.number_entry - 1 ) )
449 /* Go the next file in the archive */
450 if( unzGoToNextFile( file ) != UNZ_OK )
452 msg_Warn( p_this, "can't go to next file in zip" );
460 /* i_ret should be equal to info.number_entry */
461 unzGoToFirstFile( file );
466 /** **************************************************************************
467 * XSPF generation functions
468 *****************************************************************************/
470 /** **************************************************************************
471 * \brief Check a character for allowance in the Xml.
472 * Allowed chars are: a-z, A-Z, 0-9, \, /, ., ' ', _ and :
473 *****************************************************************************/
474 bool isAllowedChar( char c )
476 return ( c >= 'a' && c <= 'z' )
477 || ( c >= 'A' && c <= 'Z' )
478 || ( c >= '0' && c <= '9' )
479 || ( c == ':' ) || ( c == '/' )
480 || ( c == '\\' ) || ( c == '.' )
481 || ( c == ' ' ) || ( c == '_' );
484 /** **************************************************************************
485 * \brief Escape string to be XML valid
486 * Allowed chars are defined by the above function isAllowedChar()
487 * Invalid chars are escaped using non standard '?XX' notation.
488 * NOTE: We cannot trust VLC internal Web encoding functions
489 * because they are not able to encode and decode some rare utf-8
490 * characters properly. Also, we don't control exactly when they are
491 * called (from this module).
492 *****************************************************************************/
493 static int escapeToXml( char **ppsz_encoded, const char *psz_url )
495 char *psz_iter, *psz_tmp;
497 /* Count number of unallowed characters in psz_url */
498 size_t i_num = 0, i_len = 0;
499 for( psz_iter = (char*) psz_url; *psz_iter; ++psz_iter )
501 if( isAllowedChar( *psz_iter ) )
515 *ppsz_encoded = malloc( i_len + 1 );
516 memcpy( *ppsz_encoded, psz_url, i_len + 1 );
520 /* Copy string, replacing invalid characters */
521 char *psz_ret = malloc( i_len + 3*i_num + 2 );
522 if( !psz_ret ) return VLC_ENOMEM;
524 for( psz_iter = (char*) psz_url, psz_tmp = psz_ret;
525 *psz_iter; ++psz_iter, ++psz_tmp )
527 if( isAllowedChar( *psz_iter ) )
529 *psz_tmp = *psz_iter;
534 snprintf( psz_tmp, 3, "%02x", ( *psz_iter & 0x000000FF ) );
541 *ppsz_encoded = psz_ret;
545 /** **************************************************************************
546 * \brief Write the XSPF playlist given the list of files
547 *****************************************************************************/
548 static int WriteXSPF( char **pp_buffer, vlc_array_t *p_filenames,
549 const char *psz_zippath )
551 char *psz_zip = strrchr( psz_zippath, DIR_SEP_CHAR );
552 psz_zip = convert_xml_special_chars( psz_zip ? (psz_zip+1) : psz_zippath );
554 if( asprintf( pp_buffer, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
555 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" "
556 "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
557 " <title>%s</title>\n"
558 " <trackList>\n", psz_zip ) == -1)
562 node *playlist = new_node( psz_zip );
564 /* Encode the URI and append ZIP_SEP */
566 escapeToXml( &psz_pathtozip, psz_zippath );
567 if( astrcatf( &psz_pathtozip, "%s", ZIP_SEP ) < 0 ) return -1;
570 for( int i = 0; i < vlc_array_count( p_filenames ); ++i )
572 char *psz_name = (char*) vlc_array_item_at_index( p_filenames, i );
573 int i_len = strlen( psz_name );
575 if( !i_len ) continue;
577 /* Is it a folder ? */
578 if( psz_name[i_len-1] == '/' )
584 /* Extract file name */
585 char *psz_file = strrchr( psz_name, '/' );
586 psz_file = convert_xml_special_chars( psz_file ?
587 (psz_file+1) : psz_name );
590 char *psz_path = strdup( psz_pathtozip );
591 char *psz_escapedName;
592 escapeToXml( &psz_escapedName, psz_name );
593 if( astrcatf( &psz_path, "%s", psz_escapedName ) < 0 ) return -1;
595 /* Track information */
596 if( astrcatf( pp_buffer,
598 " <location>zip://%s</location>\n"
599 " <title>%s</title>\n"
600 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n"
601 " <vlc:id>%d</vlc:id>\n"
604 psz_path, psz_file, i_track ) < 0 ) return -1;
609 /* Find the parent node */
610 node *parent = findOrCreateParentNode( playlist, psz_name );
613 /* Add the item to this node */
614 item *tmp = parent->media;
617 parent->media = new_item( i_track );
625 tmp->next = new_item( i_track );
632 free( psz_pathtozip );
634 /* Close tracklist, open the extension */
635 if( astrcatf( pp_buffer,
637 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n"
641 if( nodeToXSPF( pp_buffer, playlist, true ) < 0 ) return -1;
643 /* Close extension and playlist */
644 if( astrcatf( pp_buffer, " </extension>\n</playlist>\n" ) < 0 ) return -1;
646 /* printf( "%s", *pp_buffer ); */
648 free_all_node( playlist );
653 /** **************************************************************************
654 * \brief Recursively convert a node to its XSPF representation
655 *****************************************************************************/
656 static int nodeToXSPF( char **pp_buffer, node *n, bool b_root )
660 if( astrcatf( pp_buffer, " <vlc:node title=\"%s\">\n", n->name ) < 0 )
664 nodeToXSPF( pp_buffer, n->child, false );
668 if( astrcatf( pp_buffer, " <vlc:item tid=\"%d\" />\n", i->id ) < 0 )
674 if( astrcatf( pp_buffer, " </vlc:node>\n" ) < 0 )
680 /** **************************************************************************
681 * \brief Either create or find the parent node of the item
682 *****************************************************************************/
683 static node* findOrCreateParentNode( node *root, const char *fullpath )
686 char *path = strdup( fullpath );
691 char *sep = strchr( folder, '/' );
701 node *current = root->child;
705 if( !strcmp( current->name, folder ) )
707 /* We found the folder, go recursively deeper */
708 return findOrCreateParentNode( current, sep );
710 current = current->next;
713 /* If we are here, then it means that we did not find the parent */
714 node *ret = new_node( folder );
719 current = root->child;
720 while( current->next )
722 current = current->next;
727 /* And now, create the subfolders */
728 ret = findOrCreateParentNode( ret, sep );
735 /** **************************************************************************
736 * ZipIO function definitions : how to use vlc_stream to read the zip
737 *****************************************************************************/
739 /** **************************************************************************
740 * \brief interface for unzip module to open a file using a vlc_stream
743 * \param mode how to open the file (read/write ?). We support only read
745 *****************************************************************************/
746 static void ZCALLBACK *ZipIO_Open( void *opaque, const char *file, int mode )
749 stream_t *s = (stream_t*) opaque;
750 if( mode & ( ZLIB_FILEFUNC_MODE_CREATE | ZLIB_FILEFUNC_MODE_WRITE ) )
752 msg_Dbg( s, "ZipIO_Open: we cannot write into zip files" );
758 /** **************************************************************************
759 * \brief read something from stream into buffer
760 * \param opaque should be the stream
761 * \param stream stream created by ZipIO_Open
762 * \param buf buffer to read the file
763 * \param size length of this buffer
764 * \return return the number of bytes read (<= size)
765 *****************************************************************************/
766 static unsigned long ZCALLBACK ZipIO_Read( void *opaque, void *stream,
767 void *buf, unsigned long size )
770 stream_t *s = (stream_t*) opaque;
771 return (unsigned long) stream_Read( s->p_source, buf, (int) size );
774 /** **************************************************************************
775 * \brief tell size of stream
776 * \param opaque should be the stream
777 * \param stream stream created by ZipIO_Open
778 * \return size of the file / stream
779 * ATTENTION: this is not stream_Tell, but stream_Size !
780 *****************************************************************************/
781 static long ZCALLBACK ZipIO_Tell( void *opaque, void *stream )
784 stream_t *s = (stream_t*) opaque;
785 return (long) stream_Size( s->p_source ); /* /!\ not stream_Tell /!\ */
788 /** **************************************************************************
789 * \brief seek in the stream
790 * \param opaque should be the stream
791 * \param stream stream created by ZipIO_Open
792 * \param offset positive offset to seek
793 * \param origin current position in stream
794 * \return ¿ VLC_SUCCESS or an error code ?
795 *****************************************************************************/
796 static long ZCALLBACK ZipIO_Seek ( void *opaque, void *stream,
797 unsigned long offset, int origin )
800 stream_t *s = (stream_t*) opaque;
803 uint64_t pos = offset + origin;
804 l_ret = (long) stream_Seek( s->p_source, pos );
808 /** **************************************************************************
809 * \brief close the stream
810 * \param opaque should be the stream
811 * \param stream stream created by ZipIO_Open
812 * \return always VLC_SUCCESS
813 * This closes zip archive
814 *****************************************************************************/
815 static int ZCALLBACK ZipIO_Close ( void *opaque, void *stream )
819 // stream_t *s = (stream_t*) opaque;
820 // if( p_demux->p_sys && p_demux->p_sys->zipFile )
821 // p_demux->p_sys->zipFile = NULL;
822 // stream_Seek( s->p_source, 0 );
826 /** **************************************************************************
827 * \brief I/O functions for the ioapi: write (assert insteadof segfault)
828 *****************************************************************************/
829 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
830 const void* buf, uLong size )
832 (void)opaque; (void)stream; (void)buf; (void)size;
833 int ERROR_zip_cannot_write_this_should_not_happen = 0;
834 assert( ERROR_zip_cannot_write_this_should_not_happen );
838 /** **************************************************************************
839 * \brief I/O functions for the ioapi: test error (man 3 ferror)
840 *****************************************************************************/
841 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )