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 Fill( stream_t * );
71 static int CreatePlaylist( stream_t *s, char **pp_buffer );
72 static int GetFilesInZip( stream_t*, unzFile, vlc_array_t*, vlc_array_t* );
73 static node* findOrCreateParentNode( node *root, const char *fullpath );
74 static int WriteXSPF( char **pp_buffer, vlc_array_t *p_filenames,
75 const char *psz_zippath );
76 static int nodeToXSPF( char **pp_buffer, node *n, bool b_root );
77 static node* findOrCreateParentNode( node *root, const char *fullpath );
79 /** **************************************************************************
81 *****************************************************************************/
84 /* zlib / unzip members */
86 zlib_filefunc_def *fileFunctions;
107 /** **************************************************************************
109 *****************************************************************************/
111 inline static node* new_node( char *name )
113 node *n = (node*) calloc( 1, sizeof(node) );
114 n->name = convert_xml_special_chars( name );
118 inline static item* new_item( int id )
120 item *media = (item*) calloc( 1, sizeof(item) );
125 inline static void free_all_node( node *root )
129 free_all_node( root->child );
131 node *tmp = root->next;
137 /* Allocate strcat and format */
138 static int astrcatf( char **ppsz_dest, const char *psz_fmt_src, ... )
141 va_start( args, psz_fmt_src );
144 int i_ret = vasprintf( &psz_tmp, psz_fmt_src, args );
145 if( i_ret == -1 ) return -1;
149 int i_len = strlen( *ppsz_dest ) + strlen( psz_tmp ) + 1;
150 char *psz_out = realloc( *ppsz_dest, i_len );
151 if( !psz_out ) return -1;
153 strcat( psz_out, psz_tmp );
156 *ppsz_dest = psz_out;
160 /** **************************************************************************
161 * Zip file identifier
162 *****************************************************************************/
163 static const uint8_t p_zip_marker[] = { 0x50, 0x4b, 0x03, 0x04 }; // "PK^C^D"
164 static const int i_zip_marker = 4;
167 /** **************************************************************************
169 *****************************************************************************/
170 int StreamOpen( vlc_object_t *p_this )
172 stream_t *s = (stream_t*) p_this;
175 /* Verify file format */
176 const uint8_t *p_peek;
177 if( stream_Peek( s->p_source, &p_peek, i_zip_marker ) < i_zip_marker )
179 if( memcmp( p_peek, p_zip_marker, i_zip_marker ) )
182 s->p_sys = p_sys = calloc( 1, sizeof( *p_sys ) );
188 s->pf_control = Control;
190 p_sys->fileFunctions = ( zlib_filefunc_def * )
191 calloc( 1, sizeof( zlib_filefunc_def ) );
192 if( !p_sys->fileFunctions )
197 p_sys->fileFunctions->zopen_file = ZipIO_Open;
198 p_sys->fileFunctions->zread_file = ZipIO_Read;
199 p_sys->fileFunctions->zwrite_file = ZipIO_Write;
200 p_sys->fileFunctions->ztell_file = ZipIO_Tell;
201 p_sys->fileFunctions->zseek_file = ZipIO_Seek;
202 p_sys->fileFunctions->zclose_file = ZipIO_Close;
203 p_sys->fileFunctions->zerror_file = ZipIO_Error;
204 p_sys->fileFunctions->opaque = ( void * ) s;
205 p_sys->zipFile = unzOpen2( NULL /* path */, p_sys->fileFunctions );
206 if( !p_sys->zipFile )
208 msg_Warn( s, "unable to open file" );
209 free( p_sys->fileFunctions );
214 /* Find the stream uri */
216 if( asprintf( &psz_tmp, "%s.xspf", s->psz_path ) == -1 )
218 free( p_sys->fileFunctions );
222 p_sys->psz_path = s->psz_path;
223 s->psz_path = psz_tmp;
228 /** *************************************************************************
230 ****************************************************************************/
231 void StreamClose( vlc_object_t *p_this )
233 stream_t *s = (stream_t*)p_this;
234 stream_sys_t *p_sys = s->p_sys;
237 unzClose( p_sys->zipFile );
239 free( p_sys->fileFunctions );
240 free( p_sys->psz_xspf );
241 free( p_sys->psz_path );
245 /** *************************************************************************
246 * Stream filters functions
247 ****************************************************************************/
249 /** *************************************************************************
251 ****************************************************************************/
252 static int Read( stream_t *s, void *p_read, unsigned int i_read )
254 stream_sys_t *p_sys = s->p_sys;
256 /* Fill the buffer */
260 /* Read the buffer */
261 unsigned i_len = __MIN( i_read, p_sys->i_len - p_sys->i_pos );
263 memcpy( p_read, p_sys->psz_xspf + p_sys->i_pos, i_len );
264 p_sys->i_pos += i_len;
269 /** *************************************************************************
271 ****************************************************************************/
272 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
274 stream_sys_t *p_sys = s->p_sys;
276 /* Fill the buffer */
280 /* Point to the buffer */
281 int i_len = __MIN( i_peek, p_sys->i_len - p_sys->i_pos );
282 *pp_peek = (uint8_t*) p_sys->psz_xspf + p_sys->i_pos;
287 /** *************************************************************************
289 ****************************************************************************/
290 static int Control( stream_t *s, int i_query, va_list args )
292 stream_sys_t *p_sys = s->p_sys;
296 case STREAM_SET_POSITION:
298 uint64_t i_position = va_arg( args, uint64_t );
299 if( i_position >= p_sys->i_len )
303 p_sys->i_pos = (size_t) i_position;
308 case STREAM_GET_POSITION:
310 uint64_t *pi_position = va_arg( args, uint64_t* );
311 *pi_position = p_sys->i_pos;
315 case STREAM_GET_SIZE:
317 uint64_t *pi_size = va_arg( args, uint64_t* );
318 *pi_size = p_sys->i_len;
322 case STREAM_GET_CONTENT_TYPE:
325 case STREAM_UPDATE_SIZE:
326 case STREAM_CONTROL_ACCESS:
327 case STREAM_CAN_SEEK:
328 case STREAM_CAN_FASTSEEK:
329 case STREAM_SET_RECORD_STATE:
330 return stream_vaControl( s->p_source, i_query, args );
337 static int Fill( stream_t *s )
339 stream_sys_t *p_sys = s->p_sys;
341 if( p_sys->psz_xspf )
344 if( CreatePlaylist( s, &p_sys->psz_xspf ) < 0 )
347 p_sys->i_len = strlen( p_sys->psz_xspf );
352 static int CreatePlaylist( stream_t *s, char **pp_buffer )
354 stream_sys_t *p_sys = s->p_sys;
356 unzFile file = p_sys->zipFile;
360 /* Get some infos about zip archive */
362 vlc_array_t *p_filenames = vlc_array_new(); /* Will contain char* */
364 /* List all file names in Zip archive */
365 i_ret = GetFilesInZip( s, file, p_filenames, NULL );
372 /* Construct the xspf playlist */
373 i_ret = WriteXSPF( pp_buffer, p_filenames, p_sys->psz_path );
382 p_sys->zipFile = NULL;
384 for( int i = 0; i < vlc_array_count( p_filenames ); i++ )
386 free( vlc_array_item_at_index( p_filenames, i ) );
388 vlc_array_destroy( p_filenames );
393 /** **************************************************************************
394 * Zip utility functions
395 *****************************************************************************/
397 /** **************************************************************************
398 * \brief List files in zip and append their names to p_array
400 * \param file Opened zip file
401 * \param p_array vlc_array_t which will receive all filenames
403 * In case of error, returns VLC_EGENERIC.
404 * In case of success, returns number of files found, and goes back to first file.
405 *****************************************************************************/
406 static int GetFilesInZip( stream_t *p_this, unzFile file,
407 vlc_array_t *p_filenames, vlc_array_t *p_fileinfos )
409 if( !p_filenames || !p_this )
414 /* Get global info */
415 unz_global_info info;
417 if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
419 msg_Warn( p_this, "this is not a valid zip archive" );
423 /* Go to first file in archive */
424 unzGoToFirstFile( file );
426 /* Get info about each file */
427 for( unsigned long i = 0; i < info.number_entry; i++ )
429 char *psz_fileName = calloc( ZIP_FILENAME_LEN, 1 );
430 unz_file_info *p_fileInfo = calloc( 1, sizeof( unz_file_info ) );
432 if( !p_fileInfo || !psz_fileName )
434 free( psz_fileName );
439 if( unzGetCurrentFileInfo( file, p_fileInfo, psz_fileName,
440 ZIP_FILENAME_LEN, NULL, 0, NULL, 0 )
443 msg_Warn( p_this, "can't get info about file in zip" );
448 vlc_array_append( p_filenames, strdup( psz_fileName ) );
449 free( psz_fileName );
452 vlc_array_append( p_fileinfos, p_fileInfo );
456 if( i < ( info.number_entry - 1 ) )
458 /* Go the next file in the archive */
459 if( unzGoToNextFile( file ) != UNZ_OK )
461 msg_Warn( p_this, "can't go to next file in zip" );
469 /* i_ret should be equal to info.number_entry */
470 unzGoToFirstFile( file );
475 /** **************************************************************************
476 * XSPF generation functions
477 *****************************************************************************/
479 /** **************************************************************************
480 * \brief Check a character for allowance in the Xml.
481 * Allowed chars are: a-z, A-Z, 0-9, \, /, ., ' ', _ and :
482 *****************************************************************************/
483 bool isAllowedChar( char c )
485 return ( c >= 'a' && c <= 'z' )
486 || ( c >= 'A' && c <= 'Z' )
487 || ( c >= '0' && c <= '9' )
488 || ( c == ':' ) || ( c == '/' )
489 || ( c == '\\' ) || ( c == '.' )
490 || ( c == ' ' ) || ( c == '_' );
493 /** **************************************************************************
494 * \brief Escape string to be XML valid
495 * Allowed chars are defined by the above function isAllowedChar()
496 * Invalid chars are escaped using non standard '?XX' notation.
497 * NOTE: We cannot trust VLC internal Web encoding functions
498 * because they are not able to encode and decode some rare utf-8
499 * characters properly. Also, we don't control exactly when they are
500 * called (from this module).
501 *****************************************************************************/
502 static int escapeToXml( char **ppsz_encoded, const char *psz_url )
504 char *psz_iter, *psz_tmp;
506 /* Count number of unallowed characters in psz_url */
507 size_t i_num = 0, i_len = 0;
508 for( psz_iter = (char*) psz_url; *psz_iter; ++psz_iter )
510 if( isAllowedChar( *psz_iter ) )
524 *ppsz_encoded = malloc( i_len + 1 );
525 memcpy( *ppsz_encoded, psz_url, i_len + 1 );
529 /* Copy string, replacing invalid characters */
530 char *psz_ret = malloc( i_len + 3*i_num + 2 );
531 if( !psz_ret ) return VLC_ENOMEM;
533 for( psz_iter = (char*) psz_url, psz_tmp = psz_ret;
534 *psz_iter; ++psz_iter, ++psz_tmp )
536 if( isAllowedChar( *psz_iter ) )
538 *psz_tmp = *psz_iter;
543 snprintf( psz_tmp, 3, "%02x", ( *psz_iter & 0x000000FF ) );
550 *ppsz_encoded = psz_ret;
554 /** **************************************************************************
555 * \brief Write the XSPF playlist given the list of files
556 *****************************************************************************/
557 static int WriteXSPF( char **pp_buffer, vlc_array_t *p_filenames,
558 const char *psz_zippath )
560 char *psz_zip = strrchr( psz_zippath, DIR_SEP_CHAR );
561 psz_zip = convert_xml_special_chars( psz_zip ? (psz_zip+1) : psz_zippath );
563 if( asprintf( pp_buffer, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
564 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" "
565 "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
566 " <title>%s</title>\n"
567 " <trackList>\n", psz_zip ) == -1)
571 node *playlist = new_node( psz_zip );
573 /* Encode the URI and append ZIP_SEP */
575 escapeToXml( &psz_pathtozip, psz_zippath );
576 if( astrcatf( &psz_pathtozip, "%s", ZIP_SEP ) < 0 ) return -1;
579 for( int i = 0; i < vlc_array_count( p_filenames ); ++i )
581 char *psz_name = (char*) vlc_array_item_at_index( p_filenames, i );
582 int i_len = strlen( psz_name );
584 if( !i_len ) continue;
586 /* Is it a folder ? */
587 if( psz_name[i_len-1] == '/' )
593 /* Extract file name */
594 char *psz_file = strrchr( psz_name, '/' );
595 psz_file = convert_xml_special_chars( psz_file ?
596 (psz_file+1) : psz_name );
599 char *psz_path = strdup( psz_pathtozip );
600 char *psz_escapedName;
601 escapeToXml( &psz_escapedName, psz_name );
602 if( astrcatf( &psz_path, "%s", psz_escapedName ) < 0 ) return -1;
604 /* Track information */
605 if( astrcatf( pp_buffer,
607 " <location>zip://%s</location>\n"
608 " <title>%s</title>\n"
609 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n"
610 " <vlc:id>%d</vlc:id>\n"
613 psz_path, psz_file, i_track ) < 0 ) return -1;
618 /* Find the parent node */
619 node *parent = findOrCreateParentNode( playlist, psz_name );
622 /* Add the item to this node */
623 item *tmp = parent->media;
626 parent->media = new_item( i_track );
634 tmp->next = new_item( i_track );
641 free( psz_pathtozip );
643 /* Close tracklist, open the extension */
644 if( astrcatf( pp_buffer,
646 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n"
650 if( nodeToXSPF( pp_buffer, playlist, true ) < 0 ) return -1;
652 /* Close extension and playlist */
653 if( astrcatf( pp_buffer, " </extension>\n</playlist>\n" ) < 0 ) return -1;
655 /* printf( "%s", *pp_buffer ); */
657 free_all_node( playlist );
662 /** **************************************************************************
663 * \brief Recursively convert a node to its XSPF representation
664 *****************************************************************************/
665 static int nodeToXSPF( char **pp_buffer, node *n, bool b_root )
669 if( astrcatf( pp_buffer, " <vlc:node title=\"%s\">\n", n->name ) < 0 )
673 nodeToXSPF( pp_buffer, n->child, false );
677 if( astrcatf( pp_buffer, " <vlc:item tid=\"%d\" />\n", i->id ) < 0 )
683 if( astrcatf( pp_buffer, " </vlc:node>\n" ) < 0 )
689 /** **************************************************************************
690 * \brief Either create or find the parent node of the item
691 *****************************************************************************/
692 static node* findOrCreateParentNode( node *root, const char *fullpath )
695 char *path = strdup( fullpath );
700 char *sep = strchr( folder, '/' );
710 node *current = root->child;
714 if( !strcmp( current->name, folder ) )
716 /* We found the folder, go recursively deeper */
717 return findOrCreateParentNode( current, sep );
719 current = current->next;
722 /* If we are here, then it means that we did not find the parent */
723 node *ret = new_node( folder );
728 current = root->child;
729 while( current->next )
731 current = current->next;
736 /* And now, create the subfolders */
737 ret = findOrCreateParentNode( ret, sep );
744 /** **************************************************************************
745 * ZipIO function definitions : how to use vlc_stream to read the zip
746 *****************************************************************************/
748 /** **************************************************************************
749 * \brief interface for unzip module to open a file using a vlc_stream
752 * \param mode how to open the file (read/write ?). We support only read
754 *****************************************************************************/
755 static void ZCALLBACK *ZipIO_Open( void *opaque, const char *file, int mode )
758 stream_t *s = (stream_t*) opaque;
759 if( mode & ( ZLIB_FILEFUNC_MODE_CREATE | ZLIB_FILEFUNC_MODE_WRITE ) )
761 msg_Dbg( s, "ZipIO_Open: we cannot write into zip files" );
767 /** **************************************************************************
768 * \brief read something from stream into buffer
769 * \param opaque should be the stream
770 * \param stream stream created by ZipIO_Open
771 * \param buf buffer to read the file
772 * \param size length of this buffer
773 * \return return the number of bytes read (<= size)
774 *****************************************************************************/
775 static unsigned long ZCALLBACK ZipIO_Read( void *opaque, void *stream,
776 void *buf, unsigned long size )
779 stream_t *s = (stream_t*) opaque;
780 return (unsigned long) stream_Read( s->p_source, buf, (int) size );
783 /** **************************************************************************
784 * \brief tell size of stream
785 * \param opaque should be the stream
786 * \param stream stream created by ZipIO_Open
787 * \return size of the file / stream
788 * ATTENTION: this is not stream_Tell, but stream_Size !
789 *****************************************************************************/
790 static long ZCALLBACK ZipIO_Tell( void *opaque, void *stream )
793 stream_t *s = (stream_t*) opaque;
794 return (long) stream_Size( s->p_source ); /* /!\ not stream_Tell /!\ */
797 /** **************************************************************************
798 * \brief seek in the stream
799 * \param opaque should be the stream
800 * \param stream stream created by ZipIO_Open
801 * \param offset positive offset to seek
802 * \param origin current position in stream
803 * \return ¿ VLC_SUCCESS or an error code ?
804 *****************************************************************************/
805 static long ZCALLBACK ZipIO_Seek ( void *opaque, void *stream,
806 unsigned long offset, int origin )
809 stream_t *s = (stream_t*) opaque;
812 uint64_t pos = offset + origin;
813 l_ret = (long) stream_Seek( s->p_source, pos );
817 /** **************************************************************************
818 * \brief close the stream
819 * \param opaque should be the stream
820 * \param stream stream created by ZipIO_Open
821 * \return always VLC_SUCCESS
822 * This closes zip archive
823 *****************************************************************************/
824 static int ZCALLBACK ZipIO_Close ( void *opaque, void *stream )
828 // stream_t *s = (stream_t*) opaque;
829 // if( p_demux->p_sys && p_demux->p_sys->zipFile )
830 // p_demux->p_sys->zipFile = NULL;
831 // stream_Seek( s->p_source, 0 );
835 /** **************************************************************************
836 * \brief I/O functions for the ioapi: write (assert insteadof segfault)
837 *****************************************************************************/
838 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
839 const void* buf, uLong size )
841 (void)opaque; (void)stream; (void)buf; (void)size;
842 int ERROR_zip_cannot_write_this_should_not_happen = 0;
843 assert( ERROR_zip_cannot_write_this_should_not_happen );
847 /** **************************************************************************
848 * \brief I/O functions for the ioapi: test error (man 3 ferror)
849 *****************************************************************************/
850 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )