summaryrefslogtreecommitdiffstats
path: root/lzip.h
diff options
context:
space:
mode:
Diffstat (limited to 'lzip.h')
-rw-r--r--lzip.h148
1 files changed, 118 insertions, 30 deletions
diff --git a/lzip.h b/lzip.h
index 1861443..78b7dc2 100644
--- a/lzip.h
+++ b/lzip.h
@@ -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
@@ -121,7 +121,7 @@ public:
{
const std::string & s = filenames[i];
const unsigned len = ( s == "-" ) ? stdin_name_len : s.size();
- if( len > longest_name ) longest_name = len;
+ if( longest_name < len ) longest_name = len;
}
if( longest_name == 0 ) longest_name = stdin_name_len;
}
@@ -140,7 +140,7 @@ public:
if( filename.size() && filename != "-" ) name_ = filename;
else name_ = stdin_name;
padded_name = " "; padded_name += name_; padded_name += ": ";
- if( name_.size() < longest_name )
+ if( longest_name > name_.size() )
padded_name.append( longest_name - name_.size(), ' ' );
first_post = true;
}
@@ -198,30 +198,30 @@ inline int real_bits( unsigned value )
}
-const uint8_t magic_string[4] = { 0x4C, 0x5A, 0x49, 0x50 }; // "LZIP"
+const uint8_t lzip_magic[4] = { 0x4C, 0x5A, 0x49, 0x50 }; // "LZIP"
-struct File_header
+struct Lzip_header
{
uint8_t data[6]; // 0-3 magic bytes
// 4 version
// 5 coded_dict_size
enum { size = 6 };
- void set_magic() { std::memcpy( data, magic_string, 4 ); data[4] = 1; }
+ void set_magic() { std::memcpy( data, lzip_magic, 4 ); data[4] = 1; }
bool verify_magic() const
- { return ( std::memcmp( data, magic_string, 4 ) == 0 ); }
+ { return ( std::memcmp( data, lzip_magic, 4 ) == 0 ); }
bool verify_prefix( const int sz ) const // detect (truncated) header
{
for( int i = 0; i < sz && i < 4; ++i )
- if( data[i] != magic_string[i] ) return false;
+ if( data[i] != lzip_magic[i] ) return false;
return ( sz > 0 );
}
bool verify_corrupt() const // detect corrupt header
{
int matches = 0;
for( int i = 0; i < 4; ++i )
- if( data[i] == magic_string[i] ) ++matches;
+ if( data[i] == lzip_magic[i] ) ++matches;
return ( matches > 1 && matches < 4 );
}
@@ -253,12 +253,11 @@ struct File_header
};
-struct File_trailer
+struct Lzip_trailer
{
uint8_t data[20]; // 0-3 CRC32 of the uncompressed data
// 4-11 size of the uncompressed data
// 12-19 member size including header and trailer
-
enum { size = 20 };
unsigned data_crc() const
@@ -290,6 +289,20 @@ struct File_trailer
void member_size( unsigned long long sz )
{ for( int i = 12; i <= 19; ++i ) { data[i] = (uint8_t)sz; sz >>= 8; } }
+
+ bool verify_consistency() const // check internal consistency
+ {
+ const unsigned crc = data_crc();
+ const unsigned long long dsize = data_size();
+ if( ( crc == 0 ) != ( dsize == 0 ) ) return false;
+ const unsigned long long msize = member_size();
+ if( msize < min_member_size ) return false;
+ const unsigned long long mlimit = ( 9 * dsize + 7 ) / 8 + min_member_size;
+ if( mlimit > dsize && msize > mlimit ) return false;
+ const unsigned long long dlimit = 7090 * ( msize - 26 ) - 1;
+ if( dlimit > msize && dsize > dlimit ) return false;
+ return true;
+ }
};
@@ -310,6 +323,72 @@ struct Bad_byte
};
+#ifndef INT64_MAX
+#define INT64_MAX 0x7FFFFFFFFFFFFFFFLL
+#endif
+
+class Block
+ {
+ long long pos_, size_; // pos + size <= INT64_MAX
+
+public:
+ Block( const long long p, const long long s ) : pos_( p ), size_( s ) {}
+
+ long long pos() const { return pos_; }
+ long long size() const { return size_; }
+ long long end() const { return pos_ + size_; }
+
+ void pos( const long long p ) { pos_ = p; }
+ void size( const long long s ) { size_ = s; }
+
+ bool operator==( const Block & b ) const
+ { return pos_ == b.pos_ && size_ == b.size_; }
+ bool operator!=( const Block & b ) const
+ { return pos_ != b.pos_ || size_ != b.size_; }
+
+ bool operator<( const Block & b ) const { return pos_ < b.pos_; }
+
+ bool includes( const long long pos ) const
+ { return ( pos_ <= pos && end() > pos ); }
+ bool overlaps( const Block & b ) const
+ { return ( pos_ < b.end() && b.pos_ < end() ); }
+
+ void shift( Block & b ) { ++size_; ++b.pos_; --b.size_; }
+ Block split( const long long pos );
+ };
+
+
+struct Member_list // members/gaps/tdata to be dumped/removed/stripped
+ {
+ bool damaged;
+ bool tdata;
+ bool in, rin;
+ std::vector< Block > range_vector, rrange_vector;
+
+ Member_list() : damaged( false ), tdata( false ), in( true ), rin( true ) {}
+ void parse( const char * p );
+
+ bool range() const { return range_vector.size() || rrange_vector.size(); }
+
+ // blocks is the sum of members + gaps, excluding trailing data
+ bool includes( const long i, const long blocks ) const
+ {
+ for( unsigned j = 0; j < range_vector.size(); ++j )
+ {
+ if( range_vector[j].pos() > i ) break;
+ if( range_vector[j].end() > i ) return in;
+ }
+ if( i >= 0 && i < blocks )
+ for( unsigned j = 0; j < rrange_vector.size(); ++j )
+ {
+ if( rrange_vector[j].pos() > blocks - i - 1 ) break;
+ if( rrange_vector[j].end() > blocks - i - 1 ) return rin;
+ }
+ return !in || !rin;
+ }
+ };
+
+
struct Error
{
const char * const msg;
@@ -334,14 +413,25 @@ int alone_to_lz( const int infd, const Pretty_print & pp );
long readblock( const int fd, uint8_t * const buf, const long size );
long writeblock( const int fd, const uint8_t * const buf, const long size );
-// defined in file_index.cc
-int seek_read( const int fd, uint8_t * const buf, const int size,
- const long long pos );
+// defined in dump_remove.cc
+int dump_members( const std::vector< std::string > & filenames,
+ const std::string & default_output_filename,
+ const Member_list & member_list, const bool force,
+ bool ignore_errors, bool ignore_trailing,
+ const bool loose_trailing, const bool strip );
+int remove_members( const std::vector< std::string > & filenames,
+ const Member_list & member_list, bool ignore_errors,
+ bool ignore_trailing, const bool loose_trailing );
// defined in list.cc
int list_files( const std::vector< std::string > & filenames,
+ const bool ignore_errors,
const bool ignore_trailing, const bool loose_trailing );
+// defined in lzip_index.cc
+int seek_read( const int fd, uint8_t * const buf, const int size,
+ const long long pos );
+
// defined in main.cc
extern std::string output_filename; // global vars for output file
extern int outfd;
@@ -357,6 +447,7 @@ bool open_outstream( const bool force, const bool from_stdin,
const bool rw = false, const bool skipping = true );
bool file_exists( const std::string & filename );
void cleanup_and_fail( const int retval );
+void set_signal_handler();
int close_outstream( const struct stat * const in_statsp );
std::string insert_fixed( std::string name );
void show_error( const char * const msg, const int errcode = 0,
@@ -364,9 +455,7 @@ void show_error( const char * const msg, const int errcode = 0,
void show_file_error( const char * const filename, const char * const msg,
const int errcode = 0 );
void internal_error( const char * const msg );
-void show_error2( const char * const msg1, const char * const name,
- const char * const msg2 );
-void show_error4( const char * const msg1, const char * const name1,
+void show_2file_error( const char * const msg1, const char * const name1,
const char * const name2, const char * const msg2 );
class Range_decoder;
void show_dprogress( const unsigned long long cfile_size = 0,
@@ -377,32 +466,31 @@ void show_dprogress( const unsigned long long cfile_size = 0,
// defined in merge.cc
bool copy_file( const int infd, const int outfd,
const long long max_size = -1 );
-bool test_member_from_file( const int infd, const unsigned long long msize,
- long long * const failure_posp = 0 );
+int test_member_from_file( const int infd, const unsigned long long msize,
+ long long * const failure_posp = 0 );
int merge_files( const std::vector< std::string > & filenames,
const std::string & default_output_filename,
- const bool force );
+ const bool force, const char terminator );
// defined in range_dec.cc
bool safe_seek( const int fd, const long long pos );
+int range_decompress( const std::string & input_filename,
+ const std::string & default_output_filename,
+ Block range, const bool force, const bool ignore_errors,
+ const bool ignore_trailing, const bool loose_trailing,
+ const bool to_stdout );
// defined in repair.cc
int repair_file( const std::string & input_filename,
const std::string & default_output_filename,
- const bool force );
+ const bool force, const char terminator );
+int debug_delay( const std::string & input_filename, Block range,
+ const char terminator );
int debug_repair( const std::string & input_filename,
- const Bad_byte & bad_byte );
+ const Bad_byte & bad_byte, const char terminator );
int debug_decompress( const std::string & input_filename,
const Bad_byte & bad_byte, const bool show_packets );
// defined in split.cc
-bool verify_header( const File_header & header, const Pretty_print & pp );
int split_file( const std::string & input_filename,
const std::string & default_output_filename, const bool force );
-
-// defined in trailing_data.cc
-int dump_tdata( const std::vector< std::string > & filenames,
- const std::string & default_output_filename, const bool force,
- const bool strip, const bool loose_trailing );
-int remove_tdata( const std::vector< std::string > & filenames,
- const bool loose_trailing );