diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:36:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:36:16 +0000 |
commit | d588197644d2a8a26b681d0c92b0b209c796626d (patch) | |
tree | 028a7387ea6724e8f5d8c96a1c7a360402a42120 /lzip_index.c | |
parent | Adding upstream version 1.11. (diff) | |
download | lunzip-d588197644d2a8a26b681d0c92b0b209c796626d.tar.xz lunzip-d588197644d2a8a26b681d0c92b0b209c796626d.zip |
Adding upstream version 1.12.upstream/1.12
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | lzip_index.c | 113 |
1 files changed, 66 insertions, 47 deletions
diff --git a/lzip_index.c b/lzip_index.c index 44c7b17..ca4df8d 100644 --- a/lzip_index.c +++ b/lzip_index.c @@ -1,28 +1,28 @@ -/* Lunzip - Decompressor for the lzip format - Copyright (C) 2010-2019 Antonio Diaz Diaz. +/* Lunzip - Decompressor for the lzip format + Copyright (C) 2010-2021 Antonio Diaz Diaz. - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 2 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ #define _FILE_OFFSET_BITS 64 #include <errno.h> #include <stdbool.h> -#include <stdio.h> -#include <string.h> #include <stdint.h> +#include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include "lzip.h" @@ -58,8 +58,7 @@ static bool push_back_member( struct Lzip_index * const li, struct Member * p; void * tmp = resize_buffer( li->member_vector, ( li->members + 1 ) * sizeof li->member_vector[0] ); - if( !tmp ) - { add_error( li, "Not enough memory." ); li->retval = 1; return false; } + if( !tmp ) { add_error( li, mem_msg ); li->retval = 1; return false; } li->member_vector = (struct Member *)tmp; p = &(li->member_vector[li->members]); init_member( p, dp, ds, mp, ms, dict_size ); @@ -89,6 +88,19 @@ static void Li_reverse_member_vector( struct Lzip_index * const li ) } +static bool Li_check_header_error( struct Lzip_index * const li, + const Lzip_header header ) + { + if( !Lh_verify_magic( header ) ) + { add_error( li, bad_magic_msg ); li->retval = 2; return true; } + if( !Lh_verify_version( header ) ) + { add_error( li, bad_version( Lh_version( header ) ) ); li->retval = 2; + return true; } + if( !isvalid_ds( Lh_get_dictionary_size( header ) ) ) + { add_error( li, bad_dict_msg ); li->retval = 2; return true; } + return false; + } + static void Li_set_errno_error( struct Lzip_index * const li, const char * const msg ) { @@ -106,9 +118,18 @@ 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 ) + { + if( seek_read( fd, header, Lh_size, pos ) != Lh_size ) + { Li_set_errno_error( li, "Error reading member header: " ); 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, long long * const pos, +static bool Li_skip_trailing_data( struct Lzip_index * const li, const int fd, + unsigned long long * const pos, const bool ignore_trailing, const bool loose_trailing ) { @@ -135,36 +156,40 @@ static bool Li_skip_trailing_data( struct Lzip_index * const li, if( buffer[i-1] <= max_msb ) /* most significant byte of member_size */ { Lzip_header header; + const Lzip_header * header2; const Lzip_trailer * const trailer = (const Lzip_trailer *)( buffer + i - Lt_size ); const unsigned long long member_size = Lt_get_member_size( *trailer ); unsigned dictionary_size; + bool full_h2; if( member_size == 0 ) /* skip trailing zeros */ { while( i > Lt_size && buffer[i-9] == 0 ) --i; continue; } if( member_size > ipos + i || !Lt_verify_consistency( *trailer ) ) continue; - if( seek_read( fd, header, Lh_size, - ipos + i - member_size ) != Lh_size ) - { Li_set_errno_error( li, "Error reading member header: " ); - return false; } - dictionary_size = Lh_get_dictionary_size( header ); - if( !Lh_verify_magic( header ) || !Lh_verify_version( header ) || - !isvalid_ds( dictionary_size ) ) continue; - if( Lh_verify_prefix( buffer + i, bsize - i ) ) + if( !Li_read_header( li, fd, header, ipos + i - member_size ) ) + return false; + if( !Lh_verify( header ) ) continue; + header2 = (const Lzip_header *)( buffer + i ); + full_h2 = bsize - i >= Lh_size; + if( Lh_verify_prefix( *header2, bsize - i ) ) /* last member */ { - add_error( li, "Last member in input file is truncated or corrupt." ); + if( !full_h2 ) add_error( li, "Last member in input file is truncated." ); + else if( !Li_check_header_error( li, *header2 ) ) + add_error( li, "Last member in input file is truncated or corrupt." ); li->retval = 2; return false; } - if( !loose_trailing && bsize - i >= Lh_size && - Lh_verify_corrupt( buffer + i ) ) + if( !loose_trailing && full_h2 && Lh_verify_corrupt( *header2 ) ) { add_error( li, corrupt_mm_msg ); li->retval = 2; return false; } if( !ignore_trailing ) { add_error( li, trailing_msg ); li->retval = 2; return false; } *pos = ipos + i - member_size; + dictionary_size = Lh_get_dictionary_size( header ); + if( li->dictionary_size < dictionary_size ) + li->dictionary_size = dictionary_size; return push_back_member( li, 0, Lt_get_data_size( *trailer ), *pos, member_size, dictionary_size ); } - if( ipos <= 0 ) + if( ipos == 0 ) { Li_set_num_error( li, "Bad trailer at pos ", *pos - Lt_size ); return false; } bsize = buffer_size; @@ -180,7 +205,7 @@ bool Li_init( struct Lzip_index * const li, const int infd, const bool ignore_trailing, const bool loose_trailing ) { Lzip_header header; - long long pos; + unsigned long long pos; long i; li->member_vector = 0; li->error = 0; @@ -188,6 +213,7 @@ bool Li_init( struct Lzip_index * const li, const int infd, li->members = 0; li->error_size = 0; li->retval = 0; + li->dictionary_size = 0; if( li->insize < 0 ) { Li_set_errno_error( li, "Input file is not seekable: " ); return false; } if( li->insize < min_member_size ) @@ -197,15 +223,8 @@ 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; } - if( seek_read( infd, header, Lh_size, 0 ) != Lh_size ) - { Li_set_errno_error( li, "Error reading member header: " ); return false; } - if( !Lh_verify_magic( header ) ) - { add_error( li, bad_magic_msg ); li->retval = 2; return false; } - if( !Lh_verify_version( header ) ) - { add_error( li, bad_version( Lh_version( header ) ) ); li->retval = 2; - return false; } - if( !isvalid_ds( Lh_get_dictionary_size( header ) ) ) - { add_error( li, bad_dict_msg ); li->retval = 2; return false; } + if( !Li_read_header( li, infd, header, 0 ) ) return false; + if( Li_check_header_error( li, header ) ) return false; pos = li->insize; /* always points to a header or to EOF */ while( pos >= min_member_size ) @@ -216,19 +235,16 @@ bool Li_init( struct Lzip_index * const li, const int infd, if( seek_read( infd, trailer, Lt_size, pos - Lt_size ) != Lt_size ) { Li_set_errno_error( li, "Error reading member trailer: " ); break; } member_size = Lt_get_member_size( trailer ); - if( member_size > (unsigned long long)pos || !Lt_verify_consistency( trailer ) ) - { + if( member_size > pos || !Lt_verify_consistency( trailer ) ) + { /* bad trailer */ if( li->members <= 0 ) { if( Li_skip_trailing_data( li, infd, &pos, ignore_trailing, loose_trailing ) ) continue; else return false; } Li_set_num_error( li, "Bad trailer at pos ", pos - Lt_size ); break; } - if( seek_read( infd, header, Lh_size, pos - member_size ) != Lh_size ) - { Li_set_errno_error( li, "Error reading member header: " ); break; } - dictionary_size = Lh_get_dictionary_size( header ); - if( !Lh_verify_magic( header ) || !Lh_verify_version( header ) || - !isvalid_ds( dictionary_size ) ) + if( !Li_read_header( li, infd, header, pos - member_size ) ) break; + if( !Lh_verify( header ) ) /* bad header */ { if( li->members <= 0 ) { if( Li_skip_trailing_data( li, infd, &pos, ignore_trailing, @@ -237,6 +253,9 @@ bool Li_init( struct Lzip_index * const li, const int infd, break; } pos -= member_size; + dictionary_size = Lh_get_dictionary_size( header ); + if( li->dictionary_size < dictionary_size ) + li->dictionary_size = dictionary_size; if( !push_back_member( li, 0, Lt_get_data_size( trailer ), pos, member_size, dictionary_size ) ) return false; |