2025-02-21 11:21:25 +01:00
|
|
|
/* Lziprecover - Data recovery tool for the lzip format
|
2025-02-21 11:28:50 +01:00
|
|
|
Copyright (C) 2009-2018 Antonio Diaz Diaz.
|
2025-02-21 10:12:48 +01:00
|
|
|
|
|
|
|
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
|
2025-02-21 11:20:57 +01:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2025-02-21 10:12:48 +01:00
|
|
|
(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.
|
|
|
|
|
|
|
|
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 <algorithm>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "lzip.h"
|
|
|
|
#include "decoder.h"
|
2025-02-21 11:23:37 +01:00
|
|
|
#include "block.h"
|
2025-02-21 11:11:34 +01:00
|
|
|
#include "file_index.h"
|
2025-02-21 10:12:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2025-02-21 11:26:24 +01:00
|
|
|
int decompress_member( const int infd, const Pretty_print & pp,
|
2025-02-21 11:11:34 +01:00
|
|
|
const unsigned long long mpos,
|
|
|
|
const unsigned long long outskip,
|
|
|
|
const unsigned long long outend )
|
2025-02-21 10:12:48 +01:00
|
|
|
{
|
2025-02-21 11:26:24 +01:00
|
|
|
Range_decoder rdec( infd );
|
|
|
|
File_header header;
|
|
|
|
rdec.read_data( header.data, File_header::size );
|
|
|
|
if( rdec.finished() ) // End Of File
|
|
|
|
{ pp( "File ends unexpectedly at member header." ); return 2; }
|
|
|
|
if( !verify_header( header, pp ) ) return 2;
|
|
|
|
const unsigned dictionary_size = header.dictionary_size();
|
|
|
|
if( !isvalid_ds( dictionary_size ) )
|
|
|
|
{ pp( "Invalid dictionary size in member header." ); return 2; }
|
|
|
|
|
2025-02-21 11:28:50 +01:00
|
|
|
if( verbosity >= 2 ) pp();
|
2025-02-21 11:26:24 +01:00
|
|
|
|
|
|
|
LZ_decoder decoder( rdec, dictionary_size, outfd, outskip, outend );
|
|
|
|
const int result = decoder.decode_member( pp );
|
|
|
|
if( result != 0 )
|
|
|
|
{
|
2025-02-21 11:28:50 +01:00
|
|
|
if( verbosity >= 0 && result <= 2 )
|
2025-02-21 10:12:48 +01:00
|
|
|
{
|
2025-02-21 11:26:24 +01:00
|
|
|
pp();
|
|
|
|
std::fprintf( stderr, "%s at pos %llu\n", ( result == 2 ) ?
|
|
|
|
"File ends unexpectedly" : "Decoder error",
|
|
|
|
mpos + rdec.member_position() );
|
2025-02-21 10:12:48 +01:00
|
|
|
}
|
2025-02-21 11:26:24 +01:00
|
|
|
return 2;
|
2025-02-21 10:12:48 +01:00
|
|
|
}
|
2025-02-21 11:28:50 +01:00
|
|
|
if( verbosity >= 2 ) std::fputs( "done\n", stderr );
|
2025-02-21 11:11:34 +01:00
|
|
|
return 0;
|
2025-02-21 10:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-21 11:23:37 +01:00
|
|
|
const char * format_num( unsigned long long num,
|
2025-02-21 11:27:58 +01:00
|
|
|
unsigned long long limit = -1ULL,
|
|
|
|
const int set_prefix = 0 )
|
2025-02-21 11:23:37 +01:00
|
|
|
{
|
|
|
|
const char * const si_prefix[8] =
|
|
|
|
{ "k", "M", "G", "T", "P", "E", "Z", "Y" };
|
|
|
|
const char * const binary_prefix[8] =
|
|
|
|
{ "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
|
2025-02-21 11:25:43 +01:00
|
|
|
enum { buffers = 8, bufsize = 32 };
|
|
|
|
static char buffer[buffers][bufsize]; // circle of static buffers for printf
|
|
|
|
static int current = 0;
|
2025-02-21 11:23:37 +01:00
|
|
|
static bool si = true;
|
|
|
|
|
|
|
|
if( set_prefix ) si = ( set_prefix > 0 );
|
|
|
|
const unsigned factor = ( si ? 1000 : 1024 );
|
2025-02-21 11:25:43 +01:00
|
|
|
char * const buf = buffer[current++]; current %= buffers;
|
2025-02-21 11:23:37 +01:00
|
|
|
const char * const * prefix = ( si ? si_prefix : binary_prefix );
|
|
|
|
const char * p = "";
|
|
|
|
bool exact = ( num % factor == 0 );
|
|
|
|
|
|
|
|
for( int i = 0; i < 8 && ( num > limit || ( exact && num >= factor ) ); ++i )
|
|
|
|
{ num /= factor; if( num % factor != 0 ) exact = false; p = prefix[i]; }
|
2025-02-21 11:25:43 +01:00
|
|
|
snprintf( buf, bufsize, "%llu %s", num, p );
|
2025-02-21 11:23:37 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2025-02-21 11:27:58 +01:00
|
|
|
} // end namespace
|
|
|
|
|
2025-02-21 11:23:37 +01:00
|
|
|
|
2025-02-21 11:16:43 +01:00
|
|
|
bool safe_seek( const int fd, const long long pos )
|
|
|
|
{
|
|
|
|
if( lseek( fd, pos, SEEK_SET ) == pos ) return true;
|
|
|
|
show_error( "Seek error", errno ); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-02-21 10:12:48 +01:00
|
|
|
int range_decompress( const std::string & input_filename,
|
2025-02-21 11:26:24 +01:00
|
|
|
const std::string & default_output_filename,
|
2025-02-21 11:28:50 +01:00
|
|
|
Block range, const bool force, const bool ignore_errors,
|
|
|
|
const bool ignore_trailing, const bool loose_trailing,
|
2025-02-21 11:27:58 +01:00
|
|
|
const bool to_stdout )
|
2025-02-21 10:12:48 +01:00
|
|
|
{
|
|
|
|
struct stat in_stats;
|
2025-02-21 11:19:58 +01:00
|
|
|
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
|
2025-02-21 10:12:48 +01:00
|
|
|
if( infd < 0 ) return 1;
|
2025-02-21 11:11:34 +01:00
|
|
|
|
2025-02-21 11:28:50 +01:00
|
|
|
Pretty_print pp( input_filename );
|
|
|
|
const File_index file_index( infd, ignore_errors, ignore_trailing,
|
|
|
|
loose_trailing );
|
2025-02-21 11:11:34 +01:00
|
|
|
if( file_index.retval() != 0 )
|
2025-02-21 11:27:58 +01:00
|
|
|
{ show_file_error( input_filename.c_str(), file_index.error().c_str() );
|
|
|
|
return file_index.retval(); }
|
2025-02-21 11:11:34 +01:00
|
|
|
|
2025-02-21 11:27:58 +01:00
|
|
|
if( range.end() > file_index.udata_size() )
|
|
|
|
range.size( std::max( 0LL, file_index.udata_size() - range.pos() ) );
|
2025-02-21 10:12:48 +01:00
|
|
|
if( range.size() <= 0 )
|
2025-02-21 11:28:50 +01:00
|
|
|
{ pp( "Nothing to do." ); return 0; }
|
2025-02-21 10:12:48 +01:00
|
|
|
|
|
|
|
if( verbosity >= 1 )
|
2025-02-21 11:28:50 +01:00
|
|
|
std::fprintf( stderr, "Decompressing range %sB to %sB (%sof %sBytes)\n",
|
2025-02-21 11:25:43 +01:00
|
|
|
format_num( range.pos() ),
|
|
|
|
format_num( range.pos() + range.size() ),
|
2025-02-21 11:28:50 +01:00
|
|
|
format_num( range.size() ),
|
|
|
|
format_num( file_index.udata_size() ) );
|
2025-02-21 10:12:48 +01:00
|
|
|
|
2025-02-21 11:26:24 +01:00
|
|
|
if( to_stdout || default_output_filename.empty() )
|
2025-02-21 10:12:48 +01:00
|
|
|
outfd = STDOUT_FILENO;
|
|
|
|
else
|
2025-02-21 11:26:24 +01:00
|
|
|
{
|
|
|
|
output_filename = default_output_filename;
|
|
|
|
if( !open_outstream( force, false, false, false ) )
|
|
|
|
{ close( infd ); return 1; }
|
|
|
|
}
|
2025-02-21 11:16:43 +01:00
|
|
|
|
2025-02-21 10:12:48 +01:00
|
|
|
int retval = 0;
|
2025-02-21 11:20:27 +01:00
|
|
|
for( long i = 0; i < file_index.members(); ++i )
|
2025-02-21 10:12:48 +01:00
|
|
|
{
|
2025-02-21 11:11:34 +01:00
|
|
|
const Block & db = file_index.dblock( i );
|
2025-02-21 10:12:48 +01:00
|
|
|
if( range.overlaps( db ) )
|
|
|
|
{
|
2025-02-21 11:28:50 +01:00
|
|
|
if( verbosity >= 3 && file_index.members() > 1 )
|
2025-02-21 11:20:27 +01:00
|
|
|
std::fprintf( stderr, "Decompressing member %3ld\n", i + 1 );
|
2025-02-21 10:12:48 +01:00
|
|
|
const long long outskip = std::max( 0LL, range.pos() - db.pos() );
|
2025-02-21 11:13:34 +01:00
|
|
|
const long long outend = std::min( db.size(), range.end() - db.pos() );
|
2025-02-21 11:11:34 +01:00
|
|
|
const long long mpos = file_index.mblock( i ).pos();
|
|
|
|
if( !safe_seek( infd, mpos ) ) { retval = 1; break; }
|
2025-02-21 11:26:24 +01:00
|
|
|
const int tmp = decompress_member( infd, pp, mpos, outskip, outend );
|
2025-02-21 11:27:58 +01:00
|
|
|
if( tmp && ( tmp != 2 || !ignore_errors ) )
|
2025-02-21 11:26:24 +01:00
|
|
|
cleanup_and_fail( tmp );
|
2025-02-21 11:13:34 +01:00
|
|
|
if( tmp > retval ) retval = tmp;
|
2025-02-21 10:12:48 +01:00
|
|
|
pp.reset();
|
|
|
|
}
|
|
|
|
}
|
2025-02-21 11:11:34 +01:00
|
|
|
close( infd );
|
2025-02-21 11:26:24 +01:00
|
|
|
retval = std::max( retval, close_outstream( &in_stats ) );
|
2025-02-21 11:13:34 +01:00
|
|
|
if( verbosity >= 2 && retval == 0 )
|
2025-02-21 11:25:43 +01:00
|
|
|
std::fputs( "Byte range decompressed successfully.\n", stderr );
|
2025-02-21 10:12:48 +01:00
|
|
|
return retval;
|
|
|
|
}
|