summaryrefslogtreecommitdiffstats
path: root/range_dec.cc
diff options
context:
space:
mode:
Diffstat (limited to 'range_dec.cc')
-rw-r--r--range_dec.cc125
1 files changed, 29 insertions, 96 deletions
diff --git a/range_dec.cc b/range_dec.cc
index 0fa9708..8b4d6e0 100644
--- a/range_dec.cc
+++ b/range_dec.cc
@@ -1,5 +1,5 @@
/* Lziprecover - Data recovery tool for the lzip format
- Copyright (C) 2009-2014 Antonio Diaz Diaz.
+ Copyright (C) 2009-2015 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
@@ -30,101 +30,12 @@
#include "lzip.h"
#include "decoder.h"
+#include "block.h"
#include "file_index.h"
namespace {
-const char * format_num( unsigned long long num,
- unsigned long long limit = -1ULL,
- const int set_prefix = 0 )
- {
- 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" };
- static bool si = true;
- static char buf[32];
-
- if( set_prefix ) si = ( set_prefix > 0 );
- const unsigned factor = ( si ? 1000 : 1024 );
- 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]; }
- snprintf( buf, sizeof buf, "%llu %s", num, p );
- return buf;
- }
-
-
-// Returns the number of chars read, or 0 if error.
-//
-int parse_long_long( const char * const ptr, long long & value )
- {
- char * tail;
- errno = 0;
- value = strtoll( ptr, &tail, 0 );
- if( tail == ptr || errno || value < 0 ) return 0;
- int c = tail - ptr;
-
- if( ptr[c] )
- {
- const int factor = ( ptr[c+1] == 'i' ) ? 1024 : 1000;
- int exponent = 0;
- switch( ptr[c] )
- {
- case 'Y': exponent = 8; break;
- case 'Z': exponent = 7; break;
- case 'E': exponent = 6; break;
- case 'P': exponent = 5; break;
- case 'T': exponent = 4; break;
- case 'G': exponent = 3; break;
- case 'M': exponent = 2; break;
- case 'K': if( factor == 1024 ) exponent = 1; else return 0; break;
- case 'k': if( factor == 1000 ) exponent = 1; else return 0; break;
- }
- if( exponent > 0 )
- {
- ++c;
- if( ptr[c] == 'i' ) { ++c; if( value ) format_num( 0, 0, -1 ); }
- if( ptr[c] == 'B' ) ++c;
- for( int i = 0; i < exponent; ++i )
- {
- if( INT64_MAX / factor >= value ) value *= factor;
- else return 0;
- }
- }
- }
- return c;
- }
-
-
-// Recognized formats: <begin> <begin>-<end> <begin>,<size>
-//
-void parse_range( const char * const ptr, Block & range )
- {
- long long value = 0;
- int c = parse_long_long( ptr, value ); // pos
- if( c && value >= 0 && value < INT64_MAX &&
- ( ptr[c] == 0 || ptr[c] == ',' || ptr[c] == '-' ) )
- {
- range.pos( value );
- if( ptr[c] == 0 ) { range.size( INT64_MAX - value ); return; }
- const bool issize = ( ptr[c] == ',' );
- c = parse_long_long( ptr + c + 1, value ); // size
- if( c && value > 0 && ( issize || value > range.pos() ) )
- {
- if( !issize ) value -= range.pos();
- if( INT64_MAX - range.pos() >= value ) { range.size( value ); return; }
- }
- }
- show_error( "Bad decompression range.", 0, true );
- std::exit( 1 );
- }
-
-
int decompress_member( const int infd, const int outfd,
const Pretty_print & pp,
const unsigned long long mpos,
@@ -221,6 +132,30 @@ int list_file( const char * const input_filename, const Pretty_print & pp )
} // end namespace
+const char * format_num( unsigned long long num,
+ unsigned long long limit,
+ const int set_prefix )
+ {
+ 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" };
+ static bool si = true;
+ static char buf[32];
+
+ if( set_prefix ) si = ( set_prefix > 0 );
+ const unsigned factor = ( si ? 1000 : 1024 );
+ 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]; }
+ snprintf( buf, sizeof buf, "%llu %s", num, p );
+ return buf;
+ }
+
+
bool safe_seek( const int fd, const long long pos )
{
if( lseek( fd, pos, SEEK_SET ) == pos ) return true;
@@ -245,11 +180,9 @@ int list_files( const std::vector< std::string > & filenames,
int range_decompress( const std::string & input_filename,
const std::string & output_filename,
- const std::string & range_string, const int verbosity,
- const bool force, const bool ignore, const bool to_stdout )
+ Block range, const int verbosity, const bool force,
+ const bool ignore, const bool to_stdout )
{
- Block range( 0, 0 );
- parse_range( range_string.c_str(), range );
struct stat in_stats;
const int infd = open_instream( input_filename.c_str(), &in_stats, true, true );
if( infd < 0 ) return 1;
@@ -262,7 +195,7 @@ int range_decompress( const std::string & input_filename,
if( range.end() > file_index.data_end() )
range.size( std::max( 0LL, file_index.data_end() - range.pos() ) );
if( range.size() <= 0 )
- { if( verbosity >= 1 ) pp( "Nothing to do." ); return 0; }
+ { if( verbosity >= 0 ) pp( "Nothing to do." ); return 0; }
if( verbosity >= 1 )
{