1 /*****************************************************************************
2 * rar.c: uncompressed RAR stream filter (only the biggest file is extracted)
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ 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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_stream.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
45 set_category( CAT_INPUT )
46 set_subcategory( SUBCAT_INPUT_STREAM_FILTER )
47 set_description( N_("Uncompressed RAR") )
48 set_capability( "stream_filter", 1 )
49 set_callbacks( Open, Close )
52 /*****************************************************************************
54 *****************************************************************************/
55 static const uint8_t p_rar_marker[] = {
56 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00
58 static const int i_rar_marker = sizeof(p_rar_marker);
64 int64_t i_cummulated_size;
73 rar_file_chunk_t **pp_chunk;
74 int64_t i_real_size; /* Gathered size */
77 static void RarFileDelete( rar_file_t * );
82 const rar_file_chunk_t *p_chunk;
86 uint8_t *p_peek_alloc;
92 /****************************************************************************
94 ****************************************************************************/
95 static int Read ( stream_t *, void *p_read, unsigned int i_read );
96 static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
97 static int Control( stream_t *, int i_query, va_list );
99 static int Parse ( stream_t * );
100 static int Seek ( stream_t *s, int64_t i_position );
102 /****************************************************************************
104 ****************************************************************************/
105 static int Open ( vlc_object_t *p_this )
107 stream_t *s = (stream_t*)p_this;
111 const uint8_t *p_peek;
112 if( stream_Peek( s->p_source, &p_peek, i_rar_marker ) < i_rar_marker )
114 if( memcmp( p_peek, p_rar_marker, i_rar_marker ) )
120 s->pf_control = Control;
122 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
127 p_sys->p_file = NULL;
128 p_sys->i_position = 0;
129 p_sys->p_chunk = NULL;
131 p_sys->p_peek_alloc = NULL;
132 p_sys->p_peek = NULL;
136 if( Parse( s ) || !p_sys->p_file || p_sys->p_file->i_chunk <= 0 )
138 msg_Err( s, "Invalid or unsupported RAR archive" );
140 RarFileDelete( p_sys->p_file );
149 const rar_file_t *p_file = p_sys->p_file;
150 msg_Dbg( s, "Using RAR stream filter for '%s' %lld(expected %lld) bytes in %d chunks",
151 p_file->psz_name, p_file->i_real_size, p_file->i_size, p_file->i_chunk );
156 /****************************************************************************
158 ****************************************************************************/
159 static void Close( vlc_object_t *p_this )
161 stream_t *s = (stream_t*)p_this;
162 stream_sys_t *p_sys = s->p_sys;
164 RarFileDelete( p_sys->p_file );
165 free( p_sys->p_peek_alloc );
169 /****************************************************************************
170 * Stream filters functions
171 ****************************************************************************/
172 static int Read( stream_t *s, void *p_read, unsigned int i_read )
174 stream_sys_t *p_sys = s->p_sys;
175 uint8_t *p_data = p_read;
176 unsigned int i_total = 0;
178 if( p_sys->i_peek > 0 && i_read > 0 )
180 const unsigned int i_copy = __MIN( i_read, p_sys->i_peek );
184 memcpy( p_data, p_sys->p_peek, i_copy );
188 p_sys->i_peek -= i_copy;
189 p_sys->p_peek += i_copy;
193 while( i_total < i_read )
195 const int64_t i_chunk_end = p_sys->p_chunk->i_cummulated_size + p_sys->p_chunk->i_size;
197 int i_max = __MIN( i_read - i_total, i_chunk_end - p_sys->i_position );
201 int i_real = stream_Read( s->p_source, p_data, i_max );
208 p_sys->i_position += i_real;
209 if( p_sys->i_position >= i_chunk_end )
211 if( Seek( s, p_sys->i_position ) )
218 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
220 stream_sys_t *p_sys = s->p_sys;
222 if( i_peek <= p_sys->i_peek )
224 *pp_peek = p_sys->p_peek;
229 uint8_t *p_peek = malloc( i_peek );
233 /* XXX yes stream_Read on ourself */
234 int i_read = stream_Read( s, p_peek, i_peek );
241 if( p_sys->p_peek_alloc )
242 free( p_sys->p_peek_alloc );
244 p_sys->p_peek_alloc =
245 p_sys->p_peek = p_peek;
246 p_sys->i_peek = i_read;
248 *pp_peek = p_sys->p_peek;
249 return p_sys->i_peek;
252 static int Control( stream_t *s, int i_query, va_list args )
254 stream_sys_t *p_sys = s->p_sys;
259 case STREAM_SET_POSITION:
261 int64_t i_position = (int64_t)va_arg( args, int64_t );
262 return Seek( s, i_position );
265 case STREAM_GET_POSITION:
267 int64_t *pi_position = (int64_t*)va_arg( args, int64_t* );
268 *pi_position = p_sys->i_position - p_sys->i_peek;
272 case STREAM_GET_SIZE:
274 int64_t *pi_size = (int64_t*)va_arg( args, int64_t* );
275 *pi_size = p_sys->p_file->i_real_size;
281 int *pi_mtu = (int*)va_arg( args, int* );
287 case STREAM_GET_CONTENT_TYPE: /* arg1= char ** */
290 case STREAM_UPDATE_SIZE: /* TODO maybe we should update i_real_size from file size and chunk offset ? */
291 case STREAM_CONTROL_ACCESS:
292 case STREAM_CAN_SEEK:
293 case STREAM_CAN_FASTSEEK:
294 case STREAM_SET_RECORD_STATE:
295 return stream_vaControl( s->p_source, i_query, args );
301 /****************************************************************************
303 ****************************************************************************/
304 static int Seek( stream_t *s, int64_t i_position )
306 stream_sys_t *p_sys = s->p_sys;
310 else if( i_position > p_sys->p_file->i_real_size )
311 i_position = p_sys->p_file->i_real_size;
313 /* Search the chunk */
314 const rar_file_t *p_file = p_sys->p_file;
315 for( int i = 0; i < p_file->i_chunk; i++ )
317 p_sys->p_chunk = p_file->pp_chunk[i];
318 if( i_position < p_sys->p_chunk->i_cummulated_size + p_sys->p_chunk->i_size )
321 p_sys->i_position = i_position;
324 const int64_t i_seek = p_sys->p_chunk->i_offset +
325 ( i_position - p_sys->p_chunk->i_cummulated_size );
326 return stream_Seek( s->p_source, i_seek );
329 static void RarFileDelete( rar_file_t *p_file )
331 for( int i = 0; i < p_file->i_chunk; i++ )
332 free( p_file->pp_chunk[i] );
333 free( p_file->pp_chunk );
334 free( p_file->psz_name );
349 RAR_BLOCK_MARKER = 0x72,
350 RAR_BLOCK_ARCHIVE = 0x73,
351 RAR_BLOCK_FILE = 0x74,
352 RAR_BLOCK_END = 0x7b,
356 RAR_BLOCK_END_HAS_NEXT = 0x0001,
360 RAR_BLOCK_FILE_HAS_PREVIOUS = 0x0001,
361 RAR_BLOCK_FILE_HAS_NEXT = 0x0002,
362 RAR_BLOCK_FILE_HAS_HIGH = 0x0100,
365 static int PeekBlock( stream_t *s, rar_block_t *p_hdr )
367 const uint8_t *p_peek;
368 int i_peek = stream_Peek( s->p_source, &p_peek, 11 );
373 p_hdr->i_crc = GetWLE( &p_peek[0] );
374 p_hdr->i_type = p_peek[2];
375 p_hdr->i_flags = GetWLE( &p_peek[3] );
376 p_hdr->i_size = GetWLE( &p_peek[5] );
377 p_hdr->i_add_size = 0;
378 if( p_hdr->i_flags & 0x8000 )
382 p_hdr->i_add_size = GetDWLE( &p_peek[7] );
385 if( p_hdr->i_size < 7 )
389 static int SkipBlock( stream_t *s, const rar_block_t *p_hdr )
391 int64_t i_size = (int64_t)p_hdr->i_size + p_hdr->i_add_size;
393 assert( i_size >= 0 );
396 int i_skip = __MIN( i_size, INT_MAX );
397 if( stream_Read( s->p_source, NULL, i_skip ) < i_skip )
405 static int IgnoreBlock( stream_t *s, int i_block )
409 if( PeekBlock( s, &bk ) || bk.i_type != i_block )
411 return SkipBlock( s, &bk );
414 static int SkipEnd( stream_t *s, const rar_block_t *p_hdr )
416 if( !(p_hdr->i_flags & RAR_BLOCK_END_HAS_NEXT) )
419 if( SkipBlock( s, p_hdr ) )
422 /* Now, we need to look for a marker block,
423 * It seems that there is garbage at EOF */
426 const uint8_t *p_peek;
428 if( stream_Peek( s->p_source, &p_peek, i_rar_marker ) < i_rar_marker )
431 if( !memcmp( p_peek, p_rar_marker, i_rar_marker ) )
434 if( stream_Read( s->p_source, NULL, 1 ) != 1 )
438 /* Skip marker and archive blocks */
439 if( IgnoreBlock( s, RAR_BLOCK_MARKER ) )
441 if( IgnoreBlock( s, RAR_BLOCK_ARCHIVE ) )
447 static int SkipFile( stream_t *s,const rar_block_t *p_hdr )
449 stream_sys_t *p_sys = s->p_sys;
450 const uint8_t *p_peek;
452 if( stream_Peek( s->p_source, &p_peek, p_hdr->i_size ) < p_hdr->i_size )
455 int i_min_size = 7+21;
456 if( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH )
458 if( p_hdr->i_size < i_min_size )
462 uint32_t i_file_size_low = GetDWLE( &p_peek[7+4] );
463 uint8_t i_method = p_peek[7+18];
464 uint16_t i_name_size = GetWLE( &p_peek[7+19] );
465 uint32_t i_file_size_high = 0;
466 if( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH )
467 i_file_size_high = GetDWLE( &p_peek[7+25] );
469 char *psz_name = calloc( 1, i_name_size + 1 );
473 const int i_name_offset = (p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH) ? (7+33) : (7+25);
474 if( i_name_offset + i_name_size <= p_hdr->i_size )
475 memcpy( psz_name, &p_peek[i_name_offset], i_name_size );
477 if( i_method != 0x30 )
479 msg_Warn( s, "Ignoring compressed file %s (method=0x%2.2x)", psz_name, i_method );
483 /* Ignore smaller files */
484 const int64_t i_file_size = ((int64_t)i_file_size_high << 32) | i_file_size_low;
486 p_sys->p_file->i_size < i_file_size )
488 RarFileDelete( p_sys->p_file );
489 p_sys->p_file = NULL;
492 rar_file_t *p_current = p_sys->p_file;
495 p_sys->p_file = p_current = malloc( sizeof( *p_sys->p_file ) );
500 p_current->psz_name = psz_name;
501 p_current->i_size = i_file_size;
502 p_current->b_complete = false;
503 p_current->i_real_size = 0;
504 TAB_INIT( p_current->i_chunk, p_current->pp_chunk );
510 if( !p_current->b_complete )
512 bool b_append = false;
513 /* Append if first chunk */
514 if( p_current->i_chunk <= 0 )
516 /* Append if it is really a continuous chunck */
517 if( p_current->i_size == i_file_size &&
518 ( !psz_name || !strcmp( p_current->psz_name, psz_name ) ) &&
519 ( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_PREVIOUS ) )
524 rar_file_chunk_t *p_chunk = malloc( sizeof( *p_chunk ) );
527 p_chunk->i_offset = stream_Tell( s->p_source );
528 p_chunk->i_size = p_hdr->i_add_size;
529 p_chunk->i_cummulated_size = 0;
530 if( p_current->i_chunk > 0 )
532 rar_file_chunk_t *p_previous = p_current->pp_chunk[p_current->i_chunk-1];
534 p_chunk->i_cummulated_size += p_previous->i_cummulated_size +
538 TAB_APPEND( p_current->i_chunk, p_current->pp_chunk, p_chunk );
540 p_current->i_real_size += p_hdr->i_add_size;
544 if( !(p_hdr->i_flags & RAR_BLOCK_FILE_HAS_NEXT ) )
545 p_current->b_complete = true;
552 /* We stop on the first non empty file if we cannot seek */
555 bool b_can_seek = false;
556 stream_Control( s->p_source, STREAM_CAN_SEEK, &b_can_seek );
557 if( !b_can_seek && p_current->i_size > 0 )
561 if( SkipBlock( s, p_hdr ) )
566 static int Parse( stream_t *s )
569 if( IgnoreBlock( s, RAR_BLOCK_MARKER ) )
573 if( IgnoreBlock( s, RAR_BLOCK_ARCHIVE ) )
582 if( PeekBlock( s, &bk ) )
588 i_ret = SkipEnd( s, &bk );
591 i_ret = SkipFile( s, &bk );
594 i_ret = SkipBlock( s, &bk );