diff options
Diffstat (limited to 'lzip_index.c')
-rw-r--r-- | lzip_index.c | 78 |
1 files changed, 32 insertions, 46 deletions
diff --git a/lzip_index.c b/lzip_index.c index ef0b0bd..91a9ec6 100644 --- a/lzip_index.c +++ b/lzip_index.c @@ -38,7 +38,7 @@ static int seek_read( const int fd, uint8_t * const buf, const int size, } -static bool add_error( struct Lzip_index * const li, const char * const msg ) +static bool add_error( Lzip_index * const li, const char * const msg ) { const int len = strlen( msg ); void * tmp = resize_buffer( li->error, li->error_size + len + 1 ); @@ -50,16 +50,15 @@ static bool add_error( struct Lzip_index * const li, const char * const msg ) } -static bool push_back_member( struct Lzip_index * const li, - const long long dp, const long long ds, - const long long mp, const long long ms, - const unsigned dict_size ) +static bool push_back_member( Lzip_index * const li, const long long dp, + const long long ds, const long long mp, + const long long ms, const unsigned dict_size ) { - struct Member * p; + Member * p; void * tmp = resize_buffer( li->member_vector, ( li->members + 1 ) * sizeof li->member_vector[0] ); if( !tmp ) { add_error( li, mem_msg ); li->retval = 1; return false; } - li->member_vector = (struct Member *)tmp; + li->member_vector = (Member *)tmp; p = &(li->member_vector[li->members]); init_member( p, dp, ds, mp, ms, dict_size ); ++li->members; @@ -67,7 +66,7 @@ static bool push_back_member( struct Lzip_index * const li, } -static void Li_free_member_vector( struct Lzip_index * const li ) +static void Li_free_member_vector( Lzip_index * const li ) { if( li->member_vector ) { free( li->member_vector ); li->member_vector = 0; } @@ -75,9 +74,9 @@ static void Li_free_member_vector( struct Lzip_index * const li ) } -static void Li_reverse_member_vector( struct Lzip_index * const li ) +static void Li_reverse_member_vector( Lzip_index * const li ) { - struct Member tmp; + Member tmp; long i; for( i = 0; i < li->members / 2; ++i ) { @@ -88,8 +87,7 @@ static void Li_reverse_member_vector( struct Lzip_index * const li ) } -static bool Li_check_header( struct Lzip_index * const li, - const Lzip_header header ) +static bool Li_check_header( Lzip_index * const li, const Lzip_header header ) { if( !Lh_check_magic( header ) ) { add_error( li, bad_magic_msg ); li->retval = 2; return false; } @@ -101,15 +99,14 @@ static bool Li_check_header( struct Lzip_index * const li, return true; } -static void Li_set_errno_error( struct Lzip_index * const li, - const char * const msg ) +static void Li_set_errno_error( Lzip_index * const li, const char * const msg ) { add_error( li, msg ); add_error( li, strerror( errno ) ); li->retval = 1; } -static void Li_set_num_error( struct Lzip_index * const li, - const char * const msg, unsigned long long num ) +static void Li_set_num_error( Lzip_index * const li, const char * const msg, + unsigned long long num ) { char buf[80]; snprintf( buf, sizeof buf, "%s%llu", msg, num ); @@ -118,22 +115,19 @@ static void Li_set_num_error( struct Lzip_index * const li, } -static bool Li_read_header( struct Lzip_index * const li, const int fd, - Lzip_header header, const long long pos, const bool ignore_marking ) +static bool Li_read_header( Lzip_index * const li, const int fd, + Lzip_header header, const long long pos ) { if( seek_read( fd, header, Lh_size, pos ) != Lh_size ) { Li_set_errno_error( li, "Error reading member header: " ); return false; } - uint8_t byte; - if( !ignore_marking && readblock( fd, &byte, 1 ) == 1 && byte != 0 ) - { add_error( li, marking_msg ); li->retval = 2; return false; } return true; } /* If successful, push last member and set pos to member header. */ -static bool Li_skip_trailing_data( struct Lzip_index * const li, const int fd, +static bool Li_skip_trailing_data( Lzip_index * const li, const int fd, unsigned long long * const pos, - const struct Cl_options * const cl_opts ) + const Cl_options * const cl_opts ) { if( *pos < min_member_size ) return false; enum { block_size = 16384, @@ -162,8 +156,8 @@ static bool Li_skip_trailing_data( struct Lzip_index * const li, const int fd, if( member_size > ipos + i || !Lt_check_consistency( *trailer ) ) continue; Lzip_header header; - if( !Li_read_header( li, fd, header, ipos + i - member_size, - cl_opts->ignore_marking ) ) return false; + if( !Li_read_header( li, fd, header, ipos + i - member_size ) ) + return false; if( !Lh_check( header ) ) continue; const Lzip_header * header2 = (const Lzip_header *)( buffer + i ); const bool full_h2 = bsize - i >= Lh_size; @@ -178,15 +172,12 @@ static bool Li_skip_trailing_data( struct Lzip_index * const li, const int fd, { add_error( li, corrupt_mm_msg ); li->retval = 2; return false; } if( !cl_opts->ignore_trailing ) { add_error( li, trailing_msg ); li->retval = 2; return false; } - const unsigned long long data_size = Lt_get_data_size( *trailer ); - if( !cl_opts->ignore_empty && data_size == 0 ) - { add_error( li, empty_msg ); li->retval = 2; return false; } *pos = ipos + i - member_size; /* good member */ const unsigned dictionary_size = Lh_get_dictionary_size( header ); if( li->dictionary_size < dictionary_size ) li->dictionary_size = dictionary_size; - return push_back_member( li, 0, data_size, *pos, member_size, - dictionary_size ); + return push_back_member( li, 0, Lt_get_data_size( *trailer ), *pos, + member_size, dictionary_size ); } if( ipos == 0 ) { Li_set_num_error( li, "Bad trailer at pos ", *pos - Lt_size ); @@ -200,8 +191,8 @@ static bool Li_skip_trailing_data( struct Lzip_index * const li, const int fd, } -bool Li_init( struct Lzip_index * const li, const int infd, - const struct Cl_options * const cl_opts ) +bool Li_init( Lzip_index * const li, const int infd, + const Cl_options * const cl_opts ) { li->member_vector = 0; li->error = 0; @@ -212,6 +203,10 @@ bool Li_init( struct Lzip_index * const li, const int infd, li->dictionary_size = 0; if( li->insize < 0 ) { Li_set_errno_error( li, "Input file is not seekable: " ); return false; } + Lzip_header header; + if( li->insize >= Lh_size && + ( !Li_read_header( li, infd, header, 0 ) || + !Li_check_header( li, header ) ) ) return false; if( li->insize < min_member_size ) { add_error( li, "Input file is too short." ); li->retval = 2; return false; } @@ -219,10 +214,6 @@ bool Li_init( struct Lzip_index * const li, const int infd, { add_error( li, "Input file is too long (2^63 bytes or more)." ); li->retval = 2; return false; } - Lzip_header header; - if( !Li_read_header( li, infd, header, 0, cl_opts->ignore_marking ) || - !Li_check_header( li, header ) ) return false; - unsigned long long pos = li->insize; /* always points to a header or to EOF */ while( pos >= min_member_size ) { @@ -237,8 +228,7 @@ bool Li_init( struct Lzip_index * const li, const int infd, return false; } Li_set_num_error( li, "Bad trailer at pos ", pos - Lt_size ); break; } - if( !Li_read_header( li, infd, header, pos - member_size, - cl_opts->ignore_marking ) ) break; + if( !Li_read_header( li, infd, header, pos - member_size ) ) break; if( !Lh_check( header ) ) /* bad header */ { if( li->members <= 0 ) @@ -246,15 +236,12 @@ bool Li_init( struct Lzip_index * const li, const int infd, return false; } Li_set_num_error( li, "Bad header at pos ", pos - member_size ); break; } - const unsigned long long data_size = Lt_get_data_size( trailer ); - if( !cl_opts->ignore_empty && data_size == 0 ) - { add_error( li, empty_msg ); li->retval = 2; break; } pos -= member_size; /* good member */ const unsigned dictionary_size = Lh_get_dictionary_size( header ); if( li->dictionary_size < dictionary_size ) li->dictionary_size = dictionary_size; - if( !push_back_member( li, 0, data_size, pos, member_size, - dictionary_size ) ) return false; + if( !push_back_member( li, 0, Lt_get_data_size( trailer ), pos, + member_size, dictionary_size ) ) return false; } if( pos != 0 || li->members <= 0 || li->retval != 0 ) { @@ -264,8 +251,7 @@ bool Li_init( struct Lzip_index * const li, const int infd, return false; } Li_reverse_member_vector( li ); - long i; - for( i = 0; ; ++i ) + long i; for( i = 0; ; ++i ) { const long long end = block_end( li->member_vector[i].dblock ); if( end < 0 || end > INT64_MAX ) @@ -281,7 +267,7 @@ bool Li_init( struct Lzip_index * const li, const int infd, } -void Li_free( struct Lzip_index * const li ) +void Li_free( Lzip_index * const li ) { Li_free_member_vector( li ); if( li->error ) { free( li->error ); li->error = 0; } |