1 /*****************************************************************************
2 * zipstream.c: stream_filter that creates a XSPF playlist from a Zip archive
3 *****************************************************************************
4 * Copyright (C) 2007 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( _( "Zip files filter" ) )
49 set_capability( "stream_filter", 1 )
50 set_callbacks( StreamOpen, StreamClose )
52 set_subcategory( SUBCAT_INPUT_ACCESS )
53 set_description( _( "Zip access" ) )
54 set_capability( "access", 70 )
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( sizeof( *p_sys ), 1 );
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" );
209 free( p_sys->fileFunctions );
213 /* Find the stream uri */
214 /* FIXME FIXME FIXME */
215 input_thread_t *p_input_thread = (input_thread_t*)
216 vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_PARENT );
217 if( !p_input_thread )
220 free( p_sys->fileFunctions );
223 input_item_t *p_input_item = input_GetItem( p_input_thread );
227 free( p_sys->fileFunctions );
230 s->p_sys->psz_path = strdup( p_input_item->psz_uri );
231 vlc_gc_decref( p_input_item );
232 // vlc_object_release( p_input_thread );
233 /* FIXME FIXME FIXME */
238 /** *************************************************************************
240 ****************************************************************************/
241 void StreamClose( vlc_object_t *p_this )
243 stream_t *s = (stream_t*)p_this;
244 stream_sys_t *p_sys = s->p_sys;
246 free( p_sys->fileFunctions );
247 free( p_sys->psz_xspf );
248 free( p_sys->psz_path );
252 /** *************************************************************************
253 * Stream filters functions
254 ****************************************************************************/
256 /** *************************************************************************
258 ****************************************************************************/
259 static int Read( stream_t *s, void *p_read, unsigned int i_read )
261 stream_sys_t *p_sys = s->p_sys;
263 if( !p_read ) return 0;
265 /* Fill the buffer */
266 if( p_sys->psz_xspf == NULL )
268 int i_ret = CreatePlaylist( s, &p_sys->psz_xspf );
271 p_sys->i_len = strlen( p_sys->psz_xspf );
275 /* Read the buffer */
276 int i_len = __MIN( i_read, p_sys->i_len - p_sys->i_pos );
277 memcpy( p_read, p_sys->psz_xspf + p_sys->i_pos, i_len );
278 p_sys->i_pos += i_len;
283 /** *************************************************************************
285 ****************************************************************************/
286 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
288 stream_sys_t *p_sys = s->p_sys;
290 /* Fill the buffer */
291 if( p_sys->psz_xspf == NULL )
293 int i_ret = CreatePlaylist( s, &p_sys->psz_xspf );
296 p_sys->i_len = strlen( p_sys->psz_xspf );
301 /* Point to the buffer */
302 int i_len = __MIN( i_peek, p_sys->i_len - p_sys->i_pos );
303 *pp_peek = (uint8_t*) p_sys->psz_xspf + p_sys->i_pos;
308 /** *************************************************************************
310 ****************************************************************************/
311 static int Control( stream_t *s, int i_query, va_list args )
313 stream_sys_t *p_sys = s->p_sys;
317 case STREAM_SET_POSITION:
319 int64_t i_position = (int64_t)va_arg( args, int64_t );
320 if( i_position >= p_sys->i_len )
324 p_sys->i_len = (size_t) i_position;
329 case STREAM_GET_POSITION:
331 int64_t *pi_position = (int64_t*)va_arg( args, int64_t* );
332 *pi_position = p_sys->i_pos;
336 case STREAM_GET_SIZE:
338 int64_t *pi_size = (int64_t*)va_arg( args, int64_t* );
339 *pi_size = (int64_t) p_sys->i_len;
345 int *pi_mtu = (int*)va_arg( args, int* );
350 case STREAM_GET_CONTENT_TYPE:
353 case STREAM_UPDATE_SIZE:
354 case STREAM_CONTROL_ACCESS:
355 case STREAM_CAN_SEEK:
356 case STREAM_CAN_FASTSEEK:
357 case STREAM_SET_RECORD_STATE:
358 return stream_vaControl( s->p_source, i_query, args );
365 static int CreatePlaylist( stream_t *s, char **pp_buffer )
367 /* Get some infos about zip archive */
369 unzFile file = s->p_sys->zipFile;
370 vlc_array_t *p_filenames = vlc_array_new(); /* Will contain char* */
372 /* List all file names in Zip archive */
373 i_ret = GetFilesInZip( s, file, p_filenames, NULL );
381 // msg_Dbg( s, "%d files in Zip", vlc_array_count( p_filenames ) );
385 s->p_sys->zipFile = NULL;
387 /* Construct the xspf playlist */
388 i_ret = WriteXSPF( pp_buffer, p_filenames, s->p_sys->psz_path );
395 for( int i = 0; i < vlc_array_count( p_filenames ); i++ )
397 free( vlc_array_item_at_index( p_filenames, i ) );
399 vlc_array_destroy( p_filenames );
404 /** **************************************************************************
405 * Zip utility functions
406 *****************************************************************************/
408 /** **************************************************************************
409 * \brief List files in zip and append their names to p_array
411 * \param file Opened zip file
412 * \param p_array vlc_array_t which will receive all filenames
414 * In case of error, returns VLC_EGENERIC.
415 * In case of success, returns number of files found, and goes back to first file.
416 *****************************************************************************/
417 static int GetFilesInZip( stream_t *p_this, unzFile file,
418 vlc_array_t *p_filenames, vlc_array_t *p_fileinfos )
420 if( !p_filenames || !p_this )
425 /* Get global info */
426 unz_global_info info;
428 if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
430 msg_Warn( p_this, "this is not a valid zip archive" );
434 /* Go to first file in archive */
435 unzGoToFirstFile( file );
437 /* Get info about each file */
438 for( unsigned long i = 0; i < info.number_entry; i++ )
440 char *psz_fileName = calloc( ZIP_FILENAME_LEN, 1 );
441 unz_file_info *p_fileInfo = calloc( 1, sizeof( unz_file_info ) );
443 if( !p_fileInfo || !psz_fileName )
445 msg_Warn( p_this, "not enough memory" );
449 if( unzGetCurrentFileInfo( file, p_fileInfo, psz_fileName,
450 ZIP_FILENAME_LEN, NULL, 0, NULL, 0 )
453 msg_Warn( p_this, "can't get info about file in zip" );
458 vlc_array_append( p_filenames, strdup( psz_fileName ) );
459 free( psz_fileName );
462 vlc_array_append( p_fileinfos, p_fileInfo );
466 if( i < ( info.number_entry - 1 ) )
468 /* Go the next file in the archive */
469 if( unzGoToNextFile( file ) != UNZ_OK )
471 msg_Warn( p_this, "can't go to next file in zip" );
479 /* i_ret should be equal to info.number_entry */
480 unzGoToFirstFile( file );
485 /** **************************************************************************
486 * XSPF generation functions
487 *****************************************************************************/
489 /** **************************************************************************
490 * \brief Write the XSPF playlist given the list of files
491 *****************************************************************************/
492 static int WriteXSPF( char **pp_buffer, vlc_array_t *p_filenames,
493 const char *psz_zippath )
495 char *psz_zip = strrchr( psz_zippath, DIR_SEP_CHAR );
496 psz_zip = convert_xml_special_chars( psz_zip ? (psz_zip+1) : psz_zippath );
498 if( asprintf( pp_buffer, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
499 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" "
500 "xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
501 " <title>%s</title>\n"
502 " <trackList>\n", psz_zip ) == -1)
506 node *playlist = new_node( psz_zip );
508 /* Web-Encode the URI and append '!' */
509 char *psz_pathtozip = vlc_UrlEncode( psz_zippath );
510 if( astrcatf( &psz_pathtozip, ZIP_SEP ) < 0 ) return -1;
513 for( int i = 0; i < vlc_array_count( p_filenames ); ++i )
515 char *psz_name = (char*) vlc_array_item_at_index( p_filenames, i );
516 int i_len = strlen( psz_name );
518 if( !i_len ) continue;
520 /* Is it a folder ? */
521 if( psz_name[i_len-1] == '/' )
527 /* Extract file name */
528 char *psz_file = strrchr( psz_name, '/' );
529 psz_file = convert_xml_special_chars( psz_file ?
530 (psz_file+1) : psz_name );
533 char *psz_path = strdup( psz_pathtozip );
534 if( astrcatf( &psz_path, psz_name ) < 0 ) return -1;
536 /* Double url-encode */
537 char *psz_tmp = psz_path;
538 psz_path = vlc_UrlEncode( psz_tmp );
541 /* Track information */
542 if( astrcatf( pp_buffer,
544 " <location>zip://%s</location>\n"
545 " <title>%s</title>\n"
546 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n"
547 " <vlc:id>%d</vlc:id>\n"
550 psz_path, psz_file, i_track ) < 0 ) return -1;
555 /* Find the parent node */
556 node *parent = findOrCreateParentNode( playlist, psz_name );
559 /* Add the item to this node */
560 item *tmp = parent->media;
563 parent->media = new_item( i_track );
571 tmp->next = new_item( i_track );
578 free( psz_pathtozip );
580 /* Close tracklist, open the extension */
581 if( astrcatf( pp_buffer,
583 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n"
587 if( nodeToXSPF( pp_buffer, playlist, true ) < 0 ) return -1;
589 /* Close extension and playlist */
590 if( astrcatf( pp_buffer, " </extension>\n</playlist>\n" ) < 0 ) return -1;
592 /* printf( "%s", *pp_buffer ); */
594 free_all_node( playlist );
599 /** **************************************************************************
600 * \brief Recursively convert a node to its XSPF representation
601 *****************************************************************************/
602 static int nodeToXSPF( char **pp_buffer, node *n, bool b_root )
606 if( astrcatf( pp_buffer, " <vlc:node title=\"%s\">\n", n->name ) < 0 )
610 nodeToXSPF( pp_buffer, n->child, false );
614 if( astrcatf( pp_buffer, " <vlc:item tid=\"%d\" />\n", i->id ) < 0 )
620 if( astrcatf( pp_buffer, " </vlc:node>\n" ) < 0 )
626 /** **************************************************************************
627 * \brief Either create or find the parent node of the item
628 *****************************************************************************/
629 static node* findOrCreateParentNode( node *root, const char *fullpath )
632 char *path = strdup( fullpath );
637 char *sep = strchr( folder, '/' );
647 node *current = root->child;
651 if( !strcmp( current->name, folder ) )
653 /* We found the folder, go recursively deeper */
654 return findOrCreateParentNode( current, sep );
656 current = current->next;
659 /* If we are here, then it means that we did not find the parent */
660 node *ret = new_node( folder );
665 current = root->child;
666 while( current->next )
668 current = current->next;
673 /* And now, create the subfolders */
674 ret = findOrCreateParentNode( ret, sep );
681 /** **************************************************************************
682 * ZipIO function definitions : how to use vlc_stream to read the zip
683 *****************************************************************************/
685 /** **************************************************************************
686 * \brief interface for unzip module to open a file using a vlc_stream
689 * \param mode how to open the file (read/write ?). We support only read
691 *****************************************************************************/
692 static void ZCALLBACK *ZipIO_Open( void *opaque, const char *file, int mode )
695 stream_t *s = (stream_t*) opaque;
696 if( mode & ( ZLIB_FILEFUNC_MODE_CREATE | ZLIB_FILEFUNC_MODE_WRITE ) )
698 msg_Dbg( s, "ZipIO_Open: we cannot write into zip files" );
704 /** **************************************************************************
705 * \brief read something from stream into buffer
706 * \param opaque should be the stream
707 * \param stream stream created by ZipIO_Open
708 * \param buf buffer to read the file
709 * \param size length of this buffer
710 * \return return the number of bytes read (<= size)
711 *****************************************************************************/
712 static unsigned long ZCALLBACK ZipIO_Read( void *opaque, void *stream,
713 void *buf, unsigned long size )
716 stream_t *s = (stream_t*) opaque;
717 return (unsigned long) stream_Read( s->p_source, buf, (int) size );
720 /** **************************************************************************
721 * \brief tell size of stream
722 * \param opaque should be the stream
723 * \param stream stream created by ZipIO_Open
724 * \return size of the file / stream
725 * ATTENTION: this is not stream_Tell, but stream_Size !
726 *****************************************************************************/
727 static long ZCALLBACK ZipIO_Tell( void *opaque, void *stream )
730 stream_t *s = (stream_t*) opaque;
731 return (long) stream_Size( s->p_source ); /* /!\ not stream_Tell /!\ */
734 /** **************************************************************************
735 * \brief seek in the stream
736 * \param opaque should be the stream
737 * \param stream stream created by ZipIO_Open
738 * \param offset positive offset to seek
739 * \param origin current position in stream
740 * \return ¿ VLC_SUCCESS or an error code ?
741 *****************************************************************************/
742 static long ZCALLBACK ZipIO_Seek ( void *opaque, void *stream,
743 unsigned long offset, int origin )
746 stream_t *s = (stream_t*) opaque;
749 uint64_t pos = offset + origin;
750 l_ret = (long) stream_Seek( s->p_source, pos );
754 /** **************************************************************************
755 * \brief close the stream
756 * \param opaque should be the stream
757 * \param stream stream created by ZipIO_Open
758 * \return always VLC_SUCCESS
759 * This closes zip archive
760 *****************************************************************************/
761 static int ZCALLBACK ZipIO_Close ( void *opaque, void *stream )
765 // stream_t *s = (stream_t*) opaque;
766 // if( p_demux->p_sys && p_demux->p_sys->zipFile )
767 // p_demux->p_sys->zipFile = NULL;
768 // stream_Seek( s->p_source, 0 );
772 /** **************************************************************************
773 * \brief I/O functions for the ioapi: write (assert insteadof segfault)
774 *****************************************************************************/
775 static uLong ZCALLBACK ZipIO_Write( void* opaque, void* stream,
776 const void* buf, uLong size )
778 (void)opaque; (void)stream; (void)buf; (void)size;
779 int ERROR_zip_cannot_write_this_should_not_happen = 0;
780 assert( ERROR_zip_cannot_write_this_should_not_happen );
784 /** **************************************************************************
785 * \brief I/O functions for the ioapi: test error (man 3 ferror)
786 *****************************************************************************/
787 static int ZCALLBACK ZipIO_Error( void* opaque, void* stream )
791 //msg_Dbg( p_access, "error" );