int main( int argc, char * const argv[] ) #endif { #if !defined( HAVE_GLOB_H ) libcsystem_glob_t *glob = NULL; #endif libcerror_error_t *error = NULL; libcstring_system_character_t * const *source_filenames = NULL; libcstring_system_character_t **ewf_filenames = NULL; libcstring_system_character_t *option_header_codepage = NULL; libcstring_system_character_t *program = _LIBCSTRING_SYSTEM_STRING( "ewfdebug" ); libcstring_system_integer_t option = 0; size_t first_filename_length = 0; uint8_t verbose = 0; int number_of_filenames = 0; int header_codepage = LIBEWF_CODEPAGE_ASCII; int result = 0; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); if( libclocale_initialize( "ewftools", &error ) != 1 ) { fprintf( stderr, "Unable to initialize locale values.\n" ); goto on_error; } if( libcsystem_initialize( _IONBF, &error ) != 1 ) { ewfoutput_version_fprint( stdout, program ); fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "A:hqvV" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: ewfoutput_version_fprint( stdout, program ); fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n", argv[ optind - 1 ] ); usage_fprint( stdout ); return( EXIT_FAILURE ); case (libcstring_system_integer_t) 'A': option_header_codepage = optarg; break; case (libcstring_system_integer_t) 'h': ewfoutput_version_fprint( stdout, program ); usage_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'q': break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': ewfoutput_version_fprint( stdout, program ); ewfoutput_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { ewfoutput_version_fprint( stdout, program ); fprintf( stderr, "Missing EWF image file(s).\n" ); usage_fprint( stdout ); goto on_error; } ewfoutput_version_fprint( stdout, program ); libcnotify_verbose_set( verbose ); #if !defined( HAVE_LOCAL_LIBEWF ) libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); #endif if( option_header_codepage != NULL ) { if( ewfinput_determine_header_codepage( option_header_codepage, &header_codepage, &error ) != 1 ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); fprintf( stderr, "Unsupported header codepage defaulting to: ascii.\n" ); header_codepage = LIBEWF_CODEPAGE_ASCII; } } if( libcsystem_signal_attach( ewfdebug_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } #if !defined( HAVE_GLOB_H ) if( libcsystem_glob_initialize( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to initialize glob.\n" ); goto on_error; } if( libcsystem_glob_resolve( glob, &( argv[ optind ] ), argc - optind, &error ) != 1 ) { fprintf( stderr, "Unable to resolve glob.\n" ); goto on_error; } if( libcsystem_glob_get_results( glob, &number_of_filenames, (libcstring_system_character_t ***) &source_filenames, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve glob results.\n" ); goto on_error; } #else source_filenames = &( argv[ optind ] ); number_of_filenames = argc - optind; #endif if( number_of_filenames == 1 ) { first_filename_length = libcstring_system_string_length( source_filenames[ 0 ] ); #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_glob_wide( source_filenames[ 0 ], first_filename_length, LIBEWF_FORMAT_UNKNOWN, &ewf_filenames, &number_of_filenames, &error ) != 1 ) #else if( libewf_glob( source_filenames[ 0 ], first_filename_length, LIBEWF_FORMAT_UNKNOWN, &ewf_filenames, &number_of_filenames, &error ) != 1 ) #endif { fprintf( stderr, "Unable to resolve ewf file(s).\n" ); goto on_error; } source_filenames = (libcstring_system_character_t * const *) ewf_filenames; } if( libewf_handle_initialize( &ewfdebug_input_handle, &error ) != 1 ) { fprintf( stderr, "Unable to initialize input handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) result = libewf_handle_open_wide( ewfdebug_input_handle, source_filenames, number_of_filenames, LIBEWF_OPEN_READ, &error ); #else result = libewf_handle_open( ewfdebug_input_handle, source_filenames, number_of_filenames, LIBEWF_OPEN_READ, &error ); #endif #if !defined( HAVE_GLOB_H ) if( libcsystem_glob_free( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } #endif if( ewf_filenames != NULL ) { for( ; number_of_filenames > 0; number_of_filenames-- ) { memory_free( ewf_filenames[ number_of_filenames - 1 ] ); } memory_free( ewf_filenames ); } if( ( ewfdebug_abort == 0 ) && ( result != 1 ) ) { fprintf( stderr, "Unable to open EWF file(s).\n" ); libewf_handle_close( ewfdebug_input_handle, NULL ); libewf_handle_free( &ewfdebug_input_handle, NULL ); return( EXIT_FAILURE ); } /* TODO */ if( libewf_handle_close( ewfdebug_input_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close EWF file(s).\n" ); libewf_handle_free( &ewfdebug_input_handle, NULL ); return( EXIT_FAILURE ); } if( libewf_handle_free( &ewfdebug_input_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free input handle.\n" ); return( EXIT_FAILURE ); } if( libcsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( ewfdebug_abort != 0 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n", program ); return( EXIT_FAILURE ); } fprintf( stdout, "Debug completed.\n" ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } #if !defined( HAVE_GLOB_H ) if( glob != NULL ) { libcsystem_glob_free( &glob, NULL ); } #endif return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libewf_handle_t *handle = NULL; off64_t read_offset = 0; size64_t media_size = 0; size64_t read_size = 0; size32_t chunk_size = 0; if( argc < 2 ) { fprintf( stderr, "Missing filename(s).\n" ); return( EXIT_FAILURE ); } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_READ_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif /* Initialization */ if( libewf_handle_initialize( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, &( argv[ 1 ] ), argc - 1, LIBEWF_OPEN_READ, &error ) != 1 ) #else if( libewf_handle_open( handle, &( argv[ 1 ] ), argc - 1, LIBEWF_OPEN_READ, &error ) != 1 ) #endif { fprintf( stderr, "Unable to open file(s).\n" ); goto on_error; } if( libewf_handle_get_media_size( handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } if( media_size > (size64_t) INT64_MAX ) { fprintf( stderr, "Media size exceeds maximum.\n" ); goto on_error; } if( libewf_handle_get_chunk_size( handle, &chunk_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve chunk size.\n" ); goto on_error; } if( chunk_size == 0 ) { fprintf( stderr, "Invalid chunk size.\n" ); goto on_error; } fprintf( stdout, "Media size: %" PRIu64 " bytes\n", media_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <media_size / 7> size: <media_size / 2> * Expected result: offset: <media_size / 7> size: <media_size / 2> */ read_offset = (off64_t) ( media_size / 7 ); read_size = media_size / 2; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } /* Case 2: test read buffer beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: <media_size - 1024> size: 1024 */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } if( ewf_test_read_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read buffer.\n" ); goto on_error; } } fprintf( stdout, "\nChunk size: %" PRIu32 " bytes\n", chunk_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> * Expected result: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> */ read_offset = (off64_t) ( ( media_size / 7 ) / chunk_size ) * chunk_size; read_size = ( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size; if( media_size == 0 ) { if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } else { if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } /* Case 2: test read chunk beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: <media_size - 1024> size: chunk size or media_size % chunk_size */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } if( ewf_test_read_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read chunk.\n" ); goto on_error; } } /* Clean up */ if( libewf_handle_close( handle, &error ) != 0 ) { fprintf( stderr, "Unable to close file(s).\n" ); goto on_error; } if( libewf_handle_free( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t acquiry_operating_system[ 32 ]; libcstring_system_character_t input_buffer[ EWFEXPORT_INPUT_BUFFER_SIZE ]; libcstring_system_character_t * const *argv_filenames = NULL; liberror_error_t *error = NULL; #if !defined( LIBSYSTEM_HAVE_GLOB ) libsystem_glob_t *glob = NULL; #endif libcstring_system_character_t *acquiry_software_version = NULL; libcstring_system_character_t *log_filename = NULL; libcstring_system_character_t *option_additional_digest_types = NULL; libcstring_system_character_t *option_compression_level = NULL; libcstring_system_character_t *option_format = NULL; libcstring_system_character_t *option_header_codepage = NULL; libcstring_system_character_t *option_maximum_segment_size = NULL; libcstring_system_character_t *option_offset = NULL; libcstring_system_character_t *option_process_buffer_size = NULL; libcstring_system_character_t *option_sectors_per_chunk = NULL; libcstring_system_character_t *option_size = NULL; libcstring_system_character_t *option_target_filename = NULL; libcstring_system_character_t *program = _LIBCSTRING_SYSTEM_STRING( "ewfexport" ); libcstring_system_character_t *request_string = NULL; log_handle_t *log_handle = NULL; libcstring_system_integer_t option = 0; size64_t media_size = 0; size_t string_length = 0; uint8_t calculate_md5 = 1; uint8_t print_status_information = 1; uint8_t swap_byte_pairs = 0; uint8_t verbose = 0; uint8_t zero_chunk_on_error = 0; int interactive_mode = 1; int number_of_filenames = 0; int result = 1; libsystem_notify_set_stream( stderr, NULL ); libsystem_notify_set_verbose( 1 ); if( libsystem_initialize( "ewftools", &error ) != 1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } #if defined( WINAPI ) && !defined( __CYGWIN__ ) #if defined( _MSC_VER ) if( _setmode( _fileno( stdout ), _O_BINARY ) == -1 ) #else if( setmode( _fileno( stdout ), _O_BINARY ) == -1 ) #endif { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to set stdout to binary mode.\n" ); usage_fprint( stdout ); goto on_error; } #endif while( ( option = libsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "A:b:B:c:d:f:hl:o:p:qsS:t:uvVw" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n", argv[ optind - 1 ] ); usage_fprint( stderr ); goto on_error; case (libcstring_system_integer_t) 'A': option_header_codepage = optarg; break; case (libcstring_system_integer_t) 'b': option_sectors_per_chunk = optarg; break; case (libcstring_system_integer_t) 'B': option_size = optarg; break; case (libcstring_system_integer_t) 'c': option_compression_level = optarg; break; case (libcstring_system_integer_t) 'd': option_additional_digest_types = optarg; break; case (libcstring_system_integer_t) 'f': option_format = optarg; break; case (libcstring_system_integer_t) 'h': ewfoutput_version_fprint( stderr, program ); usage_fprint( stderr ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'l': log_filename = optarg; break; case (libcstring_system_integer_t) 'o': option_offset = optarg; break; case (libcstring_system_integer_t) 'p': option_process_buffer_size = optarg; break; case (libcstring_system_integer_t) 'q': print_status_information = 0; break; case (libcstring_system_integer_t) 's': swap_byte_pairs = 1; break; case (libcstring_system_integer_t) 'S': option_maximum_segment_size = optarg; break; case (libcstring_system_integer_t) 't': option_target_filename = optarg; break; case (libcstring_system_integer_t) 'u': interactive_mode = 0; break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': ewfoutput_version_fprint( stderr, program ); ewfoutput_copyright_fprint( stderr ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'w': zero_chunk_on_error = 1; break; } } if( optind == argc ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Missing EWF image file(s).\n" ); usage_fprint( stderr ); goto on_error; } ewfoutput_version_fprint( stderr, program ); libsystem_notify_set_verbose( verbose ); libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); #if !defined( LIBSYSTEM_HAVE_GLOB ) if( libsystem_glob_initialize( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to initialize glob.\n" ); goto on_error; } if( libsystem_glob_resolve( glob, &( argv[ optind ] ), argc - optind, &error ) != 1 ) { fprintf( stderr, "Unable to resolve glob.\n" ); goto on_error; } argv_filenames = glob->result; number_of_filenames = glob->number_of_results; #else argv_filenames = &( argv[ optind ] ); number_of_filenames = argc - optind; #endif if( export_handle_initialize( &ewfexport_export_handle, calculate_md5, &error ) != 1 ) { fprintf( stderr, "Unable to create export handle.\n" ); goto on_error; } if( libsystem_signal_attach( ewfexport_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } result = export_handle_open_input( ewfexport_export_handle, argv_filenames, number_of_filenames, &error ); if( ewfexport_abort != 0 ) { goto on_abort; } if( result != 1 ) { fprintf( stderr, "Unable to open EWF file(s).\n" ); goto on_error; } #if !defined( LIBSYSTEM_HAVE_GLOB ) if( libsystem_glob_free( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } #endif if( export_handle_get_input_media_size( ewfexport_export_handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve input media size.\n" ); goto on_error; } if( option_header_codepage != NULL ) { result = export_handle_set_header_codepage( ewfexport_export_handle, option_header_codepage, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set header codepage.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported header codepage defaulting to: ascii.\n" ); } } if( option_target_filename != NULL ) { if( export_handle_set_string( ewfexport_export_handle, option_target_filename, &( ewfexport_export_handle->target_filename ), &( ewfexport_export_handle->target_filename_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set target filename.\n" ); goto on_error; } } else if( interactive_mode == 0 ) { /* Make sure the target filename is set in unattended mode */ if( export_handle_set_string( ewfexport_export_handle, _LIBCSTRING_SYSTEM_STRING( "export" ), &( ewfexport_export_handle->target_filename ), &( ewfexport_export_handle->target_filename_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set target filename.\n" ); goto on_error; } } if( option_compression_level != NULL ) { result = export_handle_set_compression_values( ewfexport_export_handle, option_compression_level, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set compression values.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported compression level defaulting to: none.\n" ); } } if( option_format != NULL ) { result = export_handle_set_format( ewfexport_export_handle, option_format, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set format.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported output format defaulting to: raw.\n" ); } } if( option_sectors_per_chunk != NULL ) { result = export_handle_set_sectors_per_chunk( ewfexport_export_handle, option_sectors_per_chunk, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set sectors per chunk.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported sectors per chunk defaulting to: 64.\n" ); } } if( option_maximum_segment_size != NULL ) { result = export_handle_set_maximum_segment_size( ewfexport_export_handle, option_maximum_segment_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set maximum segment size.\n" ); goto on_error; } if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_EWF ) { if( ( result == 0 ) || ( ewfexport_export_handle->maximum_segment_size < EWFCOMMON_MINIMUM_SEGMENT_FILE_SIZE ) || ( ( ewfexport_export_handle->ewf_format == LIBEWF_FORMAT_ENCASE6 ) && ( ewfexport_export_handle->maximum_segment_size >= (uint64_t) EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_64BIT ) ) || ( ( ewfexport_export_handle->ewf_format != LIBEWF_FORMAT_ENCASE6 ) && ( ewfexport_export_handle->maximum_segment_size >= (uint64_t) EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_32BIT ) ) ) { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_DEFAULT_SEGMENT_FILE_SIZE; fprintf( stderr, "Unsupported maximum segment size defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->maximum_segment_size ); } } else if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_RAW ) { if( ( result == 0 ) || ( ( ewfexport_export_handle->maximum_segment_size != 0 ) && ( ewfexport_export_handle->maximum_segment_size >= (uint64_t) EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_64BIT ) ) ) { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_DEFAULT_SEGMENT_FILE_SIZE; fprintf( stderr, "Unsupported maximum segment size defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->maximum_segment_size ); } } } if( option_offset != NULL ) { string_length = libcstring_system_string_length( option_offset ); if( libsystem_string_to_uint64( option_offset, string_length + 1, &ewfexport_export_handle->export_offset, &error ) != 1 ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); ewfexport_export_handle->export_offset = 0; fprintf( stderr, "Unsupported export offset defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->export_offset ); } } if( option_size != NULL ) { string_length = libcstring_system_string_length( option_size ); if( libsystem_string_to_uint64( option_size, string_length + 1, &ewfexport_export_handle->export_size, &error ) != 1 ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); ewfexport_export_handle->export_size = 0; fprintf( stderr, "Unsupported export size defaulting to: all bytes.\n" ); } } if( option_process_buffer_size != NULL ) { result = export_handle_set_process_buffer_size( ewfexport_export_handle, option_process_buffer_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set process buffer size.\n" ); goto on_error; } else if( ( result == 0 ) || ( ewfexport_export_handle->process_buffer_size > (size_t) SSIZE_MAX ) ) { ewfexport_export_handle->process_buffer_size = 0; fprintf( stderr, "Unsupported process buffer size defaulting to: chunk size.\n" ); } } if( option_additional_digest_types != NULL ) { result = export_handle_set_additional_digest_types( ewfexport_export_handle, option_additional_digest_types, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set additional digest types.\n" ); goto on_error; } } /* Initialize values */ if( ( ewfexport_export_handle->export_size == 0 ) || ( ewfexport_export_handle->export_size > ( media_size - ewfexport_export_handle->export_offset ) ) ) { ewfexport_export_handle->export_size = media_size - ewfexport_export_handle->export_offset; } /* Request the necessary case data */ if( interactive_mode != 0 ) { if( libsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } fprintf( stderr, "Information for export required, please provide the necessary input\n" ); if( option_format == NULL ) { result = export_handle_prompt_for_format( ewfexport_export_handle, _LIBCSTRING_SYSTEM_STRING( "Export to format" ), &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine format.\n" ); goto on_error; } } if( option_target_filename == NULL ) { if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_EWF ) { request_string = _LIBCSTRING_SYSTEM_STRING( "Target path and filename without extension" ); } else if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_FILES ) { request_string = _LIBCSTRING_SYSTEM_STRING( "Target path" ); } else if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_RAW ) { request_string = _LIBCSTRING_SYSTEM_STRING( "Target path and filename without extension or - for stdout" ); } } if( request_string != NULL ) { do { result = export_handle_prompt_for_string( ewfexport_export_handle, request_string, &( ewfexport_export_handle->target_filename ), &( ewfexport_export_handle->target_filename_size ), &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine target.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stdout, "Target is required, please try again or terminate using Ctrl^C.\n" ); } } while( result != 1 ); } if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_EWF ) { if( option_compression_level == NULL ) { result = export_handle_prompt_for_compression_level( ewfexport_export_handle, _LIBCSTRING_SYSTEM_STRING( "Use compression" ), &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine compression level.\n" ); goto on_error; } } if( option_maximum_segment_size == NULL ) { result = export_handle_prompt_for_maximum_segment_size( ewfexport_export_handle, _LIBCSTRING_SYSTEM_STRING( "Evidence segment file size in bytes" ), &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine maximum segment size.\n" ); goto on_error; } if( ( ewfexport_export_handle->maximum_segment_size < EWFCOMMON_MINIMUM_SEGMENT_FILE_SIZE ) || ( ( ewfexport_export_handle->ewf_format == LIBEWF_FORMAT_ENCASE6 ) && ( ewfexport_export_handle->maximum_segment_size >= (uint64_t) EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_64BIT ) ) || ( ( ewfexport_export_handle->ewf_format != LIBEWF_FORMAT_ENCASE6 ) && ( ewfexport_export_handle->maximum_segment_size >= (uint64_t) EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_32BIT ) ) ) { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_DEFAULT_SEGMENT_FILE_SIZE; fprintf( stderr, "Unsupported maximum segment size defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->maximum_segment_size ); } } if( option_sectors_per_chunk == NULL ) { result = export_handle_prompt_for_sectors_per_chunk( ewfexport_export_handle, _LIBCSTRING_SYSTEM_STRING( "The number of sectors to read at once" ), &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine sectors per chunk.\n" ); goto on_error; } } } else if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_RAW ) { if( ( ewfexport_export_handle->target_filename != NULL ) && ( ( ewfexport_export_handle->target_filename )[ 0 ] == (libcstring_system_character_t) '-' ) && ( ( ewfexport_export_handle->target_filename )[ 1 ] == 0 ) ) { /* No need for segment files when exporting to stdout */ } else if( option_maximum_segment_size == NULL ) { result = export_handle_prompt_for_maximum_segment_size( ewfexport_export_handle, _LIBCSTRING_SYSTEM_STRING( "Evidence segment file size in bytes (0 is unlimited)" ), &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine maximum segment size.\n" ); goto on_error; } if( ( ewfexport_export_handle->maximum_segment_size != 0 ) && ( ewfexport_export_handle->maximum_segment_size >= (uint64_t) EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_64BIT ) ) { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_DEFAULT_SEGMENT_FILE_SIZE; fprintf( stderr, "Unsupported maximum segment size defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->maximum_segment_size ); } } } if( ( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_EWF ) || ( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_RAW ) ) { if( option_offset == NULL ) { if( ewfinput_get_size_variable( stderr, input_buffer, EWFEXPORT_INPUT_BUFFER_SIZE, _LIBCSTRING_SYSTEM_STRING( "Start export at offset" ), 0, media_size, ewfexport_export_handle->export_offset, &( ewfexport_export_handle->export_offset ), &error ) == -1 ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); ewfexport_export_handle->export_offset = 0; fprintf( stderr, "Unable to determine export offset defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->export_offset ); } } if( option_size == NULL ) { if( ewfinput_get_size_variable( stderr, input_buffer, EWFEXPORT_INPUT_BUFFER_SIZE, _LIBCSTRING_SYSTEM_STRING( "Number of bytes to export" ), 0, media_size - ewfexport_export_handle->export_offset, ewfexport_export_handle->export_size, &( ewfexport_export_handle->export_size ), &error ) == -1 ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); ewfexport_export_handle->export_size = media_size - ewfexport_export_handle->export_offset; fprintf( stderr, "Unable to determine export size defaulting to: %" PRIu64 ".\n", ewfexport_export_handle->export_size ); } } } if( libsystem_signal_attach( ewfexport_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } } else { if( ewfexport_export_handle->maximum_segment_size == 0 ) { if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_EWF ) { if( ewfexport_export_handle->ewf_format == LIBEWF_FORMAT_ENCASE6 ) { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_64BIT; } else { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_32BIT; } } else if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_RAW ) { ewfexport_export_handle->maximum_segment_size = EWFCOMMON_MAXIMUM_SEGMENT_FILE_SIZE_64BIT; } } } fprintf( stderr, "\n" ); if( log_filename != NULL ) { if( log_handle_initialize( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to create log handle.\n" ); goto on_error; } if( log_handle_open( log_handle, log_filename, &error ) != 1 ) { fprintf( stderr, "Unable to open log file: %" PRIs_LIBCSTRING_SYSTEM ".\n", log_filename ); goto on_error; } } if( ewfexport_export_handle->output_format == EXPORT_HANDLE_OUTPUT_FORMAT_FILES ) { result = export_handle_export_single_files( ewfexport_export_handle, ewfexport_export_handle->target_filename, print_status_information, log_handle, &error ); if( result != 1 ) { fprintf( stderr, "Unable to export single files.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } } else { if( export_handle_open_output( ewfexport_export_handle, ewfexport_export_handle->target_filename, &error ) != 1 ) { fprintf( stderr, "Unable to open output.\n" ); goto on_error; } if( platform_get_operating_system( acquiry_operating_system, 32, &error ) != 1 ) { fprintf( stderr, "Unable to determine operating system.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); acquiry_operating_system[ 0 ] = 0; } acquiry_software_version = _LIBCSTRING_SYSTEM_STRING( LIBEWF_VERSION_STRING ); if( export_handle_set_output_values( ewfexport_export_handle, acquiry_operating_system, program, acquiry_software_version, zero_chunk_on_error, &error ) != 1 ) { fprintf( stderr, "Unable to set output values.\n" ); goto on_error; } result = export_handle_export_input( ewfexport_export_handle, swap_byte_pairs, print_status_information, log_handle, &error ); if( result != 1 ) { fprintf( stderr, "Unable to export input.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } } if( log_handle != NULL ) { if( log_handle_close( log_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close log file: %" PRIs_LIBCSTRING_SYSTEM ".\n", log_filename ); goto on_error; } if( log_handle_free( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free log handle.\n" ); goto on_error; } } on_abort: if( export_handle_close( ewfexport_export_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close export handle.\n" ); goto on_error; } if( libsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } if( export_handle_free( &ewfexport_export_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free export handle.\n" ); goto on_error; } if( ewfexport_abort != 0 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n", program ); return( EXIT_FAILURE ); } if( result != 1 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": FAILURE\n", program ); return( EXIT_FAILURE ); } fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": SUCCESS\n", program ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } if( log_handle != NULL ) { log_handle_close( log_handle, NULL ); log_handle_free( &log_handle, NULL ); } if( ewfexport_export_handle != NULL ) { export_handle_close( ewfexport_export_handle, NULL ); export_handle_free( &ewfexport_export_handle, NULL ); } #if !defined( LIBSYSTEM_HAVE_GLOB ) if( glob != NULL ) { libsystem_glob_free( &glob, NULL ); } #endif return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { #if defined( HAVE_GETRLIMIT ) struct rlimit limit_data; #endif libcerror_error_t *error = NULL; libcstring_system_character_t * const *source_filenames = NULL; #if !defined( HAVE_GLOB_H ) libcsystem_glob_t *glob = NULL; #endif libcstring_system_character_t *log_filename = NULL; libcstring_system_character_t *program = _LIBCSTRING_SYSTEM_STRING( "ewfverify" ); libcstring_system_character_t *option_additional_digest_types = NULL; libcstring_system_character_t *option_format = NULL; libcstring_system_character_t *option_header_codepage = NULL; libcstring_system_character_t *option_process_buffer_size = NULL; log_handle_t *log_handle = NULL; libcstring_system_integer_t option = 0; uint8_t calculate_md5 = 1; uint8_t print_status_information = 1; uint8_t use_chunk_data_functions = 0; uint8_t verbose = 0; uint8_t zero_chunk_on_error = 0; int number_of_filenames = 0; int result = 0; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); if( libclocale_initialize( "ewftools", &error ) != 1 ) { fprintf( stderr, "Unable to initialize locale values.\n" ); goto on_error; } if( libcsystem_initialize( _IONBF, &error ) != 1 ) { fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } ewfoutput_version_fprint( stdout, program ); while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "A:d:f:hl:p:qvVwx" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM "\n", argv[ optind - 1 ] ); usage_fprint( stdout ); goto on_error; case (libcstring_system_integer_t) 'A': option_header_codepage = optarg; break; case (libcstring_system_integer_t) 'd': option_additional_digest_types = optarg; break; case (libcstring_system_integer_t) 'f': option_format = optarg; break; case (libcstring_system_integer_t) 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'l': log_filename = optarg; break; case (libcstring_system_integer_t) 'p': option_process_buffer_size = optarg; break; case (libcstring_system_integer_t) 'q': print_status_information = 0; break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': ewfoutput_copyright_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'w': zero_chunk_on_error = 1; break; case (libcstring_system_integer_t) 'x': use_chunk_data_functions = 1; break; } } if( optind == argc ) { fprintf( stderr, "Missing EWF image file(s).\n" ); usage_fprint( stdout ); goto on_error; } libcnotify_verbose_set( verbose ); #if !defined( HAVE_LOCAL_LIBEWF ) libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); #endif if( verification_handle_initialize( &ewfverify_verification_handle, calculate_md5, use_chunk_data_functions, &error ) != 1 ) { fprintf( stderr, "Unable to create verification handle.\n" ); goto on_error; } if( option_header_codepage != NULL ) { result = verification_handle_set_header_codepage( ewfverify_verification_handle, option_header_codepage, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set header codepage.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported header codepage defaulting to: ascii.\n" ); } } if( option_format != NULL ) { result = verification_handle_set_format( ewfverify_verification_handle, option_format, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set format.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported input format defaulting to: raw.\n" ); } } if( option_process_buffer_size != NULL ) { result = verification_handle_set_process_buffer_size( ewfverify_verification_handle, option_process_buffer_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set process buffer size.\n" ); goto on_error; } else if( ( result == 0 ) || ( ewfverify_verification_handle->process_buffer_size > (size_t) SSIZE_MAX ) ) { ewfverify_verification_handle->process_buffer_size = 0; fprintf( stderr, "Unsupported process buffer size defaulting to: chunk size.\n" ); } } if( option_additional_digest_types != NULL ) { result = verification_handle_set_additional_digest_types( ewfverify_verification_handle, option_additional_digest_types, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set additional digest types.\n" ); goto on_error; } } #if !defined( HAVE_GLOB_H ) if( libcsystem_glob_initialize( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to initialize glob.\n" ); goto on_error; } if( libcsystem_glob_resolve( glob, &( argv[ optind ] ), argc - optind, &error ) != 1 ) { fprintf( stderr, "Unable to resolve glob.\n" ); goto on_error; } source_filenames = glob->result; number_of_filenames = glob->number_of_results; #else source_filenames = &( argv[ optind ] ); number_of_filenames = argc - optind; #endif #if defined( HAVE_GETRLIMIT ) if( getrlimit( RLIMIT_NOFILE, &limit_data ) != 0 ) { fprintf( stderr, "Unable to determine limit: number of open file descriptors.\n" ); } if( limit_data.rlim_max > (rlim_t) INT_MAX ) { limit_data.rlim_max = (rlim_t) INT_MAX; } if( limit_data.rlim_max > 0 ) { limit_data.rlim_max /= 2; } if( verification_handle_set_maximum_number_of_open_handles( ewfverify_verification_handle, (int) limit_data.rlim_max, &error ) != 1 ) { fprintf( stderr, "Unable to set maximum number of open file handles.\n" ); goto on_error; } #endif if( libcsystem_signal_attach( ewfverify_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } result = verification_handle_open_input( ewfverify_verification_handle, source_filenames, number_of_filenames, &error ); if( ewfverify_abort != 0 ) { goto on_abort; } if( result != 1 ) { fprintf( stderr, "Unable to open EWF image file(s).\n" ); goto on_error; } #if !defined( HAVE_GLOB_H ) if( libcsystem_glob_free( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } #endif if( verification_handle_set_zero_chunk_on_error( ewfverify_verification_handle, zero_chunk_on_error, &error ) != 1 ) { fprintf( stderr, "Unable to set zero on chunk error.\n" ); goto on_error; } if( log_filename != NULL ) { if( log_handle_initialize( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to create log handle.\n" ); goto on_error; } if( log_handle_open( log_handle, log_filename, &error ) != 1 ) { fprintf( stderr, "Unable to open log file: %" PRIs_LIBCSTRING_SYSTEM ".\n", log_filename ); goto on_error; } } if( ewfverify_verification_handle->input_format == VERIFICATION_HANDLE_INPUT_FORMAT_FILES ) { result = verification_handle_verify_single_files( ewfverify_verification_handle, print_status_information, log_handle, &error ); if( result != 1 ) { fprintf( stderr, "Unable to verify single files.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } } else { result = verification_handle_verify_input( ewfverify_verification_handle, print_status_information, log_handle, &error ); if( result == -1 ) { fprintf( stderr, "Unable to verify input.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } } if( log_handle != NULL ) { if( log_handle_close( log_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close log handle.\n" ); goto on_error; } if( log_handle_free( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free log handle.\n" ); goto on_error; } } on_abort: if( libcsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( verification_handle_close( ewfverify_verification_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close verification handle.\n" ); goto on_error; } if( verification_handle_free( &ewfverify_verification_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free verification handle.\n" ); goto on_error; } if( ewfverify_abort != 0 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n", program ); return( EXIT_FAILURE ); } if( result != 1 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": FAILURE\n", program ); return( EXIT_FAILURE ); } fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": SUCCESS\n", program ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( log_handle != NULL ) { log_handle_close( log_handle, NULL ); log_handle_free( &log_handle, NULL ); } if( ewfverify_verification_handle != NULL ) { verification_handle_close( ewfverify_verification_handle, NULL ); verification_handle_free( &ewfverify_verification_handle, NULL ); } #if !defined( HAVE_GLOB_H ) if( glob != NULL ) { libcsystem_glob_free( &glob, NULL ); } #endif return( EXIT_FAILURE ); }
disk_t *fewf_init(const char *device, const int mode) { unsigned int num_files=0; char **filenames= NULL; disk_t *disk=NULL; struct info_fewf_struct *data; #if !defined( HAVE_LIBEWF_V2_API ) && defined( HAVE_GLOB_H ) glob_t globbuf; #endif data=(struct info_fewf_struct *)MALLOC(sizeof(struct info_fewf_struct)); memset(data, 0, sizeof(struct info_fewf_struct)); data->file_name = strdup(device); data->handle=NULL; data->mode = mode; #ifdef DEBUG_EWF #if defined( HAVE_LIBEWF_V2_API ) libewf_notify_set_stream( stderr, NULL ); libewf_notify_set_verbose( 1 ); #else libewf_set_notify_values( stderr, 1 ); #endif #endif #if defined( HAVE_LIBEWF_V2_API ) if( libewf_glob( data->file_name, strlen(data->file_name), LIBEWF_FORMAT_UNKNOWN, &filenames, (int *)&num_files, NULL ) != 1 ) { log_error("libewf_glob failed\n"); free(data); return NULL; } #elif defined( HAVE_GLOB_H ) { globbuf.gl_offs = 0; glob(data->file_name, GLOB_DOOFFS, NULL, &globbuf); if(globbuf.gl_pathc>0) { filenames=(char **)MALLOC(globbuf.gl_pathc * sizeof(*filenames)); for (num_files=0; num_files<globbuf.gl_pathc; num_files++) { filenames[num_files]=globbuf.gl_pathv[num_files]; } } } if(filenames==NULL) { globfree(&globbuf); free(data); return NULL; } #else { filenames=(char **)MALLOC(1*sizeof(*filenames)); filenames[num_files] = data->file_name; num_files++; } #endif if((mode&TESTDISK_O_RDWR)==TESTDISK_O_RDWR) { #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_initialize( &( data->handle ), NULL ) != 1 ) { log_error("libewf_handle_initialize failed\n"); libewf_glob_free( filenames, num_files, NULL ); free(data); return NULL; } if( libewf_handle_open( data->handle, filenames, num_files, LIBEWF_OPEN_READ_WRITE, NULL ) != 1 ) { log_error("libewf_handle_open(%s) failed\n", device); } #else data->handle=libewf_open(filenames, num_files, LIBEWF_OPEN_READ_WRITE); if(data->handle==NULL) { log_error("libewf_open(%s) failed\n", device); } #endif /* defined( HAVE_LIBEWF_V2_API ) */ } if(data->handle==NULL) { data->mode&=~TESTDISK_O_RDWR; #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_initialize( &( data->handle ), NULL ) != 1 ) { log_error("libewf_handle_initialize failed\n"); libewf_glob_free( filenames, num_files, NULL ); free(data); return NULL; } if( libewf_handle_open( data->handle, filenames, num_files, LIBEWF_OPEN_READ, NULL ) != 1 ) { log_error("libewf_handle_open(%s) failed\n", device); libewf_handle_free( &( data->handle ), NULL ); libewf_glob_free( filenames, num_files, NULL ); free(data); return NULL; } #else data->handle=libewf_open(filenames, num_files, LIBEWF_OPEN_READ); if(data->handle==NULL) { log_error("libewf_open(%s) failed\n", device); #if defined( HAVE_GLOB_H ) globfree(&globbuf); #endif free(filenames); free(data); return NULL; } #endif /* defined( HAVE_LIBEWF_V2_API ) */ } #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_set_header_values_date_format( data->handle, LIBEWF_DATE_FORMAT_DAYMONTH, NULL ) != 1 ) { log_error("%s Unable to set header values date format\n", device); } #else if( libewf_parse_header_values( data->handle, LIBEWF_DATE_FORMAT_DAYMONTH) != 1 ) { log_error("%s Unable to parse EWF header values\n", device); } #endif disk=(disk_t *)MALLOC(sizeof(*disk)); init_disk(disk); disk->arch=&arch_none; disk->device=strdup(device); disk->data=data; disk->description=fewf_description; disk->description_short=fewf_description_short; disk->pread_fast=fewf_pread_fast; disk->pread=fewf_pread; disk->pwrite=(data->mode&TESTDISK_O_RDWR?fewf_pwrite:fewf_nopwrite); disk->sync=fewf_sync; disk->access_mode=(data->mode&TESTDISK_O_RDWR); disk->clean=fewf_clean; #if defined( HAVE_LIBEWF_V2_API ) || defined( LIBEWF_GET_BYTES_PER_SECTOR_HAVE_TWO_ARGUMENTS ) { uint32_t bytes_per_sector = 0; #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_get_bytes_per_sector( data->handle, &bytes_per_sector, NULL ) != 1 ) #else if( libewf_get_bytes_per_sector(data->handle, &bytes_per_sector)<0) #endif { disk->sector_size=DEFAULT_SECTOR_SIZE; } else { disk->sector_size=bytes_per_sector; } } #else disk->sector_size=libewf_get_bytes_per_sector(data->handle); #endif // printf("libewf_get_bytes_per_sector %u\n",disk->sector_size); if(disk->sector_size==0) disk->sector_size=DEFAULT_SECTOR_SIZE; /* Set geometry */ disk->geom.cylinders=0; disk->geom.heads_per_cylinder=1; disk->geom.sectors_per_head=1; disk->geom.bytes_per_sector=disk->sector_size; /* Get disk_real_size */ #if defined( HAVE_LIBEWF_V2_API ) || defined( LIBEWF_GET_MEDIA_SIZE_HAVE_TWO_ARGUMENTS ) { size64_t media_size = 0; #if defined( HAVE_LIBEWF_V2_API ) if( libewf_handle_get_media_size( data->handle, &media_size, NULL ) != 1 ) #else if(libewf_get_media_size(data->handle, &media_size)<0) #endif { disk->disk_real_size=0; } else { disk->disk_real_size=media_size; } } #else disk->disk_real_size=libewf_get_media_size(data->handle); #endif update_disk_car_fields(disk); #if defined( HAVE_LIBEWF_V2_API ) libewf_glob_free( filenames, num_files, NULL ); #else #if defined( HAVE_GLOB_H ) globfree(&globbuf); #endif free(filenames); #endif return disk; }
int main( int argc, char * const argv[] ) #endif { #if defined( HAVE_GETRLIMIT ) struct rlimit limit_data; #endif libcstring_system_character_t acquiry_operating_system[ 32 ]; libcstring_system_character_t * const *argv_filenames = NULL; libcerror_error_t *error = NULL; #if !defined( LIBCSYSTEM_HAVE_GLOB ) libcsystem_glob_t *glob = NULL; #endif libcstring_system_character_t *acquiry_software_version = NULL; libcstring_system_character_t *log_filename = NULL; libcstring_system_character_t *option_header_codepage = NULL; libcstring_system_character_t *option_process_buffer_size = NULL; libcstring_system_character_t *option_target_path = NULL; libcstring_system_character_t *program = _LIBCSTRING_SYSTEM_STRING( "ewfrecover" ); log_handle_t *log_handle = NULL; libcstring_system_integer_t option = 0; uint8_t calculate_md5 = 1; uint8_t print_status_information = 1; uint8_t verbose = 0; int number_of_filenames = 0; int result = 1; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); if( libclocale_initialize( "ewftools", &error ) != 1 ) { fprintf( stderr, "Unable to initialize locale values.\n" ); goto on_error; } if( libcsystem_initialize( _IONBF, &error ) != 1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } #if defined( WINAPI ) && !defined( __CYGWIN__ ) #if defined( _MSC_VER ) if( _setmode( _fileno( stdout ), _O_BINARY ) == -1 ) #else if( setmode( _fileno( stdout ), _O_BINARY ) == -1 ) #endif { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to set stdout to binary mode.\n" ); usage_fprint( stdout ); goto on_error; } #endif while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "A:f:hl:p:qt:uvV" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n", argv[ optind - 1 ] ); usage_fprint( stderr ); goto on_error; case (libcstring_system_integer_t) 'A': option_header_codepage = optarg; break; case (libcstring_system_integer_t) 'h': ewfoutput_version_fprint( stderr, program ); usage_fprint( stderr ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'l': log_filename = optarg; break; case (libcstring_system_integer_t) 'p': option_process_buffer_size = optarg; break; case (libcstring_system_integer_t) 'q': print_status_information = 0; break; case (libcstring_system_integer_t) 't': option_target_path = optarg; break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': ewfoutput_version_fprint( stderr, program ); ewfoutput_copyright_fprint( stderr ); return( EXIT_SUCCESS ); } } if( optind == argc ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Missing EWF image file(s).\n" ); usage_fprint( stderr ); goto on_error; } ewfoutput_version_fprint( stderr, program ); libcnotify_verbose_set( verbose ); #if !defined( HAVE_LOCAL_LIBEWF ) libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); #endif #if !defined( LIBCSYSTEM_HAVE_GLOB ) if( libcsystem_glob_initialize( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to initialize glob.\n" ); goto on_error; } if( libcsystem_glob_resolve( glob, &( argv[ optind ] ), argc - optind, &error ) != 1 ) { fprintf( stderr, "Unable to resolve glob.\n" ); goto on_error; } argv_filenames = glob->result; number_of_filenames = glob->number_of_results; #else argv_filenames = &( argv[ optind ] ); number_of_filenames = argc - optind; #endif if( export_handle_initialize( &ewfrecover_export_handle, calculate_md5, &error ) != 1 ) { fprintf( stderr, "Unable to create export handle.\n" ); goto on_error; } #if defined( HAVE_GETRLIMIT ) if( getrlimit( RLIMIT_NOFILE, &limit_data ) != 0 ) { fprintf( stderr, "Unable to determine limit: number of open file descriptors.\n" ); } if( limit_data.rlim_max > (rlim_t) INT_MAX ) { limit_data.rlim_max = (rlim_t) INT_MAX; } if( limit_data.rlim_max > 0 ) { limit_data.rlim_max /= 2; } if( export_handle_set_maximum_number_of_open_handles( ewfrecover_export_handle, (int) limit_data.rlim_max, &error ) != 1 ) { fprintf( stderr, "Unable to set maximum number of open file handles.\n" ); goto on_error; } #endif if( libcsystem_signal_attach( ewfrecover_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } result = export_handle_open_input( ewfrecover_export_handle, argv_filenames, number_of_filenames, &error ); if( ewfrecover_abort != 0 ) { goto on_abort; } if( result != 1 ) { fprintf( stderr, "Unable to open EWF file(s).\n" ); goto on_error; } #if !defined( LIBCSYSTEM_HAVE_GLOB ) if( libcsystem_glob_free( &glob, &error ) != 1 ) { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } #endif result = export_handle_input_is_corrupted( ewfrecover_export_handle, &error ); if( result == -1 ) { fprintf( stderr, "Unable to determine if EWF file(s) are corrupted.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "EWF file(s) are not corrupted.\n" ); goto on_error; } ewfrecover_export_handle->output_format = EXPORT_HANDLE_OUTPUT_FORMAT_EWF; ewfrecover_export_handle->export_size = ewfrecover_export_handle->input_media_size; if( option_header_codepage != NULL ) { result = export_handle_set_header_codepage( ewfrecover_export_handle, option_header_codepage, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set header codepage.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported header codepage defaulting to: ascii.\n" ); } } if( option_target_path != NULL ) { if( export_handle_set_string( ewfrecover_export_handle, option_target_path, &( ewfrecover_export_handle->target_path ), &( ewfrecover_export_handle->target_path_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set target path.\n" ); goto on_error; } } else { /* Make sure the target filename is set in unattended mode */ if( export_handle_set_string( ewfrecover_export_handle, _LIBCSTRING_SYSTEM_STRING( "recover" ), &( ewfrecover_export_handle->target_path ), &( ewfrecover_export_handle->target_path_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set target filename.\n" ); goto on_error; } } if( option_process_buffer_size != NULL ) { result = export_handle_set_process_buffer_size( ewfrecover_export_handle, option_process_buffer_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set process buffer size.\n" ); goto on_error; } else if( ( result == 0 ) || ( ewfrecover_export_handle->process_buffer_size > (size_t) SSIZE_MAX ) ) { ewfrecover_export_handle->process_buffer_size = 0; fprintf( stderr, "Unsupported process buffer size defaulting to: chunk size.\n" ); } } /* Initialize values */ if( log_filename != NULL ) { if( log_handle_initialize( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to create log handle.\n" ); goto on_error; } if( log_handle_open( log_handle, log_filename, &error ) != 1 ) { fprintf( stderr, "Unable to open log file: %" PRIs_LIBCSTRING_SYSTEM ".\n", log_filename ); goto on_error; } } if( export_handle_open_output( ewfrecover_export_handle, ewfrecover_export_handle->target_path, &error ) != 1 ) { fprintf( stderr, "Unable to open output.\n" ); goto on_error; } if( platform_get_operating_system( acquiry_operating_system, 32, &error ) != 1 ) { fprintf( stderr, "Unable to determine operating system.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); acquiry_operating_system[ 0 ] = 0; } acquiry_software_version = _LIBCSTRING_SYSTEM_STRING( LIBEWF_VERSION_STRING ); if( export_handle_set_output_values( ewfrecover_export_handle, acquiry_operating_system, program, acquiry_software_version, 0, 1, &error ) != 1 ) { fprintf( stderr, "Unable to set output values.\n" ); goto on_error; } result = export_handle_export_input( ewfrecover_export_handle, 0, print_status_information, log_handle, &error ); if( result != 1 ) { fprintf( stderr, "Unable to recover input.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( log_handle != NULL ) { if( log_handle_close( log_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close log file: %" PRIs_LIBCSTRING_SYSTEM ".\n", log_filename ); goto on_error; } if( log_handle_free( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free log handle.\n" ); goto on_error; } } on_abort: if( export_handle_close( ewfrecover_export_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close export handle.\n" ); goto on_error; } if( libcsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( export_handle_free( &ewfrecover_export_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free export handle.\n" ); goto on_error; } if( ewfrecover_abort != 0 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n", program ); return( EXIT_FAILURE ); } if( result != 1 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": FAILURE\n", program ); return( EXIT_FAILURE ); } fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": SUCCESS\n", program ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( log_handle != NULL ) { log_handle_close( log_handle, NULL ); log_handle_free( &log_handle, NULL ); } if( ewfrecover_export_handle != NULL ) { export_handle_close( ewfrecover_export_handle, NULL ); export_handle_free( &ewfrecover_export_handle, NULL ); } #if !defined( LIBCSYSTEM_HAVE_GLOB ) if( glob != NULL ) { libcsystem_glob_free( &glob, NULL ); } #endif return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { #if defined( HAVE_GETRLIMIT ) struct rlimit limit_data; #endif libcstring_system_character_t * const *source_filenames = NULL; #if !defined( HAVE_GLOB_H ) libcsystem_glob_t *glob = NULL; #endif libcerror_error_t *error = NULL; libcstring_system_character_t *option_date_format = NULL; libcstring_system_character_t *option_header_codepage = NULL; libcstring_system_character_t *option_output_format = NULL; libcstring_system_character_t *program = _LIBCSTRING_SYSTEM_STRING( "ewfinfo" ); libcstring_system_integer_t option = 0; uint8_t verbose = 0; char info_option = 'a'; int number_of_filenames = 0; int print_header = 1; int result = 0; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); if( libclocale_initialize( "ewftools", &error ) != 1 ) { fprintf( stderr, "Unable to initialize locale values.\n" ); goto on_error; } if( libcsystem_initialize( _IONBF, &error ) != 1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "A:d:ef:himvV" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM "\n", argv[ optind - 1 ] ); usage_fprint( stdout ); goto on_error; case (libcstring_system_integer_t) 'A': option_header_codepage = optarg; break; case (libcstring_system_integer_t) 'd': option_date_format = optarg; break; case (libcstring_system_integer_t) 'e': if( info_option != 'a' ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Conflicting options: %" PRIc_LIBCSTRING_SYSTEM " and %c\n", option, info_option ); usage_fprint( stdout ); goto on_error; } info_option = 'e'; break; case (libcstring_system_integer_t) 'f': option_output_format = optarg; break; case (libcstring_system_integer_t) 'h': ewfoutput_version_fprint( stdout, program ); usage_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'i': if( info_option != 'a' ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Conflicting options: %" PRIc_LIBCSTRING_SYSTEM " and %c\n", option, info_option ); usage_fprint( stdout ); goto on_error; } info_option = 'i'; break; case (libcstring_system_integer_t) 'm': if( info_option != 'a' ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Conflicting options: %" PRIc_LIBCSTRING_SYSTEM " and %c\n", option, info_option ); usage_fprint( stdout ); goto on_error; } info_option = 'm'; break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': ewfoutput_version_fprint( stdout, program ); ewfoutput_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Missing EWF image file(s).\n" ); usage_fprint( stdout ); goto on_error; } libcnotify_verbose_set( verbose ); #if !defined( HAVE_LOCAL_LIBEWF ) libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); #endif if( info_handle_initialize( &ewfinfo_info_handle, &error ) != 1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to create info handle.\n" ); goto on_error; } if( option_output_format != NULL ) { result = info_handle_set_output_format( ewfinfo_info_handle, option_output_format, &error ); if( result == -1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to set output format.\n" ); goto on_error; } else if( result == 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; fprintf( stderr, "Unsupported output format defaulting to: text.\n" ); } } if( ewfinfo_info_handle->output_format == INFO_HANDLE_OUTPUT_FORMAT_DFXML ) { if( info_handle_dfxml_header_fprint( ewfinfo_info_handle, &error ) != 1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to print header.\n" ); goto on_error; } } else if( ewfinfo_info_handle->output_format == INFO_HANDLE_OUTPUT_FORMAT_TEXT ) { ewfoutput_version_fprint( stdout, program ); print_header = 0; } if( ( option_output_format == NULL ) && ( option_date_format != NULL ) ) { result = info_handle_set_date_format( ewfinfo_info_handle, option_date_format, &error ); if( result == -1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to set date format.\n" ); goto on_error; } else if( result == 0 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unsupported date format defaulting to: ctime.\n" ); } } if( option_header_codepage != NULL ) { result = info_handle_set_header_codepage( ewfinfo_info_handle, option_header_codepage, &error ); if( result == -1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to set header codepage in info handle.\n" ); goto on_error; } else if( result == 0 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unsupported header codepage defaulting to: ascii.\n" ); } } #if !defined( HAVE_GLOB_H ) if( libcsystem_glob_initialize( &glob, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to initialize glob.\n" ); goto on_error; } if( libcsystem_glob_resolve( glob, &( argv[ optind ] ), argc - optind, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to resolve glob.\n" ); goto on_error; } if( libcsystem_glob_get_results( glob, &number_of_filenames, (libcstring_system_character_t ***) &source_filenames, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve glob results.\n" ); goto on_error; } #else source_filenames = &( argv[ optind ] ); number_of_filenames = argc - optind; #endif #if defined( HAVE_GETRLIMIT ) if( getrlimit( RLIMIT_NOFILE, &limit_data ) != 0 ) { fprintf( stderr, "Unable to determine limit: number of open file descriptors.\n" ); } if( limit_data.rlim_max > (rlim_t) INT_MAX ) { limit_data.rlim_max = (rlim_t) INT_MAX; } if( limit_data.rlim_max > 0 ) { limit_data.rlim_max /= 2; } if( info_handle_set_maximum_number_of_open_handles( ewfinfo_info_handle, (int) limit_data.rlim_max, &error ) != 1 ) { fprintf( stderr, "Unable to set maximum number of open file handles.\n" ); goto on_error; } #endif if( libcsystem_signal_attach( ewfinfo_signal_handler, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to attach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } result = info_handle_open_input( ewfinfo_info_handle, source_filenames, number_of_filenames, &error ); if( ewfinfo_abort != 0 ) { goto on_abort; } if( result != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to open EWF file(s).\n" ); goto on_error; } #if !defined( HAVE_GLOB_H ) if( libcsystem_glob_free( &glob, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } #endif if( ( info_option == 'a' ) || ( info_option == 'i' ) ) { if( info_handle_header_values_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print header values.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } } if( ( info_option == 'a' ) || ( info_option == 'm' ) ) { if( info_handle_media_information_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print media information.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( info_handle_hash_values_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print hash values.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( info_handle_sessions_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print sessions.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( info_handle_tracks_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print tracks.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } } if( ( info_option == 'a' ) || ( info_option == 'e' ) ) { if( info_handle_acquiry_errors_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print acquiry errors.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } } if( info_handle_single_files_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print single files.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( ewfinfo_info_handle->output_format == INFO_HANDLE_OUTPUT_FORMAT_DFXML ) { if( info_handle_dfxml_footer_fprint( ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to print footer.\n" ); goto on_error; } } on_abort: if( info_handle_close( ewfinfo_info_handle, &error ) != 0 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to close info handle.\n" ); goto on_error; } if( libcsystem_signal_detach( &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to detach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( info_handle_free( &ewfinfo_info_handle, &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to free info handle.\n" ); goto on_error; } if( ewfinfo_abort != 0 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n", program ); return( EXIT_FAILURE ); } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( ewfinfo_info_handle != NULL ) { info_handle_free( &ewfinfo_info_handle, NULL ); } #if !defined( HAVE_GLOB_H ) if( glob != NULL ) { libcsystem_glob_free( &glob, NULL ); } #endif return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t *target_filename = NULL; libcerror_error_t *error = NULL; libewf_handle_t *handle = NULL; libcstring_system_integer_t option = 0; off64_t read_offset = 0; size64_t media_size = 0; size64_t read_size = 0; size32_t chunk_size = 0; size_t delta_segment_filename_length = 0; while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "t:" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n", argv[ optind - 1 ] ); return( EXIT_FAILURE ); case (libcstring_system_integer_t) 't': target_filename = optarg; break; } } if( optind == argc ) { fprintf( stderr, "Missing EWF image filename(s).\n" ); return( EXIT_FAILURE ); } /* Initialization */ if( libewf_handle_initialize( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_READ_WRITE_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, &( argv[ optind ] ), argc - optind, LIBEWF_OPEN_READ_WRITE, &error ) != 1 ) #else if( libewf_handle_open( handle, &( argv[ optind ] ), argc - optind, LIBEWF_OPEN_READ_WRITE, &error ) != 1 ) #endif { fprintf( stderr, "Unable to open file(s).\n" ); goto on_error; } if( target_filename != NULL ) { delta_segment_filename_length = libcstring_system_string_length( target_filename ); #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_set_delta_segment_filename_wide( handle, target_filename, delta_segment_filename_length, &error ) != 1 ) #else if( libewf_handle_set_delta_segment_filename( handle, target_filename, delta_segment_filename_length, &error ) != 1 ) #endif { fprintf( stderr, "Unable to set delta segment filename.\n" ); goto on_error; } } if( libewf_handle_get_media_size( handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } if( media_size > (size64_t) INT64_MAX ) { fprintf( stderr, "Media size exceeds maximum.\n" ); goto on_error; } if( libewf_handle_get_chunk_size( handle, &chunk_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve chunk size.\n" ); goto on_error; } if( chunk_size == 0 ) { fprintf( stderr, "Invalid chunk size.\n" ); goto on_error; } fprintf( stdout, "Media size: %" PRIu64 " bytes\n", media_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <media_size / 7> size: <media_size / 2> * Expected result: offset: <media_size / 7> size: <media_size / 2> */ read_offset = (off64_t) ( media_size / 7 ); read_size = media_size / 2; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } /* Case 2: test read buffer beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: 4096 * Expected result: offset: <media_size - 1024> size: 1024 */ read_offset = (off64_t) ( media_size - 1024 ); read_size = 4096; if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } if( ewf_test_read_write_buffer_at_offset( handle, read_offset, SEEK_SET, read_size, read_offset, 1024 ) != 1 ) { fprintf( stderr, "Unable to test read/write buffer.\n" ); goto on_error; } } fprintf( stdout, "\nChunk size: %" PRIu32 " bytes\n", chunk_size ); /* Case 0: test full read */ /* Test: offset: 0 size: <media_size> * Expected result: offset: 0 size: <media_size> */ read_offset = 0; read_size = media_size; if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } /* Case 1: test random read */ /* Test: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> * Expected result: offset: <( ( media_size / 7 ) / chunk_size ) * chunk_size> size: <( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size> */ read_offset = (off64_t) ( ( media_size / 7 ) / chunk_size ) * chunk_size; read_size = ( ( ( media_size / 2 ) / chunk_size ) + 1 ) * chunk_size; if( media_size == 0 ) { if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, 0 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } else { if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, read_offset, read_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } /* Case 2: test read chunk beyond media size */ if( media_size < 1024 ) { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: -1 size: <undetermined> */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, -1, (size64_t) -1 ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } else { /* Test: offset: <media_size - 1024> size: chunk_size * Expected result: offset: <media_size - 1024> size: chunk size or media_size % chunk_size */ read_offset = (off64_t) ( media_size - 1024 ); read_size = chunk_size; if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } if( ewf_test_read_write_chunk_at_offset( handle, chunk_size, read_offset, SEEK_SET, read_size, (off64_t) ( media_size - 1024 ), ( ( media_size % chunk_size ) == 0 ) ? chunk_size : media_size % chunk_size ) != 1 ) { fprintf( stderr, "Unable to test read/write chunk.\n" ); goto on_error; } } /* Clean up */ if( libewf_handle_close( handle, &error ) != 0 ) { fprintf( stderr, "Unable to close file(s).\n" ); goto on_error; } if( libewf_handle_free( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t **filenames = NULL; libewf_error_t *error = NULL; libewf_handle_t *handle = NULL; size64_t media_size = 0; int number_of_filenames = 0; if( argc < 2 ) { fprintf( stderr, "Missing filename(s).\n" ); return( EXIT_FAILURE ); } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_SEEK_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_glob_wide( argv[ 1 ], libcstring_wide_string_length( argv[ 1 ] ), LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, &error ) != 1 ) #else if( libewf_glob( argv[ 1 ], libcstring_narrow_string_length( argv[ 1 ] ), LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, &error ) != 1 ) #endif { fprintf( stderr, "Unable to glob filenames.\n" ); goto on_error; } if( number_of_filenames < 0 ) { fprintf( stderr, "Invalid number of filenames.\n" ); goto on_error; } else if( number_of_filenames == 0 ) { fprintf( stderr, "Missing filenames.\n" ); goto on_error; } /* Initialization */ if( libewf_handle_initialize( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to create handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, filenames, number_of_filenames, LIBEWF_OPEN_READ, &error ) != 1 ) #else if( libewf_handle_open( handle, filenames, number_of_filenames, LIBEWF_OPEN_READ, &error ) != 1 ) #endif { fprintf( stderr, "Unable to open handle.\n" ); goto on_error; } if( libewf_handle_get_media_size( handle, &media_size, &error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } if( ewf_handle_test_seek( handle, media_size ) != 1 ) { fprintf( stderr, "Unable to seek in handle.\n" ); goto on_error; } /* Clean up */ if( libewf_handle_close( handle, &error ) != 0 ) { fprintf( stderr, "Unable to close handle.\n" ); goto on_error; } if( libewf_handle_free( &handle, &error ) != 1 ) { fprintf( stderr, "Unable to free handle.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_glob_wide_free( filenames, number_of_filenames, &error ) != 1 ) #else if( libewf_glob_free( filenames, number_of_filenames, &error ) != 1 ) #endif { fprintf( stderr, "Unable to free glob.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } if( filenames != NULL ) { #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) libewf_glob_wide_free( filenames, number_of_filenames, NULL ); #else libewf_glob_free( filenames, number_of_filenames, NULL ); #endif } return( EXIT_FAILURE ); }
/* Tests reading/writing data of a specific size at a specific offset * Return 1 if successful, 0 if not or -1 on error */ int ewf_test_read_write_delta( libcstring_system_character_t * const filenames[], int number_of_filenames, const libcstring_system_character_t *delta_segment_filename, off64_t write_offset, size64_t write_size, libcerror_error_t **error ) { libewf_handle_t *handle = NULL; uint8_t *buffer = NULL; static char *function = "ewf_test_read_write_delta"; size_t delta_segment_filename_length = 0; size_t read_size = 0; ssize_t read_count = 0; ssize_t write_count = 0; if( libewf_handle_initialize( &handle, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create handle.", function ); goto on_error; } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_READ_WRITE_DELTA_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_open_wide( handle, filenames, number_of_filenames, LIBEWF_OPEN_READ_WRITE, error ) != 1 ) #else if( libewf_handle_open( handle, filenames, number_of_filenames, LIBEWF_OPEN_READ_WRITE, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open handle.", function ); goto on_error; } if( delta_segment_filename != NULL ) { delta_segment_filename_length = libcstring_system_string_length( delta_segment_filename ); #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libewf_handle_set_delta_segment_filename_wide( handle, delta_segment_filename, delta_segment_filename_length, error ) != 1 ) #else if( libewf_handle_set_delta_segment_filename( handle, delta_segment_filename, delta_segment_filename_length, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set delta segment filename.", function ); goto on_error; } } if( libewf_handle_seek_offset( handle, write_offset, SEEK_SET, error ) == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to seek offset: %" PRIi64 ".", function, write_offset ); goto on_error; } buffer = (uint8_t *) memory_allocate( EWF_TEST_BUFFER_SIZE ); while( write_size > 0 ) { if( write_size > (size64_t) EWF_TEST_BUFFER_SIZE ) { read_size = EWF_TEST_BUFFER_SIZE; } else { read_size = (size_t) write_size; } read_count = libewf_handle_read_buffer( handle, buffer, read_size, error ); if( read_count < 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_WRITE_FAILED, "%s: unable read buffer of size: %" PRIzd ".", function, read_size ); goto on_error; } if( memory_set( buffer, (int) 'X', (size_t) read_count ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable set value in buffer.", function ); goto on_error; } if( libewf_handle_seek_offset( handle, -1 * (off64_t) read_size, SEEK_CUR, error ) == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to seek previous offset.", function ); goto on_error; } write_count = libewf_handle_write_buffer( handle, buffer, (size_t) read_count, error ); if( write_count < 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_WRITE_FAILED, "%s: unable write buffer of size: %" PRIzd ".", function, read_count ); goto on_error; } if( write_count != read_count ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_WRITE_FAILED, "%s: unable write buffer of size: %" PRIzd ".", function, read_count ); goto on_error; } write_offset += write_count; write_size -= write_count; } memory_free( buffer ); buffer = NULL; if( libewf_handle_close( handle, error ) != 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_CLOSE_FAILED, "%s: unable to close handle.", function ); goto on_error; } if( libewf_handle_free( &handle, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free handle.", function ); goto on_error; } return( 1 ); on_error: if( buffer != NULL ) { memory_free( buffer ); } if( handle != NULL ) { libewf_handle_close( handle, NULL ); libewf_handle_free( &handle, NULL ); } return( -1 ); }
int main( int argc, char * const argv[] ) #endif { libcerror_error_t *error = NULL; libcstring_system_character_t *log_filename = NULL; libcstring_system_character_t *option_additional_digest_types = NULL; libcstring_system_character_t *option_bytes_per_sector = NULL; libcstring_system_character_t *option_case_number = NULL; libcstring_system_character_t *option_compression_values = NULL; libcstring_system_character_t *option_description = NULL; libcstring_system_character_t *option_examiner_name = NULL; libcstring_system_character_t *option_evidence_number = NULL; libcstring_system_character_t *option_format = NULL; libcstring_system_character_t *option_header_codepage = NULL; libcstring_system_character_t *option_maximum_segment_size = NULL; libcstring_system_character_t *option_media_flags = NULL; libcstring_system_character_t *option_media_type = NULL; libcstring_system_character_t *option_notes = NULL; libcstring_system_character_t *option_offset = NULL; libcstring_system_character_t *option_process_buffer_size = NULL; libcstring_system_character_t *option_secondary_target_filename = NULL; libcstring_system_character_t *option_sectors_per_chunk = NULL; libcstring_system_character_t *option_size = NULL; libcstring_system_character_t *option_target_filename = NULL; libcstring_system_character_t *program = _LIBCSTRING_SYSTEM_STRING( "ewfacquirestream" ); log_handle_t *log_handle = NULL; libcstring_system_integer_t option = 0; size_t string_length = 0; uint8_t calculate_md5 = 1; uint8_t print_status_information = 1; uint8_t read_error_retries = 2; uint8_t resume_acquiry = 0; uint8_t swap_byte_pairs = 0; uint8_t use_chunk_data_functions = 0; uint8_t verbose = 0; int result = 0; libcnotify_stream_set( stderr, NULL ); libcnotify_verbose_set( 1 ); if( libclocale_initialize( "ewftools", &error ) != 1 ) { fprintf( stderr, "Unable to initialize locale values.\n" ); goto on_error; } if( libcsystem_initialize( _IONBF, &error ) != 1 ) { ewfoutput_version_fprint( stdout, program ); fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } #if defined( WINAPI ) && !defined( __CYGWIN__ ) #if defined( _MSC_VER ) if( _setmode( _fileno( stdin ), _O_BINARY ) == -1 ) #else if( setmode( _fileno( stdin ), _O_BINARY ) == -1 ) #endif { ewfoutput_version_fprint( stdout, program ); fprintf( stderr, "Unable to set stdin to binary mode.\n" ); usage_fprint( stdout ); goto on_error; } #endif while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "A:b:B:c:C:d:D:e:E:f:hl:m:M:N:o:p:P:qsS:t:vVx2:" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: ewfoutput_version_fprint( stdout, program ); fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM "\n", argv[ optind - 1 ] ); usage_fprint( stdout ); goto on_error; case (libcstring_system_integer_t) 'A': option_header_codepage = optarg; break; case (libcstring_system_integer_t) 'b': option_sectors_per_chunk = optarg; break; case (libcstring_system_integer_t) 'B': option_size = optarg; break; case (libcstring_system_integer_t) 'c': option_compression_values = optarg; break; case (libcstring_system_integer_t) 'C': option_case_number = optarg; break; case (libcstring_system_integer_t) 'd': option_additional_digest_types = optarg; break; case (libcstring_system_integer_t) 'D': option_description = optarg; break; case (libcstring_system_integer_t) 'e': option_examiner_name = optarg; break; case (libcstring_system_integer_t) 'E': option_evidence_number = optarg; break; case (libcstring_system_integer_t) 'f': option_format = optarg; break; case (libcstring_system_integer_t) 'h': ewfoutput_version_fprint( stdout, program ); usage_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'l': log_filename = optarg; break; case (libcstring_system_integer_t) 'm': option_media_type = optarg; break; case (libcstring_system_integer_t) 'M': option_media_flags = optarg; break; case (libcstring_system_integer_t) 'N': option_notes = optarg; break; case (libcstring_system_integer_t) 'o': option_offset = optarg; break; case (libcstring_system_integer_t) 'p': option_process_buffer_size = optarg; break; case (libcstring_system_integer_t) 'P': option_bytes_per_sector = optarg; break; case (libcstring_system_integer_t) 'q': print_status_information = 0; break; case (libcstring_system_integer_t) 's': swap_byte_pairs = 1; break; case (libcstring_system_integer_t) 'S': option_maximum_segment_size = optarg; break; case (libcstring_system_integer_t) 't': option_target_filename = optarg; break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': ewfoutput_version_fprint( stdout, program ); ewfoutput_copyright_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'x': use_chunk_data_functions = 1; break; case (libcstring_system_integer_t) '2': option_secondary_target_filename = optarg; break; } } ewfoutput_version_fprint( stdout, program ); libcnotify_verbose_set( verbose ); #if !defined( HAVE_LOCAL_LIBEWF ) libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); #endif if( ( option_target_filename != NULL ) && ( option_secondary_target_filename != NULL ) ) { string_length = libcstring_system_string_length( option_secondary_target_filename ); if( libcstring_system_string_length( option_target_filename ) == string_length ) { if( libcstring_system_string_compare( option_target_filename, option_secondary_target_filename, string_length ) == 0 ) { fprintf( stderr, "Primary and secondary target cannot be the same.\n" ); goto on_error; } } } if( imaging_handle_initialize( &ewfacquirestream_imaging_handle, calculate_md5, use_chunk_data_functions, &error ) != 1 ) { fprintf( stderr, "Unable to create imaging handle.\n" ); goto on_error; } if( option_header_codepage != NULL ) { result = imaging_handle_set_header_codepage( ewfacquirestream_imaging_handle, option_header_codepage, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set header codepage.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported header codepage defaulting to: ascii.\n" ); } } if( option_target_filename != NULL ) { if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_target_filename, &( ewfacquirestream_imaging_handle->target_filename ), &( ewfacquirestream_imaging_handle->target_filename_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set target filename.\n" ); goto on_error; } } else { /* Make sure the target filename is set */ if( imaging_handle_set_string( ewfacquirestream_imaging_handle, _LIBCSTRING_SYSTEM_STRING( "image" ), &( ewfacquirestream_imaging_handle->target_filename ), &( ewfacquirestream_imaging_handle->target_filename_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set target filename.\n" ); goto on_error; } } if( option_secondary_target_filename != NULL ) { if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_secondary_target_filename, &( ewfacquirestream_imaging_handle->secondary_target_filename ), &( ewfacquirestream_imaging_handle->secondary_target_filename_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set secondary target filename.\n" ); goto on_error; } } if( option_case_number == NULL ) { option_case_number = _LIBCSTRING_SYSTEM_STRING( "case_number" ); } if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_case_number, &( ewfacquirestream_imaging_handle->case_number ), &( ewfacquirestream_imaging_handle->case_number_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set case number.\n" ); goto on_error; } if( option_description == NULL ) { option_description = _LIBCSTRING_SYSTEM_STRING( "description" ); } if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_description, &( ewfacquirestream_imaging_handle->description ), &( ewfacquirestream_imaging_handle->description_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set description.\n" ); goto on_error; } if( option_evidence_number == NULL ) { option_evidence_number = _LIBCSTRING_SYSTEM_STRING( "evidence_number" ); } if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_evidence_number, &( ewfacquirestream_imaging_handle->evidence_number ), &( ewfacquirestream_imaging_handle->evidence_number_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set evidence number.\n" ); goto on_error; } if( option_examiner_name == NULL ) { option_examiner_name = _LIBCSTRING_SYSTEM_STRING( "examiner_name" ); } if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_examiner_name, &( ewfacquirestream_imaging_handle->examiner_name ), &( ewfacquirestream_imaging_handle->examiner_name_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set examiner name.\n" ); goto on_error; } if( option_notes == NULL ) { option_notes = _LIBCSTRING_SYSTEM_STRING( "notes" ); } if( imaging_handle_set_string( ewfacquirestream_imaging_handle, option_notes, &( ewfacquirestream_imaging_handle->notes ), &( ewfacquirestream_imaging_handle->notes_size ), &error ) != 1 ) { fprintf( stderr, "Unable to set notes.\n" ); goto on_error; } if( option_format != NULL ) { result = imaging_handle_set_format( ewfacquirestream_imaging_handle, option_format, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set format.\n" ); goto on_error; } else if( ( result == 0 ) || ( ewfacquirestream_imaging_handle->ewf_format == LIBEWF_FORMAT_EWF ) || ( ewfacquirestream_imaging_handle->ewf_format == LIBEWF_FORMAT_ENCASE1 ) || ( ewfacquirestream_imaging_handle->ewf_format == LIBEWF_FORMAT_SMART ) ) { ewfacquirestream_imaging_handle->ewf_format = LIBEWF_FORMAT_ENCASE6; fprintf( stderr, "Unsupported EWF format defaulting to: encase6.\n" ); } } if( option_compression_values != NULL ) { result = imaging_handle_set_compression_values( ewfacquirestream_imaging_handle, option_compression_values, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set compression values.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported compression values defaulting to method: deflate with level: none.\n" ); } } if( option_media_type != NULL ) { result = imaging_handle_set_media_type( ewfacquirestream_imaging_handle, option_media_type, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set media type.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported media type defaulting to: fixed.\n" ); } } if( option_media_flags != NULL ) { result = imaging_handle_set_media_flags( ewfacquirestream_imaging_handle, option_media_flags, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set media flags.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported media flags defaulting to: physical.\n" ); } } if( option_bytes_per_sector != NULL ) { result = imaging_handle_set_bytes_per_sector( ewfacquirestream_imaging_handle, option_bytes_per_sector, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set bytes per sector.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported bytes per sector defaulting to: %" PRIu32 ".\n", ewfacquirestream_imaging_handle->bytes_per_sector ); } } if( option_sectors_per_chunk != NULL ) { result = imaging_handle_set_sectors_per_chunk( ewfacquirestream_imaging_handle, option_sectors_per_chunk, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set sectors per chunk.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported sectors per chunk defaulting to: %" PRIu32 ".\n", ewfacquirestream_imaging_handle->sectors_per_chunk ); } } if( option_maximum_segment_size != NULL ) { result = imaging_handle_set_maximum_segment_size( ewfacquirestream_imaging_handle, option_maximum_segment_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set maximum segment size.\n" ); goto on_error; } else if( result == 0 ) { ewfacquirestream_imaging_handle->maximum_segment_size = EWFCOMMON_DEFAULT_SEGMENT_FILE_SIZE; fprintf( stderr, "Unsupported maximum segment size defaulting to: %" PRIu64 ".\n", ewfacquirestream_imaging_handle->maximum_segment_size ); } } if( option_offset != NULL ) { result = imaging_handle_set_acquiry_offset( ewfacquirestream_imaging_handle, option_offset, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set acquiry offset.\n" ); goto on_error; } else if( result == 0 ) { ewfacquirestream_imaging_handle->acquiry_offset = 0; fprintf( stderr, "Unsupported acquiry offset defaulting to: 0.\n" ); } } if( option_size != NULL ) { result = imaging_handle_set_acquiry_size( ewfacquirestream_imaging_handle, option_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set acquiry size.\n" ); goto on_error; } else if( result == 0 ) { ewfacquirestream_imaging_handle->acquiry_size = 0; fprintf( stderr, "Unsupported acquiry size defaulting to: all bytes.\n" ); } } if( option_process_buffer_size != NULL ) { result = imaging_handle_set_process_buffer_size( ewfacquirestream_imaging_handle, option_process_buffer_size, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set process buffer size.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported process buffer size defaulting to: chunk size.\n" ); } } if( option_additional_digest_types != NULL ) { result = imaging_handle_set_additional_digest_types( ewfacquirestream_imaging_handle, option_additional_digest_types, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set additional digest types.\n" ); goto on_error; } } fprintf( stdout, "Using the following acquiry parameters:\n" ); if( imaging_handle_print_parameters( ewfacquirestream_imaging_handle, 0, read_error_retries, 0, 0, &error ) != 1 ) { fprintf( stderr, "Unable to print acquiry parameters.\n" ); goto on_error; } if( imaging_handle_open_output( ewfacquirestream_imaging_handle, ewfacquirestream_imaging_handle->target_filename, resume_acquiry, &error ) != 1 ) { fprintf( stderr, "Unable to open output.\n" ); goto on_error; } if( ewfacquirestream_imaging_handle->secondary_target_filename != NULL ) { if( imaging_handle_open_secondary_output( ewfacquirestream_imaging_handle, ewfacquirestream_imaging_handle->secondary_target_filename, resume_acquiry, &error ) != 1 ) { fprintf( stderr, "Unable to open secondary output.\n" ); goto on_error; } } if( imaging_handle_set_output_values( ewfacquirestream_imaging_handle, program, _LIBCSTRING_SYSTEM_STRING( LIBEWF_VERSION_STRING ), NULL, NULL, &error ) != 1 ) { fprintf( stderr, "Unable to initialize output settings.\n" ); goto on_error; } if( libcsystem_signal_attach( ewfacquirestream_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( log_filename != NULL ) { if( log_handle_initialize( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to create log handle.\n" ); goto on_error; } if( log_handle_open( log_handle, log_filename, &error ) != 1 ) { fprintf( stderr, "Unable to open log file: %" PRIs_LIBCSTRING_SYSTEM ".\n", log_filename ); goto on_error; } } result = ewfacquirestream_read_input( ewfacquirestream_imaging_handle, 0, swap_byte_pairs, read_error_retries, print_status_information, use_chunk_data_functions, log_handle, &error ); if( result != 1 ) { fprintf( stderr, "Unable to read input.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( log_handle != NULL ) { if( log_handle_close( log_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close log handle.\n" ); goto on_error; } if( log_handle_free( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free log handle.\n" ); goto on_error; } } if( libcsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( imaging_handle_close( ewfacquirestream_imaging_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close output.\n" ); goto on_error; } if( imaging_handle_free( &ewfacquirestream_imaging_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free imaging handle.\n" ); goto on_error; } if( ewfacquirestream_abort != 0 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": ABORTED\n", program ); return( EXIT_FAILURE ); } if( result != 1 ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": FAILURE\n", program ); return( EXIT_FAILURE ); } fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM ": SUCCESS\n", program ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libcnotify_print_error_backtrace( error ); libcerror_error_free( &error ); } if( log_handle != NULL ) { log_handle_close( log_handle, NULL ); log_handle_free( &log_handle, NULL ); } if( ewfacquirestream_imaging_handle != NULL ) { imaging_handle_close( ewfacquirestream_imaging_handle, NULL ); imaging_handle_free( &ewfacquirestream_imaging_handle, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t *option_chunk_size = NULL; libcstring_system_character_t *option_compression_level = NULL; libcstring_system_character_t *option_maximum_segment_size = NULL; libcstring_system_character_t *option_media_size = NULL; libcerror_error_t *error = NULL; libcstring_system_integer_t option = 0; size64_t chunk_size = 0; size64_t maximum_segment_size = 0; size64_t media_size = 0; size_t string_length = 0; uint8_t compression_flags = 0; int8_t compression_level = LIBEWF_COMPRESSION_NONE; while( ( option = libcsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "b:B:c:S:" ) ) ) != (libcstring_system_integer_t) -1 ) { switch( option ) { case (libcstring_system_integer_t) '?': default: fprintf( stderr, "Invalid argument: %" PRIs_LIBCSTRING_SYSTEM ".\n", argv[ optind - 1 ] ); return( EXIT_FAILURE ); case (libcstring_system_integer_t) 'b': option_chunk_size = optarg; break; case (libcstring_system_integer_t) 'B': option_media_size = optarg; break; case (libcstring_system_integer_t) 'c': option_compression_level = optarg; break; case (libcstring_system_integer_t) 'S': option_maximum_segment_size = optarg; break; } } if( optind == argc ) { fprintf( stderr, "Missing EWF image filename.\n" ); return( EXIT_FAILURE ); } #if defined( HAVE_DEBUG_OUTPUT ) && defined( EWF_TEST_WRITE_VERBOSE ) libewf_notify_set_verbose( 1 ); libewf_notify_set_stream( stderr, NULL ); #endif if( option_chunk_size != NULL ) { string_length = libcstring_system_string_length( option_chunk_size ); if( libcsystem_string_decimal_copy_to_64_bit( option_chunk_size, string_length + 1, &chunk_size, &error ) != 1 ) { fprintf( stderr, "Unsupported chunk size.\n" ); goto on_error; } } if( option_compression_level != NULL ) { string_length = libcstring_system_string_length( option_compression_level ); if( string_length != 1 ) { fprintf( stderr, "Unsupported compression level.\n" ); goto on_error; } if( option_compression_level[ 0 ] == (libcstring_system_character_t) 'n' ) { compression_level = LIBEWF_COMPRESSION_NONE; compression_flags = 0; } else if( option_compression_level[ 0 ] == (libcstring_system_character_t) 'e' ) { compression_level = LIBEWF_COMPRESSION_NONE; compression_flags = LIBEWF_COMPRESS_FLAG_USE_EMPTY_BLOCK_COMPRESSION; } else if( option_compression_level[ 0 ] == (libcstring_system_character_t) 'f' ) { compression_level = LIBEWF_COMPRESSION_FAST; compression_flags = 0; } else if( option_compression_level[ 0 ] == (libcstring_system_character_t) 'b' ) { compression_level = LIBEWF_COMPRESSION_BEST; compression_flags = 0; } else { fprintf( stderr, "Unsupported compression level.\n" ); goto on_error; } } if( option_maximum_segment_size != NULL ) { string_length = libcstring_system_string_length( option_maximum_segment_size ); if( libcsystem_string_decimal_copy_to_64_bit( option_maximum_segment_size, string_length + 1, &maximum_segment_size, &error ) != 1 ) { fprintf( stderr, "Unsupported maximum segment size.\n" ); goto on_error; } } if( option_media_size != NULL ) { string_length = libcstring_system_string_length( option_media_size ); if( libcsystem_string_decimal_copy_to_64_bit( option_media_size, string_length + 1, &media_size, &error ) != 1 ) { fprintf( stderr, "Unsupported media size.\n" ); goto on_error; } } if( ewf_test_write( argv[ optind ], media_size, maximum_segment_size, compression_level, compression_flags, &error ) != 1 ) { fprintf( stderr, "Unable to test write.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libewf_error_backtrace_fprint( error, stderr ); libewf_error_free( &error ); } return( EXIT_FAILURE ); }