/* Tests libcfile_file_seek_offset * Returns 1 if successful, 0 if not or -1 on error */ int cfile_file_test_seek_offset( libcfile_file_t *file, off64_t input_offset, int input_whence, off64_t expected_offset ) { libcerror_error_t *error = NULL; off64_t result_offset = 0; int result = 0; if( file == NULL ) { return( -1 ); } result_offset = libcfile_file_seek_offset( file, input_offset, input_whence, &error ); if( expected_offset != result_offset ) { fprintf( stderr, "Unexpected result offset: %" PRIi64 "\n", result_offset ); } else { result = 1; } if( error != NULL ) { if( result != 1 ) { libcerror_error_backtrace_fprint( error, stderr ); } libcerror_error_free( &error ); } return( result ); }
int main( int argc, char * const argv[] ) #endif { char destination[ 128 ]; libcerror_error_t *error = NULL; libcfile_file_t *source_file = NULL; uint8_t *buffer = NULL; uint8_t *uncompressed_data = NULL; char *program = "ascii7decompress"; libcstring_system_integer_t option = 0; char *source = NULL; size64_t source_size = 0; off_t source_offset = 0; size_t buffer_size = 0; size_t uncompressed_data_size = 0; ssize_t read_count = 0; int print_count = 0; int verbose = 0; assorted_output_version_fprint( stdout, program ); while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "ho:s:vV" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case 'o': source_offset = atol( optarg ); break; case 's': source_size = atol( optarg ); break; case 'v': verbose = 1; break; case 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } if( libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ) != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); goto on_error; } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); goto on_error; } /* Create the input buffer */ buffer_size = source_size; buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); return( EXIT_FAILURE ); } uncompressed_data_size = 1 + ( ( ( source_size - 1 ) * 8 ) / 7 ); uncompressed_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * uncompressed_data_size ); if( uncompressed_data == NULL ) { fprintf( stderr, "Unable to create uncompressed data buffer.\n" ); return( EXIT_FAILURE ); } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } print_count = libcstring_narrow_string_snprintf( destination, 128, "%s.ascii7decompressed", source ); if( ( print_count < 0 ) || ( print_count > 128 ) ) { fprintf( stderr, "Unable to set destination filename.\n" ); memory_free( uncompressed_data ); memory_free( buffer ); return( EXIT_FAILURE ); } fprintf( stdout, "Starting 7-bit ASCII decompression of: %s at offset: %jd (0x%08jx).\n", source, source_offset, source_offset ); read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Decompress the data */ fprintf( stderr, "Compressed data:\n" ); libcnotify_print_data( buffer, source_size, 0 ); if( ascii7_decompress( uncompressed_data, uncompressed_data_size, buffer, source_size, NULL ) != 1 ) { fprintf( stderr, "Unable to decompress data.\n" ); goto on_error; } fprintf( stderr, "Uncompressed data:\n" ); libcnotify_print_data( uncompressed_data, uncompressed_data_size, 0 ); source_offset += source_size; source_size -= source_size; #ifdef NOWRITE libcfile_file_t *destination_file = NULL; ssize_t write_count = 0; if( destination_file == NULL ) { /* Open the destination file */ if( libcfile_file_initialize( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to create destination file.\n" ); goto on_error; } if( libcfile_file_open( destination_file, destination, LIBCFILE_OPEN_WRITE, &error ) != 1 ) { fprintf( stderr, "Unable to open destination file.\n" ); goto on_error; } } write_count = libcfile_file_write_buffer( destination_file, uncompressed_data, uncompressed_data_size, &error ); if( write_count != (ssize_t) uncompressed_data_size ) { fprintf( stderr, "Unable to write to destination file.\n" ); goto on_error; } /* Clean up */ if( destination_file != NULL ) { if( libcfile_file_close( destination_file, &error ) != 0 ) { fprintf( stderr, "Unable to close destination file.\n" ); goto on_error; } if( libcfile_file_free( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to free destination file.\n" ); goto on_error; } } #endif /* Clean up */ if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } fprintf( stdout, "7-bit ASCII decompression:\tSUCCESS\n" ); memory_free( uncompressed_data ); memory_free( buffer ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } #ifdef NOWRITE if( destination_file != NULL ) { libcfile_file_free( &destination_file, NULL ); } #endif if( uncompressed_data != NULL ) { memory_free( uncompressed_data ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libcfile_file_t *destination_file = NULL; libcfile_file_t *source_file = NULL; libfcrypto_rc4_context_t *context = NULL; system_character_t *option_keys = NULL; system_character_t *option_target_path = NULL; system_character_t *source = NULL; uint8_t *buffer = NULL; uint8_t *decrypted_data = NULL; uint8_t *key_data = NULL; char *program = "rc4crypt"; system_integer_t option = 0; size64_t source_size = 0; size_t buffer_size = 0; size_t decrypted_data_size = 0; size_t key_data_size = 0; ssize_t read_count = 0; ssize_t write_count = 0; off_t source_offset = 0; int verbose = 0; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); assorted_output_version_fprint( stdout, program ); while( ( option = assorted_getopt( argc, argv, _SYSTEM_STRING( "hk:o:s:t:vV" ) ) ) != (system_integer_t) -1 ) { switch( option ) { case (system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case (system_integer_t) 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case (system_integer_t) 'k': option_keys = optarg; break; case (system_integer_t) 'o': source_offset = atol( optarg ); break; case (system_integer_t) 's': source_size = atol( optarg ); break; case (system_integer_t) 't': option_target_path = optarg; break; case (system_integer_t) 'v': verbose = 1; break; case (system_integer_t) 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; if( option_keys == NULL ) { fprintf( stderr, "Missing key.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } if( libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ) != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); return( EXIT_FAILURE ); } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); return( EXIT_FAILURE ); } /* Create the input buffer */ buffer_size = source_size; buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); return( EXIT_FAILURE ); } decrypted_data_size = source_size; decrypted_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * decrypted_data_size ); if( decrypted_data == NULL ) { fprintf( stderr, "Unable to create decrypted data buffer.\n" ); goto on_error; } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } if( libfcrypto_rc4_context_initialize( &context, &error ) != 1 ) { fprintf( stderr, "Unable to create RC4 context.\n" ); goto on_error; } fprintf( stdout, "Starting RC4 decrypting data of: %" PRIs_SYSTEM " at offset: %" PRIjd " (0x%08" PRIjx ").\n", source, source_offset, source_offset ); read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Decrypts the data */ if( rc4crypt_set_keys( option_keys, &key_data, &key_data_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve key data from argument.\n" ); goto on_error; } if( libfcrypto_rc4_context_set_key( context, key_data, key_data_size * 8, &error ) != 1 ) { fprintf( stderr, "Unable to set key in context.\n" ); goto on_error; } if( memory_set( key_data, 0, key_data_size ) == NULL ) { fprintf( stderr, "Unable to clear key data.\n" ); goto on_error; } memory_free( key_data ); key_data = NULL; if( option_target_path == NULL ) { fprintf( stderr, "Encrypted data:\n" ); libcnotify_print_data( buffer, source_size, 0 ); } if( libfcrypto_rc4_crypt( context, buffer, source_size, decrypted_data, decrypted_data_size, &error ) != 1 ) { fprintf( stderr, "Unable to decode data.\n" ); goto on_error; } if( option_target_path == NULL ) { fprintf( stderr, "Decrypted data:\n" ); libcnotify_print_data( decrypted_data, decrypted_data_size, 0 ); } else { if( libcfile_file_initialize( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to create destination file.\n" ); goto on_error; } if( libcfile_file_open( destination_file, option_target_path, LIBCFILE_OPEN_WRITE, &error ) != 1 ) { fprintf( stderr, "Unable to open destination file.\n" ); goto on_error; } write_count = libcfile_file_write_buffer( destination_file, decrypted_data, decrypted_data_size, &error ); if( write_count != (ssize_t) decrypted_data_size ) { fprintf( stderr, "Unable to write to destination file.\n" ); goto on_error; } if( libcfile_file_close( destination_file, &error ) != 0 ) { fprintf( stderr, "Unable to close destination file.\n" ); goto on_error; } if( libcfile_file_free( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to free destination file.\n" ); goto on_error; } } /* Clean up */ if( libfcrypto_rc4_context_free( &context, &error ) != 1 ) { fprintf( stderr, "Unable to free RC4 context.\n" ); goto on_error; } if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } if( decrypted_data != NULL ) { memory_free( decrypted_data ); } if( buffer != NULL ) { memory_free( buffer ); } fprintf( stdout, "RC4 decryption:\tSUCCESS\n" ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( destination_file != NULL ) { libcfile_file_free( &destination_file, NULL ); } if( decrypted_data != NULL ) { memory_free( decrypted_data ); } if( key_data != NULL ) { memory_set( key_data, 0, key_data_size ); memory_free( key_data ); } if( context != NULL ) { libfcrypto_rc4_context_free( &context, NULL ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } fprintf( stdout, "RC4 decryption:\tFAILURE\n" ); return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libcfile_file_t *destination_file = NULL; libcfile_file_t *source_file = NULL; system_character_t *option_target_path = NULL; system_character_t *options_string = NULL; system_character_t *source = NULL; uint8_t *buffer = NULL; uint8_t *uncompressed_data = NULL; char *program = "lzxpressdecompress"; system_integer_t option = 0; size64_t source_size = 0; size_t buffer_size = 0; size_t uncompressed_data_size = 0; ssize_t read_count = 0; ssize_t write_count = 0; off_t source_offset = 0; int decompression_method = 1; int result = 0; int verbose = 0; #if defined( WINAPI ) void *workspace = NULL; unsigned short winapi_compression_method = 0; #endif assorted_output_version_fprint( stdout, program ); #if defined( WINAPI ) options_string = _SYSTEM_STRING( "d:ho:s:t:vV1234" ); #else options_string = _SYSTEM_STRING( "d:ho:s:t:vV12" ); #endif while( ( option = assorted_getopt( argc, argv, options_string ) ) != (system_integer_t) -1 ) { switch( option ) { case (system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case (system_integer_t) '1': decompression_method = 1; break; case (system_integer_t) '2': decompression_method = 2; break; #if defined( WINAPI ) case (system_integer_t) '3': decompression_method = 3; break; case (system_integer_t) '4': decompression_method = 4; break; #endif case (system_integer_t) 'd': #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) uncompressed_data_size = _wtol( optarg ); #else uncompressed_data_size = atol( optarg ); #endif break; case (system_integer_t) 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case (system_integer_t) 'o': #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) source_offset = _wtol( optarg ); #else source_offset = atol( optarg ); #endif break; case (system_integer_t) 's': #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) source_size = _wtol( optarg ); #else source_size = atol( optarg ); #endif break; case (system_integer_t) 't': option_target_path = optarg; break; case (system_integer_t) 'v': verbose = 1; break; case (system_integer_t) 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_open_wide( source_file, source, LIBCFILE_OPEN_READ, &error ); #else result = libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ); #endif if( result != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } if( source_size <= (size64_t) source_offset ) { fprintf( stderr, "Invalid source size value is less equal than source offset.\n" ); goto on_error; } source_size -= source_offset; } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); goto on_error; } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); goto on_error; } /* Create the input buffer */ buffer_size = source_size; buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * buffer_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); goto on_error; } if( uncompressed_data_size == 0 ) { uncompressed_data_size = 65536; } uncompressed_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * uncompressed_data_size ); if( uncompressed_data == NULL ) { fprintf( stderr, "Unable to create uncompressed data buffer.\n" ); goto on_error; } if( memory_set( uncompressed_data, 0, uncompressed_data_size ) == NULL ) { fprintf( stderr, "Unable to clear uncompressed data buffer.\n" ); goto on_error; } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } fprintf( stdout, "Starting LZXPRESS decompression of: %" PRIs_SYSTEM " at offset: %" PRIjd " (0x%08" PRIjx ").\n", source, source_offset, source_offset ); read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Decompress the data */ if( option_target_path == NULL ) { fprintf( stderr, "Compressed data:\n" ); libcnotify_print_data( buffer, source_size, LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); } if( decompression_method == 1 ) { result = libfwnt_lzxpress_decompress( buffer, (size_t) source_size, uncompressed_data, &uncompressed_data_size, &error ); } else if( decompression_method == 2 ) { result = libfwnt_lzxpress_huffman_decompress( buffer, (size_t) source_size, uncompressed_data, &uncompressed_data_size, &error ); } #if defined( WINAPI ) else if( ( decompression_method == 3 ) || ( decompression_method == 4 ) ) { if( decompression_method == 3 ) { winapi_compression_method = COMPRESSION_FORMAT_XPRESS; } else if( decompression_method == 4 ) { winapi_compression_method = COMPRESSION_FORMAT_XPRESS_HUFF; } /* TODO: determine workspace size: RtlGetCompressionWorkSpaceSize */ workspace = (void *) memory_allocate( 16 * 1024 * 1024 ); if( workspace == NULL ) { fprintf( stderr, "Unable to create workspace.\n" ); goto on_error; } result = lzxpresscompress_RtlDecompressBufferEx( winapi_compression_method, (unsigned char *) uncompressed_data, (unsigned long) uncompressed_data_size, (unsigned char *) buffer, (unsigned long) source_size, (unsigned long *) &uncompressed_data_size, workspace ); memory_free( workspace ); if( result != 0 ) { result = -1; } else { result = 1; } } #endif if( result == -1 ) { fprintf( stderr, "Unable to decompress data.\n" ); libcnotify_print_data( uncompressed_data, uncompressed_data_size, LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); goto on_error; } if( option_target_path == NULL ) { fprintf( stderr, "Uncompressed data:\n" ); libcnotify_print_data( uncompressed_data, uncompressed_data_size, LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA ); } else { if( libcfile_file_initialize( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to create destination file.\n" ); goto on_error; } #if defined( HAVE_WIDE_SYSTEM_CHARACTER ) result = libcfile_file_open_wide( destination_file, option_target_path, LIBCFILE_OPEN_WRITE, &error ); #else result = libcfile_file_open( destination_file, option_target_path, LIBCFILE_OPEN_WRITE, &error ); #endif if( result != 1 ) { fprintf( stderr, "Unable to open destination file.\n" ); goto on_error; } write_count = libcfile_file_write_buffer( destination_file, uncompressed_data, uncompressed_data_size, &error ); if( write_count != (ssize_t) uncompressed_data_size ) { fprintf( stderr, "Unable to write to destination file.\n" ); goto on_error; } if( libcfile_file_close( destination_file, &error ) != 0 ) { fprintf( stderr, "Unable to close destination file.\n" ); goto on_error; } if( libcfile_file_free( &destination_file, &error ) != 1 ) { fprintf( stderr, "Unable to free destination file.\n" ); goto on_error; } } /* Clean up */ if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } memory_free( uncompressed_data ); uncompressed_data = NULL; memory_free( buffer ); buffer = NULL; fprintf( stdout, "LZXPRESS decompression:\tSUCCESS\n" ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( destination_file != NULL ) { libcfile_file_free( &destination_file, NULL ); } if( uncompressed_data != NULL ) { memory_free( uncompressed_data ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } fprintf( stdout, "LZXPRESS decompression:\tFAILURE\n" ); return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libcfile_file_t *source_file = NULL; uint8_t *buffer = NULL; char *program = "crc32sum"; char *source = NULL; libcstring_system_integer_t option = 0; size64_t source_size = 0; off_t source_offset = 0; ssize_t read_count = 0; uint32_t calculated_crc32 = 0; uint32_t crc32 = 0; uint32_t initial_value = 0; uint32_t polynomial = 0xedb88320UL; uint8_t bit_index = 0; uint8_t weak_crc = 0; int calculation_method = 2; int result = 0; int validate_crc = 0; int verbose = 0; assorted_output_version_fprint( stdout, program ); while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "12c:hi:o:p:s:vVw" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM "\n", argv[ optind ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case '1': calculation_method = 1; break; case '2': calculation_method = 2; break; case 'c': crc32 = atol( optarg ); validate_crc = 1; break; case 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case 'i': initial_value = atol( optarg ); break; case 'o': source_offset = atol( optarg ); break; case 'p': polynomial = atol( optarg ); break; case 's': source_size = atol( optarg ); break; case 'v': verbose = 1; break; case 'V': assorted_output_copyright_fprint( stdout ); return( EXIT_SUCCESS ); case 'w': weak_crc = 1; break; } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( verbose ); /* Open the source file */ if( libcfile_file_initialize( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to create source file.\n" ); goto on_error; } if( libcfile_file_open( source_file, source, LIBCFILE_OPEN_READ, &error ) != 1 ) { fprintf( stderr, "Unable to open source file.\n" ); goto on_error; } if( source_size == 0 ) { if( libcfile_file_get_size( source_file, &source_size, &error ) == -1 ) { fprintf( stderr, "Unable to determine size of source file.\n" ); goto on_error; } } if( source_size == 0 ) { fprintf( stderr, "Invalid source size value is zero.\n" ); goto on_error; } if( source_size > (size_t) SSIZE_MAX ) { fprintf( stderr, "Invalid source size value exceeds maximum.\n" ); goto on_error; } buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * source_size ); if( buffer == NULL ) { fprintf( stderr, "Unable to create buffer.\n" ); goto on_error; } /* Position the source file at the right offset */ if( libcfile_file_seek_offset( source_file, source_offset, SEEK_SET, &error ) == -1 ) { fprintf( stderr, "Unable to seek offset in source file.\n" ); goto on_error; } read_count = libcfile_file_read_buffer( source_file, buffer, source_size, &error ); if( read_count != (ssize_t) source_size ) { fprintf( stderr, "Unable to read from source file.\n" ); goto on_error; } /* Clean up */ if( libcfile_file_close( source_file, &error ) != 0 ) { fprintf( stderr, "Unable to close source file.\n" ); goto on_error; } if( libcfile_file_free( &source_file, &error ) != 1 ) { fprintf( stderr, "Unable to free source file.\n" ); goto on_error; } if( calculation_method == 1 ) { result = crc32_calculate_modulo2( &calculated_crc32, buffer, source_size, initial_value, weak_crc, &error ); } else if( calculation_method == 2 ) { initialize_crc32_table( polynomial ); result = crc32_calculate( &calculated_crc32, buffer, source_size, initial_value, weak_crc, &error ); } if( result != 1 ) { fprintf( stderr, "Unable to calculate CRC-32.\n" ); goto on_error; } if( libcnotify_verbose != 0 ) { libcnotify_print_data( buffer, source_size, 0 ); } fprintf( stdout, "Calculated CRC-32: %" PRIu32 " (0x%08" PRIx32 ")\n", calculated_crc32, calculated_crc32 ); if( validate_crc != 0 ) { if( calculated_crc32 != crc32 ) { fprintf( stdout, "Mismatch between CRC-32: %" PRIu32 " and calculated CRC-32: %" PRIu32 "\n", crc32, calculated_crc32 ); result = crc32_validate( crc32, calculated_crc32, &bit_index, &error ); if( result == -1 ) { fprintf( stderr, "Unable to locate error.\n" ); goto on_error; } else if( result != 0 ) { fprintf( stdout, "Single bit-error in bit: %" PRIu8 " of CRC-32\n", bit_index ); } result = crc32_locate_error_offset( crc32, calculated_crc32, buffer, source_size, initial_value, &error ); if( result == -1 ) { fprintf( stderr, "Unable to locate error.\n" ); goto on_error; } } else { fprintf( stdout, "Match between CRC-32: %" PRIu32 " and calculated CRC-32: %" PRIu32 "\n", crc32, calculated_crc32 ); } } memory_free( buffer ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( buffer != NULL ) { memory_free( buffer ); } if( source_file != NULL ) { libcfile_file_free( &source_file, NULL ); } return( EXIT_FAILURE ); }
/* Tests libcfile_file_seek_offset * Returns 1 if successful, 0 if not or -1 on error */ int cfile_test_seek_offset( libcfile_file_t *file, off64_t input_offset, int input_whence, off64_t output_offset ) { libcerror_error_t *error = NULL; const char *whence_string = NULL; off64_t result_offset = 0; int result = 0; if( file == NULL ) { return( -1 ); } if( input_whence == SEEK_CUR ) { whence_string = "SEEK_CUR"; } else if( input_whence == SEEK_END ) { whence_string = "SEEK_END"; } else if( input_whence == SEEK_SET ) { whence_string = "SEEK_SET"; } else { whence_string = "UNKNOWN"; } fprintf( stdout, "Testing seek of offset: %" PRIi64 " and whence: %s\t", input_offset, whence_string ); result_offset = libcfile_file_seek_offset( file, input_offset, input_whence, &error ); if( result_offset == output_offset ) { result = 1; } if( result != 0 ) { fprintf( stdout, "(PASS)" ); } else { fprintf( stdout, "(FAIL)" ); } fprintf( stdout, "\n" ); if( error != NULL ) { if( result != 1 ) { libcerror_error_backtrace_fprint( error, stderr ); } libcerror_error_free( &error ); } return( result ); }