/* Lziprecover - Member recoverer program for lzip compressed files Copyright (C) 2008, 2009, 2010 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 3 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. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /* Return values: 0 for a normal exit, 1 for environmental problems (file not found, invalid flags, I/O errors, etc), 2 to indicate a corrupt or invalid input file, 3 for an internal consistency error (eg, bug) which caused lzip to panic. */ #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include "arg_parser.h" #include "lzip.h" namespace { const char * invocation_name = 0; const char * const Program_name = "Lziprecover"; const char * const program_name = "lziprecover"; const char * const program_year = "2010"; #ifdef O_BINARY const int o_binary = O_BINARY; #else const int o_binary = 0; #endif void show_help() throw() { std::printf( "%s - Member recoverer program for lzip compressed files.\n", Program_name ); std::printf( "\nSearches for members in .lz files, and writes each member in its own .lz\n" ); std::printf( "file. You can then use `lzip -t' to test the integrity of the resulting\n" ); std::printf( "files, and decompress those which are undamaged.\n" ); std::printf( "\nUsage: %s [options] file\n", invocation_name ); std::printf( "Options:\n" ); std::printf( " -h, --help display this help and exit\n" ); std::printf( " -V, --version output version information and exit\n" ); std::printf( " -q, --quiet suppress all messages\n" ); std::printf( " -v, --verbose be verbose (a 2nd -v gives more)\n" ); std::printf( "\nReport bugs to lzip-bug@nongnu.org\n"); std::printf( "Lzip home page: http://www.nongnu.org/lzip/lzip.html\n" ); } void show_version() throw() { std::printf( "%s %s\n", Program_name, PROGVERSION ); std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year ); std::printf( "License GPLv3+: GNU GPL version 3 or later \n" ); std::printf( "This is free software: you are free to change and redistribute it.\n" ); std::printf( "There is NO WARRANTY, to the extent permitted by law.\n" ); } int open_instream( const std::string & input_filename ) throw() { int infd = open( input_filename.c_str(), O_RDONLY | o_binary ); if( infd < 0 ) { if( verbosity >= 0 ) std::fprintf( stderr, "%s: Can't open input file `%s': %s.\n", program_name, input_filename.c_str(), std::strerror( errno ) ); } else { struct stat in_stats; const int i = fstat( infd, &in_stats ); if( i < 0 || !S_ISREG( in_stats.st_mode ) ) { if( verbosity >= 0 ) std::fprintf( stderr, "%s: input file `%s' is not a regular file.\n", program_name, input_filename.c_str() ); close( infd ); infd = -1; } } return infd; } int open_outstream( const std::string & output_filename ) throw() { int outfd = open( output_filename.c_str(), O_CREAT | O_TRUNC | O_WRONLY | o_binary, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); if( outfd < 0 ) { if( verbosity >= 0 ) std::fprintf( stderr, "%s: Can't create output file `%s': %s.\n", program_name, output_filename.c_str(), std::strerror( errno ) ); } return outfd; } bool next_filename( std::string & output_filename ) { for( int i = 7; i >= 3; --i ) // "rec00001" { if( output_filename[i] < '9' ) { ++output_filename[i]; return true; } else output_filename[i] = '0'; } return false; } bool verify_header( const uint8_t * const buffer, const int pos ) { File_header header; for( int i = 0; i < File_header::size; ++i ) header.data[i] = buffer[pos+i]; if( !header.verify_magic() ) { show_error( "bad magic number (file not in lzip format).\n" ); return false; } if( header.version() == 0 ) { show_error( "version 0 member format can't be recovered.\n" ); return false; } if( header.version() != 1 ) { if( verbosity >= 0 ) std::fprintf( stderr, "version %d member format not supported, newer %s needed.\n", header.version(), program_name ); return false; } return true; } int process_file( const std::string & input_filename, uint8_t * & base_buffer ) { const int hsize = File_header::size; const int tsize = File_trailer::size(); const int buffer_size = 65536; const int base_buffer_size = tsize + buffer_size + hsize; base_buffer = new uint8_t[base_buffer_size]; uint8_t * const buffer = base_buffer + tsize; const int infd = open_instream( input_filename ); if( infd < 0 ) return 1; int size = readblock( infd, buffer, buffer_size + hsize ) - hsize; bool at_stream_end = ( size < buffer_size ); if( size != buffer_size && errno ) { show_error( "read error", errno ); return 1; } if( size <= tsize ) { show_error( "file too short" ); return 2; } if( !verify_header( buffer, 0 ) ) return 2; std::string output_filename( "rec00001" ); output_filename += input_filename; int outfd = open_outstream( output_filename ); if( outfd < 0 ) { close( infd ); return 1; } long long partial_member_size = 0; while( true ) { int pos = 0; for( int newpos = 1; newpos <= size; ++newpos ) if( buffer[newpos] == magic_string[0] && buffer[newpos+1] == magic_string[1] && buffer[newpos+2] == magic_string[2] && buffer[newpos+3] == magic_string[3] ) { long long member_size = 0; for( int i = 1; i <= 8; ++i ) { member_size <<= 8; member_size += base_buffer[tsize+newpos-i]; } if( partial_member_size + newpos - pos == member_size ) { // header found const int wr = writeblock( outfd, buffer + pos, newpos - pos ); if( wr != newpos - pos ) { show_error( "write error", errno ); return 1; } if( close( outfd ) != 0 ) { show_error( "error closing output file", errno ); return 1; } if( !next_filename( output_filename ) ) { show_error( "too many members in file" ); close( infd ); return 1; } outfd = open_outstream( output_filename ); if( outfd < 0 ) { close( infd ); return 1; } partial_member_size = 0; pos = newpos; } } if( at_stream_end ) { const int wr = writeblock( outfd, buffer + pos, size + hsize - pos ); if( wr != size + hsize - pos ) { show_error( "write error", errno ); return 1; } break; } if( pos < buffer_size ) { partial_member_size += buffer_size - pos; const int wr = writeblock( outfd, buffer + pos, buffer_size - pos ); if( wr != buffer_size - pos ) { show_error( "write error", errno ); return 1; } } std::memcpy( base_buffer, base_buffer + buffer_size, tsize + hsize ); size = readblock( infd, buffer + hsize, buffer_size ); at_stream_end = ( size < buffer_size ); if( size != buffer_size && errno ) { show_error( "read error", errno ); return 1; } } close( infd ); if( close( outfd ) != 0 ) { show_error( "error closing output file", errno ); return 1; } return 0; } } // end namespace int verbosity = 0; void show_error( const char * const msg, const int errcode, const bool help ) throw() { if( verbosity >= 0 ) { if( msg && msg[0] != 0 ) { std::fprintf( stderr, "%s: %s", program_name, msg ); if( errcode > 0 ) std::fprintf( stderr, ": %s", std::strerror( errcode ) ); std::fprintf( stderr, "\n" ); } if( help && invocation_name && invocation_name[0] != 0 ) std::fprintf( stderr, "Try `%s --help' for more information.\n", invocation_name ); } } void internal_error( const char * const msg ) { std::string s( "internal error: " ); s += msg; show_error( s.c_str() ); std::exit( 3 ); } // Returns the number of bytes really read. // If (returned value < size) and (errno == 0), means EOF was reached. // int readblock( const int fd, uint8_t * const buf, const int size ) throw() { int rest = size; errno = 0; while( rest > 0 ) { errno = 0; const int n = read( fd, buf + size - rest, rest ); if( n > 0 ) rest -= n; else if( n == 0 ) break; else if( errno != EINTR && errno != EAGAIN ) break; } return ( rest > 0 ) ? size - rest : size; } // Returns the number of bytes really written. // If (returned value < size), it is always an error. // int writeblock( const int fd, const uint8_t * const buf, const int size ) throw() { int rest = size; errno = 0; while( rest > 0 ) { errno = 0; const int n = write( fd, buf + size - rest, rest ); if( n > 0 ) rest -= n; else if( errno && errno != EINTR && errno != EAGAIN ) break; } return ( rest > 0 ) ? size - rest : size; } int main( const int argc, const char * const argv[] ) { invocation_name = argv[0]; const Arg_parser::Option options[] = { { 'h', "help", Arg_parser::no }, { 'q', "quiet", Arg_parser::no }, { 'v', "verbose", Arg_parser::no }, { 'V', "version", Arg_parser::no }, { 0 , 0, Arg_parser::no } }; Arg_parser parser( argc, argv, options ); if( parser.error().size() ) // bad option { show_error( parser.error().c_str(), 0, true ); return 1; } int argind = 0; for( ; argind < parser.arguments(); ++argind ) { const int code = parser.code( argind ); if( !code ) break; // no more options switch( code ) { case 'h': show_help(); return 0; case 'q': verbosity = -1; break; case 'v': if( verbosity < 4 ) ++verbosity; break; case 'V': show_version(); return 0; default : internal_error( "uncaught option" ); } } if( argind + 1 != parser.arguments() ) { show_error( "you must specify exactly 1 file", 0, true ); return 1; } uint8_t * base_buffer; const int retval = process_file( parser.argument( argind ), base_buffer ); delete[] base_buffer; return retval; }