diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-02-21 16:13:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-02-21 16:13:20 +0000 |
commit | 83ee6131390406477f652741c1ebb25cc17e3181 (patch) | |
tree | 553f2f499192b3fa71f01f814120799aeab8a242 /lunzcrash.cc | |
parent | Adding upstream version 1.22. (diff) | |
download | lziprecover-83ee6131390406477f652741c1ebb25cc17e3181.tar.xz lziprecover-83ee6131390406477f652741c1ebb25cc17e3181.zip |
Adding upstream version 1.23.upstream/1.23
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lunzcrash.cc')
-rw-r--r-- | lunzcrash.cc | 164 |
1 files changed, 142 insertions, 22 deletions
diff --git a/lunzcrash.cc b/lunzcrash.cc index b07b748..577d355 100644 --- a/lunzcrash.cc +++ b/lunzcrash.cc @@ -1,5 +1,5 @@ /* Lziprecover - Data recovery tool for the lzip format - Copyright (C) 2009-2021 Antonio Diaz Diaz. + Copyright (C) 2009-2022 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 @@ -69,24 +69,26 @@ bool compare_member( const uint8_t * const mbuffer, const long long msize, } -int test_member_rest( const LZ_mtester & master, long * const failure_posp, +int test_member_rest( const LZ_mtester & master, uint8_t * const buffer2, + long * const failure_posp, const unsigned long long byte_pos ) { - LZ_mtester mtester( master ); - mtester.duplicate_buffer(); + LZ_mtester mtester( master ); // tester with external buffer + mtester.duplicate_buffer( buffer2 ); int result = mtester.test_member( LLONG_MAX, LLONG_MAX, stdout, byte_pos ); - if( result == 0 && !mtester.finished() ) result = -1; + if( result == 0 && !mtester.finished() ) result = -1; // false negative if( result != 0 ) *failure_posp = mtester.member_position(); return result; } -long next_pct_pos( const Lzip_index & lzip_index, const int i, const int pct ) +long next_pct_pos( const Lzip_index & lzip_index, const int i, const int pct, + const int sector_size = 0 ) { if( pct <= 0 ) return 0; - const long long cdata_size = lzip_index.cdata_size(); + const long long cdata_size = lzip_index.cdata_size() - sector_size; const long long mpos = lzip_index.mblock( i ).pos(); - const long long msize = lzip_index.mblock( i ).size(); + const long long msize = lzip_index.mblock( i ).size() - sector_size; long long pct_pos = (long long)( cdata_size / ( 100.0 / pct ) ); if( pct_pos <= mpos ) pct_pos = 0; @@ -101,18 +103,17 @@ long next_pct_pos( const Lzip_index & lzip_index, const int i, const int pct ) /* Test 1-bit errors in LZMA streams in file. Unless verbosity >= 1, print only the bytes with interesting results. */ -int lunzcrash( const std::string & input_filename ) +int lunzcrash_bit( const char * const input_filename ) { struct stat in_stats; // not used - const int infd = - open_instream( input_filename.c_str(), &in_stats, false, true ); + const int infd = open_instream( input_filename, &in_stats, false, true ); if( infd < 0 ) return 1; const Lzip_index lzip_index( infd, true, true ); if( lzip_index.retval() != 0 ) - { show_file_error( input_filename.c_str(), lzip_index.error().c_str() ); + { show_file_error( input_filename, lzip_index.error().c_str() ); return lzip_index.retval(); } - if( verbosity >= 2 ) printf( "Testing file '%s'\n", input_filename.c_str() ); + if( verbosity >= 2 ) printf( "Testing file '%s'\n", input_filename ); const long long cdata_size = lzip_index.cdata_size(); long positions = 0, decompressions = 0, successes = 0, failed_comparisons = 0; @@ -125,14 +126,15 @@ int lunzcrash( const std::string & input_filename ) uint8_t * const mbuffer = read_member( infd, mpos, msize ); if( !mbuffer ) return 1; uint8_t md5_orig[16]; - if( !verify_member( mbuffer, msize, dictionary_size, - input_filename.c_str(), md5_orig ) ) return 2; + if( !verify_member( mbuffer, msize, dictionary_size, input_filename, + md5_orig ) ) return 2; long pct_pos = next_pct_pos( lzip_index, i, pct ); long pos = Lzip_header::size + 1, printed = 0; // last pos printed const long end = msize - 20; if( verbosity == 0 ) // give a clue of the range being tested std::printf( "Testing bytes %llu to %llu\n", mpos + pos, mpos + end - 1 ); LZ_mtester master( mbuffer, msize, dictionary_size ); + uint8_t * const buffer2 = new uint8_t[dictionary_size]; for( ; pos < end; ++pos ) { const long pos_limit = pos - 16; @@ -150,17 +152,20 @@ int lunzcrash( const std::string & input_filename ) ++decompressions; mbuffer[pos] ^= mask; long failure_pos = 0; - const int result = test_member_rest( master, &failure_pos, + const int result = test_member_rest( master, buffer2, &failure_pos, ( printed < pos ) ? mpos + pos : 0 ); - if( result == 0 ) + if( result <= 0 ) { ++successes; if( verbosity >= 0 ) { if( printed < pos ) { std::printf( "byte %llu\n", mpos + pos ); printed = pos; } - std::printf( "0x%02X (0x%02X^0x%02X) passed the test\n", - mbuffer[pos], byte, mask ); + std::printf( "0x%02X (0x%02X^0x%02X) passed the test%s", + mbuffer[pos], byte, mask, ( result < 0 ) ? "" : "\n" ); + if( result < 0 ) + std::printf( ", but only consumed %lu bytes of %llu\n", + failure_pos, msize ); } if( !compare_member( mbuffer, msize, dictionary_size, mpos + pos, md5_orig ) ) ++failed_comparisons; @@ -191,18 +196,133 @@ int lunzcrash( const std::string & input_filename ) mbuffer[pos] ^= mask; } } + delete[] buffer2; + if( !compare_member( mbuffer, msize, dictionary_size, mpos + pos, md5_orig ) ) + internal_error( "Some byte was not properly restored." ); delete[] mbuffer; } if( verbosity >= 0 ) { - std::printf( "\n%8ld bytes tested\n%8ld total decompressions" - "\n%8ld decompressions returned with zero status", + std::printf( "\n%9ld bytes tested\n%9ld total decompressions" + "\n%9ld decompressions returned with zero status", positions, decompressions, successes ); if( successes > 0 ) { if( failed_comparisons > 0 ) - std::printf( ", of which\n%8ld comparisons failed\n", + std::printf( ", of which\n%9ld comparisons failed\n", + failed_comparisons ); + else std::fputs( "\n all comparisons passed\n", stdout ); + } + else std::fputc( '\n', stdout ); + } + return 0; + } + + +/* Test zeroed blocks of given size in LZMA streams in file. + Unless verbosity >= 1, print only the bytes with interesting results. */ +int lunzcrash_block( const char * const input_filename, const int sector_size ) + { + struct stat in_stats; // not used + const int infd = open_instream( input_filename, &in_stats, false, true ); + if( infd < 0 ) return 1; + + const Lzip_index lzip_index( infd, true, true ); + if( lzip_index.retval() != 0 ) + { show_file_error( input_filename, lzip_index.error().c_str() ); + return lzip_index.retval(); } + if( verbosity >= 2 ) printf( "Testing file '%s'\n", input_filename ); + + const long long cdata_size = lzip_index.cdata_size(); + long decompressions = 0, successes = 0, failed_comparisons = 0; + int pct = ( cdata_size >= 1000 && isatty( STDERR_FILENO ) ) ? 0 : 100; + uint8_t * const block = new uint8_t[sector_size]; + for( long i = 0; i < lzip_index.members(); ++i ) + { + const long long mpos = lzip_index.mblock( i ).pos(); + const long long msize = lzip_index.mblock( i ).size(); + long pos = Lzip_header::size + 1; + const long end = msize - sector_size - 20; + if( end <= pos ) continue; // sector_size larger than LZMA stream + const unsigned dictionary_size = lzip_index.dictionary_size( i ); + uint8_t * const mbuffer = read_member( infd, mpos, msize ); + if( !mbuffer ) return 1; + uint8_t md5_orig[16]; + if( !verify_member( mbuffer, msize, dictionary_size, input_filename, + md5_orig ) ) return 2; + long pct_pos = next_pct_pos( lzip_index, i, pct, sector_size ); + if( verbosity >= 0 ) // give a clue of the range being tested + std::printf( "Testing blocks of size %u from pos %llu to %llu\n", + sector_size, mpos + pos, mpos + end - 1 ); + LZ_mtester master( mbuffer, msize, dictionary_size ); + uint8_t * const buffer2 = new uint8_t[dictionary_size]; + for( ; pos < end; ++pos ) + { + const long pos_limit = pos - 16; + if( pos_limit > 0 && master.test_member( pos_limit ) != -1 ) + { show_error( "Can't advance master." ); return 1; } + if( verbosity >= 0 && pos >= pct_pos ) + { std::fprintf( stderr, "\r%3u%% done\r", pct ); ++pct; + pct_pos = next_pct_pos( lzip_index, i, pct, sector_size ); } + std::memcpy( block, mbuffer + pos, sector_size ); // save block + std::memset( mbuffer + pos, 0, sector_size ); + ++decompressions; + long failure_pos = 0; + const int result = + test_member_rest( master, buffer2, &failure_pos, mpos + pos ); + if( result <= 0 ) + { + ++successes; + if( verbosity >= 0 ) + { + std::printf( "block %llu,%u passed the test%s", + mpos + pos, sector_size, ( result < 0 ) ? "" : "\n" ); + if( result < 0 ) + std::printf( ", but only consumed %lu bytes of %llu\n", + failure_pos, msize ); + } + if( !compare_member( mbuffer, msize, dictionary_size, mpos + pos, + md5_orig ) ) ++failed_comparisons; + } + else if( result == 1 ) + { + if( verbosity >= 3 || + ( verbosity >= 2 && failure_pos - pos >= sector_size ) || + ( verbosity >= 1 && failure_pos - pos >= 10000 ) || + ( verbosity >= 0 && failure_pos - pos >= 50000 ) ) + std::printf( "block %llu,%u\nDecoder error at pos %llu\n", + mpos + pos, sector_size, mpos + failure_pos ); + } + else if( result == 3 || result == 4 ) // test_member printed the error + {} + else if( verbosity >= 0 ) + { + std::printf( "block %llu,%u\n", mpos + pos, sector_size ); + if( result == 2 ) + std::printf( "File ends unexpectedly at pos %llu\n", + mpos + failure_pos ); + else + std::printf( "Unknown error code '%d'\n", result ); + } + std::memcpy( mbuffer + pos, block, sector_size ); // restore block + } + delete[] buffer2; + if( !compare_member( mbuffer, msize, dictionary_size, mpos + pos, md5_orig ) ) + internal_error( "Block was not properly restored." ); + delete[] mbuffer; + } + delete[] block; + + if( verbosity >= 0 ) + { + std::printf( "\n%9ld blocks tested\n%9ld total decompressions" + "\n%9ld decompressions returned with zero status", + decompressions, decompressions, successes ); + if( successes > 0 ) + { + if( failed_comparisons > 0 ) + std::printf( ", of which\n%9ld comparisons failed\n", failed_comparisons ); else std::fputs( "\n all comparisons passed\n", stdout ); } |