diff options
Diffstat (limited to 'alone_to_lz.cc')
-rw-r--r-- | alone_to_lz.cc | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/alone_to_lz.cc b/alone_to_lz.cc index e949f9d..dd39e34 100644 --- a/alone_to_lz.cc +++ b/alone_to_lz.cc @@ -1,5 +1,5 @@ /* Lziprecover - Data recovery tool for the lzip format - Copyright (C) 2009-2018 Antonio Diaz Diaz. + Copyright (C) 2009-2019 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 @@ -15,6 +15,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#define _FILE_OFFSET_BITS 64 + #include <algorithm> #include <cerrno> #include <climits> @@ -36,7 +38,8 @@ namespace { the file size in '*size'. The buffer is at least 20 bytes larger. In case of error, returns 0 and does not modify '*size'. */ -uint8_t * read_file( const int infd, long * const size, const Pretty_print & pp ) +uint8_t * read_file( const int infd, long * const size, + const char * const filename ) { long buffer_size = 1 << 20; uint8_t * buffer = (uint8_t *)std::malloc( buffer_size ); @@ -46,7 +49,8 @@ uint8_t * read_file( const int infd, long * const size, const Pretty_print & pp while( file_size >= buffer_size - 20 && !errno ) { if( buffer_size >= LONG_MAX ) - { pp( "file is too large" ); std::free( buffer ); return 0; } + { show_file_error( filename, "File is too large" ); std::free( buffer ); + return 0; } buffer_size = ( buffer_size <= LONG_MAX / 2 ) ? 2 * buffer_size : LONG_MAX; uint8_t * const tmp = (uint8_t *)std::realloc( buffer, buffer_size ); if( !tmp ) { std::free( buffer ); throw std::bad_alloc(); } @@ -56,10 +60,9 @@ uint8_t * read_file( const int infd, long * const size, const Pretty_print & pp } if( errno ) { - show_error( "Error reading file", errno ); + show_file_error( filename, "Error reading file", errno ); std::free( buffer ); return 0; } - close( infd ); *size = file_size; return buffer; } @@ -79,39 +82,40 @@ bool validate_ds( unsigned * const dictionary_size ) int alone_to_lz( const int infd, const Pretty_print & pp ) { - enum { lzma_header_size = 13, offset = lzma_header_size - File_header::size }; + enum { lzma_header_size = 13, offset = lzma_header_size - Lzip_header::size }; try { long file_size = 0; - uint8_t * const buffer = read_file( infd, &file_size, pp ); + uint8_t * const buffer = read_file( infd, &file_size, pp.name() ); if( !buffer ) return 1; - if( verbosity >= 1 ) pp(); - if( file_size < lzma_header_size ) - { pp( "file is too short" ); std::free( buffer ); return 2; } + { show_file_error( pp.name(), "file is too short" ); + std::free( buffer ); return 2; } + if( buffer[0] != 93 ) // (45 * 2) + (9 * 0) + 3 { - File_header & header = *(File_header *)buffer; - const unsigned dictionary_size = header.dictionary_size(); + const Lzip_header & header = *(const Lzip_header *)buffer; if( header.verify_magic() && header.verify_version() && - isvalid_ds( dictionary_size ) ) - pp( "file is already in lzip format" ); + isvalid_ds( header.dictionary_size() ) ) + show_file_error( pp.name(), "file is already in lzip format" ); else - pp( "file has non-default LZMA properties" ); + show_file_error( pp.name(), "file has non-default LZMA properties" ); std::free( buffer ); return 2; } for( int i = 5; i < 13; ++i ) if( buffer[i] != 0xFF ) - { pp( "file is non-streamed" ); std::free( buffer ); return 2; } + { show_file_error( pp.name(), "file is non-streamed" ); + std::free( buffer ); return 2; } + if( verbosity >= 1 ) pp(); unsigned dictionary_size = 0; for( int i = 4; i > 0; --i ) { dictionary_size <<= 8; dictionary_size += buffer[i]; } const unsigned orig_dictionary_size = dictionary_size; validate_ds( &dictionary_size ); - File_header & header = *(File_header *)( buffer + offset ); + Lzip_header & header = *(Lzip_header *)( buffer + offset ); header.set_magic(); header.dictionary_size( dictionary_size ); - for( int i = 0; i < File_trailer::size; ++i ) buffer[file_size++] = 0; + for( int i = 0; i < Lzip_trailer::size; ++i ) buffer[file_size++] = 0; { LZ_mtester mtester( buffer + offset, file_size - offset, dictionary_size ); const int result = mtester.test_member(); @@ -126,8 +130,8 @@ int alone_to_lz( const int infd, const Pretty_print & pp ) std::max( mtester.max_distance(), (unsigned)min_dictionary_size ); header.dictionary_size( dictionary_size ); } - File_trailer & trailer = - *(File_trailer *)( buffer + file_size - File_trailer::size ); + Lzip_trailer & trailer = + *(Lzip_trailer *)( buffer + file_size - Lzip_trailer::size ); trailer.data_crc( mtester.crc() ); trailer.data_size( mtester.data_position() ); trailer.member_size( mtester.member_position() ); @@ -142,8 +146,8 @@ int alone_to_lz( const int infd, const Pretty_print & pp ) } std::free( buffer ); } - catch( std::bad_alloc ) { pp( "Not enough memory." ); return 1; } - catch( Error e ) { pp(); show_error( e.msg, errno ); return 1; } + catch( std::bad_alloc & ) { pp( "Not enough memory." ); return 1; } + catch( Error & e ) { pp(); show_error( e.msg, errno ); return 1; } if( verbosity >= 1 ) std::fputs( "done\n", stderr ); return 0; } |