From 5af06484d51d352018c9bac507d6157ad93ec7cd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 25 Jan 2021 14:51:50 +0100 Subject: Adding upstream version 1.22. Signed-off-by: Daniel Baumann --- alone_to_lz.cc | 144 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 74 deletions(-) (limited to 'alone_to_lz.cc') diff --git a/alone_to_lz.cc b/alone_to_lz.cc index dd39e34..1f65dfe 100644 --- a/alone_to_lz.cc +++ b/alone_to_lz.cc @@ -1,18 +1,18 @@ -/* Lziprecover - Data recovery tool for the lzip format - Copyright (C) 2009-2019 Antonio Diaz Diaz. +/* Lziprecover - Data recovery tool for the lzip format + Copyright (C) 2009-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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ #define _FILE_OFFSET_BITS 64 @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -83,71 +84,66 @@ 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 - Lzip_header::size }; - - try { - long file_size = 0; - uint8_t * const buffer = read_file( infd, &file_size, pp.name() ); - if( !buffer ) return 1; - if( file_size < lzma_header_size ) - { show_file_error( pp.name(), "file is too short" ); - std::free( buffer ); return 2; } - - if( buffer[0] != 93 ) // (45 * 2) + (9 * 0) + 3 - { - const Lzip_header & header = *(const Lzip_header *)buffer; - if( header.verify_magic() && header.verify_version() && - isvalid_ds( header.dictionary_size() ) ) - show_file_error( pp.name(), "file is already in lzip format" ); - else - 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 ) - { 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 ); - Lzip_header & header = *(Lzip_header *)( buffer + offset ); - header.set_magic(); - header.dictionary_size( dictionary_size ); - for( int i = 0; i < Lzip_trailer::size; ++i ) buffer[file_size++] = 0; + long file_size = 0; + uint8_t * const buffer = read_file( infd, &file_size, pp.name() ); + if( !buffer ) return 1; + if( file_size < lzma_header_size ) + { show_file_error( pp.name(), "file is too short" ); + std::free( buffer ); return 2; } + + if( buffer[0] != 93 ) // (45 * 2) + (9 * 0) + 3 + { + const Lzip_header & header = *(const Lzip_header *)buffer; + if( header.verify_magic() && header.verify_version() && + isvalid_ds( header.dictionary_size() ) ) + show_file_error( pp.name(), "file is already in lzip format" ); + else + 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 ) + { 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 ); + Lzip_header & header = *(Lzip_header *)( buffer + offset ); + header.set_magic(); + header.dictionary_size( dictionary_size ); + 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(); + if( result == 1 && orig_dictionary_size > max_dictionary_size ) + { pp( "dictionary size is too large" ); std::free( buffer ); return 2; } + if( result != 3 || !mtester.finished() ) + { pp( "file is corrupt" ); std::free( buffer ); return 2; } + if( mtester.max_distance() < dictionary_size && + dictionary_size > min_dictionary_size ) { - LZ_mtester mtester( buffer + offset, file_size - offset, dictionary_size ); - const int result = mtester.test_member(); - if( result == 1 && orig_dictionary_size > max_dictionary_size ) - { pp( "dictionary size is too large" ); std::free( buffer ); return 2; } - if( result != 3 || !mtester.finished() ) - { pp( "file is corrupt" ); std::free( buffer ); return 2; } - if( mtester.max_distance() < dictionary_size && - dictionary_size > min_dictionary_size ) - { - dictionary_size = - std::max( mtester.max_distance(), (unsigned)min_dictionary_size ); - header.dictionary_size( dictionary_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() ); + dictionary_size = + std::max( mtester.max_distance(), (unsigned)min_dictionary_size ); + header.dictionary_size( dictionary_size ); } - LZ_mtester mtester( buffer + offset, file_size - offset, dictionary_size ); - if( mtester.test_member() != 0 || !mtester.finished() ) - { pp( "conversion failed" ); std::free( buffer ); return 2; } - if( writeblock( outfd, buffer + offset, file_size - offset ) != file_size - offset ) - { - show_error( "Error writing output file", errno ); - std::free( buffer ); return 1; - } - std::free( buffer ); + 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() ); + } + LZ_mtester mtester( buffer + offset, file_size - offset, dictionary_size ); + if( mtester.test_member() != 0 || !mtester.finished() ) + { pp( "conversion failed" ); std::free( buffer ); return 2; } + if( writeblock( outfd, buffer + offset, file_size - offset ) != file_size - offset ) + { + show_error( "Error writing output file", errno ); + std::free( buffer ); return 1; } - catch( std::bad_alloc & ) { pp( "Not enough memory." ); return 1; } - catch( Error & e ) { pp(); show_error( e.msg, errno ); return 1; } + std::free( buffer ); if( verbosity >= 1 ) std::fputs( "done\n", stderr ); return 0; } -- cgit v1.2.3