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 )
53 /*****************************************************************************
55 *****************************************************************************/
56 static const uint8_t p_rar_marker[] = {
57 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00
59 static const int i_rar_marker = sizeof(p_rar_marker);
65 uint64_t i_cummulated_size;
74 rar_file_chunk_t **pp_chunk;
75 uint64_t i_real_size; /* Gathered size */
78 static void RarFileDelete( rar_file_t * );
83 const rar_file_chunk_t *p_chunk;
87 uint8_t *p_peek_alloc;
93 /****************************************************************************
95 ****************************************************************************/
96 static int Read ( stream_t *, void *p_read, unsigned int i_read );
97 static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
98 static int Control( stream_t *, int i_query, va_list );
100 static int Parse ( stream_t * );
101 static int Seek ( stream_t *s, uint64_t i_position );
103 /****************************************************************************
105 ****************************************************************************/
106 static int Open ( vlc_object_t *p_this )
108 stream_t *s = (stream_t*)p_this;
112 const uint8_t *p_peek;
113 if( stream_Peek( s->p_source, &p_peek, i_rar_marker ) < i_rar_marker )
115 if( memcmp( p_peek, p_rar_marker, i_rar_marker ) )
121 s->pf_control = Control;
123 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
128 p_sys->p_file = NULL;
129 p_sys->i_position = 0;
130 p_sys->p_chunk = NULL;
132 p_sys->p_peek_alloc = NULL;
133 p_sys->p_peek = NULL;
137 if( Parse( s ) || !p_sys->p_file || p_sys->p_file->i_chunk <= 0 )
139 msg_Err( s, "Invalid or unsupported RAR archive" );
141 RarFileDelete( p_sys->p_file );
150 const rar_file_t *p_file = p_sys->p_file;
151 msg_Dbg( s, "Using RAR stream filter for '%s' %"PRId64"(expected %"PRId64") bytes in %d chunks",
152 p_file->psz_name, p_file->i_real_size, p_file->i_size, p_file->i_chunk );
157 /****************************************************************************
159 ****************************************************************************/
160 static void Close( vlc_object_t *p_this )
162 stream_t *s = (stream_t*)p_this;
163 stream_sys_t *p_sys = s->p_sys;
165 RarFileDelete( p_sys->p_file );
166 free( p_sys->p_peek_alloc );
170 /****************************************************************************
171 * Stream filters functions
172 ****************************************************************************/
173 static int Read( stream_t *s, void *p_read, unsigned int i_read )
175 stream_sys_t *p_sys = s->p_sys;
176 uint8_t *p_data = p_read;
177 unsigned int i_total = 0;
179 if( p_sys->i_peek > 0 && i_read > 0 )
181 const unsigned int i_copy = __MIN( i_read, p_sys->i_peek );
185 memcpy( p_data, p_sys->p_peek, i_copy );
189 p_sys->i_peek -= i_copy;
190 p_sys->p_peek += i_copy;
194 while( i_total < i_read )
196 const uint64_t i_chunk_end = p_sys->p_chunk->i_cummulated_size + p_sys->p_chunk->i_size;
198 int i_max = __MIN( i_read - i_total, i_chunk_end - p_sys->i_position );
202 int i_real = stream_Read( s->p_source, p_data, i_max );
209 p_sys->i_position += i_real;
210 if( p_sys->i_position >= i_chunk_end )
212 if( Seek( s, p_sys->i_position ) )
219 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
221 stream_sys_t *p_sys = s->p_sys;
223 if( i_peek <= p_sys->i_peek )
225 *pp_peek = p_sys->p_peek;
230 uint8_t *p_peek = malloc( i_peek );
234 /* XXX yes stream_Read on ourself */
235 int i_read = stream_Read( s, p_peek, i_peek );
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 uint64_t i_position = va_arg( args, uint64_t );
262 return Seek( s, i_position );
265 case STREAM_GET_POSITION:
267 uint64_t *pi_position = va_arg( args, uint64_t* );
268 *pi_position = p_sys->i_position - p_sys->i_peek;
272 case STREAM_GET_SIZE:
274 uint64_t *pi_size = (uint64_t*)va_arg( args, uint64_t* );
275 *pi_size = p_sys->p_file->i_real_size;
280 case STREAM_GET_CONTENT_TYPE: /* arg1= char ** */
283 case STREAM_UPDATE_SIZE: /* TODO maybe we should update i_real_size from file size and chunk offset ? */
284 case STREAM_CONTROL_ACCESS:
285 case STREAM_CAN_SEEK:
286 case STREAM_CAN_FASTSEEK:
287 case STREAM_SET_RECORD_STATE:
288 return stream_vaControl( s->p_source, i_query, args );
294 /****************************************************************************
296 ****************************************************************************/
297 static int Seek( stream_t *s, uint64_t i_position )
299 stream_sys_t *p_sys = s->p_sys;
301 if( i_position > p_sys->p_file->i_real_size )
302 i_position = p_sys->p_file->i_real_size;
304 /* Search the chunk */
305 const rar_file_t *p_file = p_sys->p_file;
306 for( int i = 0; i < p_file->i_chunk; i++ )
308 p_sys->p_chunk = p_file->pp_chunk[i];
309 if( i_position < p_sys->p_chunk->i_cummulated_size + p_sys->p_chunk->i_size )
312 p_sys->i_position = i_position;
315 const uint64_t i_seek = p_sys->p_chunk->i_offset +
316 ( i_position - p_sys->p_chunk->i_cummulated_size );
317 return stream_Seek( s->p_source, i_seek );
320 static void RarFileDelete( rar_file_t *p_file )
322 for( int i = 0; i < p_file->i_chunk; i++ )
323 free( p_file->pp_chunk[i] );
324 free( p_file->pp_chunk );
325 free( p_file->psz_name );
340 RAR_BLOCK_MARKER = 0x72,
341 RAR_BLOCK_ARCHIVE = 0x73,
342 RAR_BLOCK_FILE = 0x74,
343 RAR_BLOCK_END = 0x7b,
347 RAR_BLOCK_END_HAS_NEXT = 0x0001,
351 RAR_BLOCK_FILE_HAS_PREVIOUS = 0x0001,
352 RAR_BLOCK_FILE_HAS_NEXT = 0x0002,
353 RAR_BLOCK_FILE_HAS_HIGH = 0x0100,
356 static int PeekBlock( stream_t *s, rar_block_t *p_hdr )
358 const uint8_t *p_peek;
359 int i_peek = stream_Peek( s->p_source, &p_peek, 11 );
364 p_hdr->i_crc = GetWLE( &p_peek[0] );
365 p_hdr->i_type = p_peek[2];
366 p_hdr->i_flags = GetWLE( &p_peek[3] );
367 p_hdr->i_size = GetWLE( &p_peek[5] );
368 p_hdr->i_add_size = 0;
369 if( p_hdr->i_flags & 0x8000 )
373 p_hdr->i_add_size = GetDWLE( &p_peek[7] );
376 if( p_hdr->i_size < 7 )
380 static int SkipBlock( stream_t *s, const rar_block_t *p_hdr )
382 uint64_t i_size = (uint64_t)p_hdr->i_size + p_hdr->i_add_size;
386 int i_skip = __MIN( i_size, INT_MAX );
387 if( stream_Read( s->p_source, NULL, i_skip ) < i_skip )
395 static int IgnoreBlock( stream_t *s, int i_block )
399 if( PeekBlock( s, &bk ) || bk.i_type != i_block )
401 return SkipBlock( s, &bk );
404 static int SkipEnd( stream_t *s, const rar_block_t *p_hdr )
406 if( !(p_hdr->i_flags & RAR_BLOCK_END_HAS_NEXT) )
409 if( SkipBlock( s, p_hdr ) )
412 /* Now, we need to look for a marker block,
413 * It seems that there is garbage at EOF */
416 const uint8_t *p_peek;
418 if( stream_Peek( s->p_source, &p_peek, i_rar_marker ) < i_rar_marker )
421 if( !memcmp( p_peek, p_rar_marker, i_rar_marker ) )
424 if( stream_Read( s->p_source, NULL, 1 ) != 1 )
428 /* Skip marker and archive blocks */
429 if( IgnoreBlock( s, RAR_BLOCK_MARKER ) )
431 if( IgnoreBlock( s, RAR_BLOCK_ARCHIVE ) )
437 static int SkipFile( stream_t *s,const rar_block_t *p_hdr )
439 stream_sys_t *p_sys = s->p_sys;
440 const uint8_t *p_peek;
442 int i_min_size = 7+21;
443 if( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH )
445 if( p_hdr->i_size < (unsigned)i_min_size )
448 if( stream_Peek( s->p_source, &p_peek, i_min_size ) < i_min_size )
452 uint32_t i_file_size_low = GetDWLE( &p_peek[7+4] );
453 uint8_t i_method = p_peek[7+18];
454 uint16_t i_name_size = GetWLE( &p_peek[7+19] );
455 uint32_t i_file_size_high = 0;
456 if( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH )
457 i_file_size_high = GetDWLE( &p_peek[7+25] );
459 char *psz_name = calloc( 1, i_name_size + 1 );
463 const int i_name_offset = (p_hdr->i_flags & RAR_BLOCK_FILE_HAS_HIGH) ? (7+33) : (7+25);
464 if( i_name_offset + i_name_size <= p_hdr->i_size )
466 const int i_max_size = i_name_offset + i_name_size;
467 if( stream_Peek( s->p_source, &p_peek, i_max_size ) < i_max_size )
472 memcpy( psz_name, &p_peek[i_name_offset], i_name_size );
475 if( i_method != 0x30 )
477 msg_Warn( s, "Ignoring compressed file %s (method=0x%2.2x)", psz_name, i_method );
481 /* Ignore smaller files */
482 const uint64_t i_file_size = ((uint64_t)i_file_size_high << 32) | i_file_size_low;
484 p_sys->p_file->i_size < i_file_size )
486 RarFileDelete( p_sys->p_file );
487 p_sys->p_file = NULL;
490 rar_file_t *p_current = p_sys->p_file;
493 p_sys->p_file = p_current = malloc( sizeof( *p_sys->p_file ) );
498 p_current->psz_name = psz_name;
499 p_current->i_size = i_file_size;
500 p_current->b_complete = false;
501 p_current->i_real_size = 0;
502 TAB_INIT( p_current->i_chunk, p_current->pp_chunk );
508 if( !p_current->b_complete )
510 bool b_append = false;
511 /* Append if first chunk */
512 if( p_current->i_chunk <= 0 )
514 /* Append if it is really a continuous chunck */
515 if( p_current->i_size == i_file_size &&
516 ( !psz_name || !strcmp( p_current->psz_name, psz_name ) ) &&
517 ( p_hdr->i_flags & RAR_BLOCK_FILE_HAS_PREVIOUS ) )
522 rar_file_chunk_t *p_chunk = malloc( sizeof( *p_chunk ) );
525 p_chunk->i_offset = stream_Tell( s->p_source ) + p_hdr->i_size;
526 p_chunk->i_size = p_hdr->i_add_size;
527 p_chunk->i_cummulated_size = 0;
528 if( p_current->i_chunk > 0 )
530 rar_file_chunk_t *p_previous = p_current->pp_chunk[p_current->i_chunk-1];
532 p_chunk->i_cummulated_size += p_previous->i_cummulated_size +
536 TAB_APPEND( p_current->i_chunk, p_current->pp_chunk, p_chunk );
538 p_current->i_real_size += p_hdr->i_add_size;
542 if( !(p_hdr->i_flags & RAR_BLOCK_FILE_HAS_NEXT ) )
543 p_current->b_complete = true;
550 /* We stop on the first non empty file if we cannot seek */
553 bool b_can_seek = false;
554 stream_Control( s->p_source, STREAM_CAN_SEEK, &b_can_seek );
555 if( !b_can_seek && p_current->i_size > 0 )
559 if( SkipBlock( s, p_hdr ) )
564 static int Parse( stream_t *s )
567 if( IgnoreBlock( s, RAR_BLOCK_MARKER ) )
571 if( IgnoreBlock( s, RAR_BLOCK_ARCHIVE ) )
580 if( PeekBlock( s, &bk ) )
586 i_ret = SkipEnd( s, &bk );
589 i_ret = SkipFile( s, &bk );
592 i_ret = SkipBlock( s, &bk );