/* Signal handler for ewfinfo */ void ewfinfo_signal_handler( libsystem_signal_t signal LIBSYSTEM_ATTRIBUTE_UNUSED ) { liberror_error_t *error = NULL; static char *function = "ewfinfo_signal_handler"; LIBSYSTEM_UNREFERENCED_PARAMETER( signal ) ewfinfo_abort = 1; if( ( ewfinfo_info_handle != NULL ) && ( info_handle_signal_abort( ewfinfo_info_handle, &error ) != 1 ) ) { libsystem_notify_printf( "%s: unable to signal info handle to abort.\n", function ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } /* Force stdin to close otherwise any function reading it will remain blocked */ if( libsystem_file_io_close( 0 ) != 0 ) { libsystem_notify_printf( "%s: unable to close stdin.\n", function ); } }
/* Signal handler for esedbinfo */ void esedbinfo_signal_handler( libsystem_signal_t signal ) { liberror_error_t *error = NULL; static char *function = "esedbinfo_signal_handler"; esedbinfo_abort = 1; if( esedbinfo_info_handle != NULL ) { if( info_handle_signal_abort( esedbinfo_info_handle, &error ) != 1 ) { libsystem_notify_printf( "%s: unable to signal info handle to abort.\n", function ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } } /* Force stdin to close otherwise any function reading it will remain blocked */ if( libsystem_file_io_close( 0 ) != 0 ) { libsystem_notify_printf( "%s: unable to close stdin.\n", function ); } }
/* Checks if the file has an EWF signature * Returns a Python object if successful or NULL on error */ PyObject *pyewf_check_file_signature( PyObject *self, PyObject *arguments, PyObject *keywords ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; static char *function = "pyewf_check_file_signature"; static char *keyword_list[] = { "filename", NULL }; const char *filename = NULL; int result = 0; if( PyArg_ParseTupleAndKeywords( arguments, keywords, "|s", keyword_list, &filename ) == 0 ) { return( NULL ); } result = libewf_check_file_signature( filename, &error ); if( result == -1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to check file signature.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to check file signature.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } if( result != 0 ) { return( Py_True ); } return( Py_False ); }
/* Closes EWF file(s) * Returns a Python object if successful or NULL on error */ PyObject *pyewf_handle_close( pyewf_handle_t *pyewf_handle ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; static char *function = "pyewf_handle_close"; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle.", function ); return( NULL ); } if( pyewf_handle->handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle - missing libewf handle.", function ); return( NULL ); } if( libewf_handle_close( pyewf_handle->handle, &error ) != 0 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to close handle.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to close handle.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } return( Py_None ); }
/* Intializes a handle object * Returns 0 if successful or -1 on error */ int pyewf_handle_init( pyewf_handle_t *pyewf_handle ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; static char *function = "pyewf_handle_init"; liberror_error_t *error = NULL; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid handle.", function ); return( -1 ); } /* Make sure libewf handle is set to NULL */ pyewf_handle->handle = NULL; if( libewf_handle_initialize( &( pyewf_handle->handle ), &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to initialize handle.", function ); } else { PyErr_Format( PyExc_MemoryError, "%s: unable to initialize handle.\n%s", function, error_string ); } liberror_error_free( &error ); return( -1 ); } return( 0 ); }
/* Tests libewf_handle_seek_offset * Returns 1 if successful, 0 if not or -1 on error */ int ewf_test_seek_offset( libewf_handle_t *handle, off64_t input_offset, int input_whence, off64_t expected_offset, liberror_error_t **error ) { static char *function = "ewf_test_seek_offset"; off64_t result_offset = 0; result_offset = libewf_handle_seek_offset( handle, input_offset, input_whence, error ); if( result_offset != expected_offset ) { if( result_offset == -1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_IO, LIBERROR_IO_ERROR_SEEK_FAILED, "%s: unable to seek offset: %" PRIi64 ".", function, input_offset ); return( -1 ); } fprintf( stderr, "%s: unexpected result offset: %" PRIi64 "\n", function, result_offset ); return( 0 ); } if( result_offset == -1 ) { liberror_error_free( error ); } return( 1 ); }
/* Retrieves the current offset in the media data * Returns a Python object holding the offset if successful or NULL on error */ PyObject *pyewf_handle_get_offset( pyewf_handle_t *pyewf_handle ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; static char *function = "pyewf_handle_get_offset"; off64_t current_offset = 0; if( libewf_handle_get_offset( pyewf_handle->handle, ¤t_offset, &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve offset.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve offset.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } return( PyLong_FromLongLong( current_offset ) ); }
/* Free an error and its elements */ void libmfdata_error_free( libmfdata_error_t **error ) { liberror_error_free( (liberror_error_t **) error ); }
/* Free an error and its elements */ void libewf_error_free( libewf_error_t **error ) { liberror_error_free( (liberror_error_t **) error ); }
/* Globs filenames according to the EWF segment file naming schema * Returns a Python object if successful or NULL on error */ PyObject *pyewf_glob( PyObject *self, PyObject *arguments, PyObject *keywords ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; char **filenames = NULL; liberror_error_t *error = NULL; PyObject *list_object = NULL; PyObject *string_object = NULL; static char *function = "pyewf_glob"; static char *keyword_list[] = { "filename", NULL }; const char *errors = NULL; const char *filename = NULL; size_t filename_length = 0; int filename_index = 0; int number_of_filenames = 0; if( PyArg_ParseTupleAndKeywords( arguments, keywords, "|s", keyword_list, &filename ) == 0 ) { return( NULL ); } filename_length = libcstring_narrow_string_length( filename ); if( libewf_glob( filename, filename_length, LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to glob filenames.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to glob filenames.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } list_object = PyList_New( (Py_ssize_t) number_of_filenames ); for( filename_index = 0; filename_index < number_of_filenames; filename_index++ ) { filename_length = libcstring_narrow_string_length( filenames[ filename_index ] ); string_object = PyUnicode_DecodeUTF8( filenames[ filename_index ], filename_length, errors ); if( string_object == NULL ) { PyErr_Format( PyExc_IOError, "%s: unable to convert UTF-8 filename: %d into Unicode.", function, filename_index ); libewf_glob_free( filenames, number_of_filenames, NULL ); Py_DecRef( list_object ); return( NULL ); } if( PyList_SetItem( list_object, (Py_ssize_t) filename_index, string_object ) != 0 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to set filename: %d in list.", function, filename_index ); libewf_glob_free( filenames, number_of_filenames, NULL ); Py_DecRef( string_object ); Py_DecRef( list_object ); return( NULL ); } } if( libewf_glob_free( filenames, number_of_filenames, &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to free globbed filenames.", function ); } else { PyErr_Format( PyExc_MemoryError, "%s: unable to free globbed filenames.\n%s", function, error_string ); } liberror_error_free( &error ); Py_DecRef( list_object ); return( NULL ); } return( list_object ); }
int main( int argc, char * const argv[] ) #endif { libesedb_error_t *error = NULL; libcstring_system_character_t *source = NULL; char *program = "esedbinfo"; libcstring_system_integer_t option = 0; int verbose = 0; libsystem_notify_set_stream( stderr, NULL ); libsystem_notify_set_verbose( 1 ); if( libsystem_initialize( "esedbtools", _IONBF, &error ) != 1 ) { fprintf( stderr, "Unable to initialize system values.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); return( EXIT_FAILURE ); } esedboutput_version_fprint( stdout, program ); while( ( option = libsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "hvV" ) ) ) != (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 ); return( EXIT_FAILURE ); case (libcstring_system_integer_t) 'h': usage_fprint( stdout ); return( EXIT_SUCCESS ); case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': esedboutput_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; libsystem_notify_set_verbose( verbose ); libesedb_notify_set_stream( stderr, NULL ); libesedb_notify_set_verbose( verbose ); if( info_handle_initialize( &esedbinfo_info_handle, &error ) != 1 ) { fprintf( stderr, "Unable to initialize info handle.\n" ); goto on_error; } if( info_handle_open( esedbinfo_info_handle, source, &error ) != 1 ) { fprintf( stderr, "Unable to open: %" PRIs_LIBCSTRING_SYSTEM ".\n", source ); goto on_error; } if( info_handle_file_fprint( esedbinfo_info_handle, &error ) != 1 ) { fprintf( stderr, "Unable to print file information.\n" ); goto on_error; } if( info_handle_close( esedbinfo_info_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close info handle.\n" ); goto on_error; } if( info_handle_free( &esedbinfo_info_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free info handle.\n" ); goto on_error; } return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } if( esedbinfo_info_handle != NULL ) { info_handle_free( &esedbinfo_info_handle, NULL ); } return( EXIT_FAILURE ); }
int main( int argc, char * const argv[] ) #endif { libcstring_system_character_t * const *argv_filenames = NULL; #if !defined( LIBSYSTEM_HAVE_GLOB ) libsystem_glob_t *glob = NULL; #endif liberror_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; libsystem_notify_set_stream( stderr, NULL ); libsystem_notify_set_verbose( 1 ); if( libsystem_initialize( "ewftools", _IONBF, &error ) != 1 ) { ewfoutput_version_fprint( stderr, program ); fprintf( stderr, "Unable to initialize system values.\n" ); goto on_error; } while( ( option = libsystem_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; } libsystem_notify_set_verbose( verbose ); libewf_notify_set_verbose( verbose ); libewf_notify_set_stream( stderr, NULL ); 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( LIBSYSTEM_HAVE_GLOB ) if( libsystem_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( libsystem_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; } argv_filenames = glob->result; number_of_filenames = glob->number_of_results; #else argv_filenames = &( argv[ optind ] ); number_of_filenames = argc - optind; #endif if( libsystem_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" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } result = info_handle_open_input( ewfinfo_info_handle, argv_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( LIBSYSTEM_HAVE_GLOB ) if( libsystem_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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" ); libsystem_notify_print_error_backtrace( error ); liberror_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( libsystem_signal_detach( &error ) != 1 ) { if( print_header != 0 ) { ewfoutput_version_fprint( stderr, program ); print_header = 0; } fprintf( stderr, "Unable to detach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_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 ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } if( ewfinfo_info_handle != NULL ) { info_handle_free( &ewfinfo_info_handle, NULL ); } #if !defined( LIBSYSTEM_HAVE_GLOB ) if( glob != NULL ) { libsystem_glob_free( &glob, NULL ); } #endif return( EXIT_FAILURE ); }
/* Tests reading and writing a buffers at a specific offset * Returns 1 if successful, 0 if not or -1 on error */ int ewf_test_read_write_buffer_at_offset( libewf_handle_t *handle, off64_t input_offset, int input_whence, size64_t input_size, off64_t expected_offset, size64_t expected_size ) { liberror_error_t *error = NULL; uint8_t *buffer = NULL; const char *whence_string = NULL; int result = 0; if( input_whence == SEEK_CUR ) { whence_string = "SEEK_CUR"; } else if( input_whence == SEEK_END ) { whence_string = "SEEK_END"; } else if( input_whence == SEEK_SET ) { whence_string = "SEEK_SET"; } else { whence_string = "UNKNOWN"; } fprintf( stdout, "Testing reading and writing range with offset: %" PRIi64 ", whence: %s and size: %" PRIu64 "\t", input_offset, whence_string, input_size ); buffer = (uint8_t *) memory_allocate( EWF_TEST_BUFFER_SIZE ); result = ewf_test_seek_offset( handle, input_offset, input_whence, expected_offset, &error ); if( result == 1 ) { if( input_offset >= 0 ) { result = ewf_test_read_write_buffer( handle, buffer, EWF_TEST_BUFFER_SIZE, input_size, expected_size, &error ); } } memory_free( buffer ); if( result != 0 ) { fprintf( stdout, "(PASS)" ); } else { fprintf( stdout, "(FAIL)" ); } fprintf( stdout, "\n" ); if( result == -1 ) { liberror_error_backtrace_fprint( error, stderr ); liberror_error_free( &error ); } return( result ); }
/* Writes a buffer of media data to EWF file(s) * Returns a Python object holding the data if successful or NULL on error */ PyObject *pyewf_handle_write_buffer( pyewf_handle_t *pyewf_handle, PyObject *arguments, PyObject *keywords ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; PyObject *result_data = NULL; static char *function = "pyewf_handle_write_buffer"; static char *keyword_list[] = { "size", NULL }; ssize_t write_count = 0; int write_size = -1; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle.", function ); return( NULL ); } if( pyewf_handle->handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle - missing libewf handle.", function ); return( NULL ); } if( PyArg_ParseTupleAndKeywords( arguments, keywords, "|i", keyword_list, &write_size ) == 0 ) { return( NULL ); } if( write_size < 0 ) { PyErr_Format( PyExc_ValueError, "%s: invalid argument read size value less than zero.", function ); return( NULL ); } /* Make sure the data fits into a memory buffer */ if( write_size > INT_MAX ) { PyErr_Format( PyExc_ValueError, "%s: invalid argument read size value exceeds maximum.", function ); return( NULL ); } result_data = PyString_FromStringAndSize( NULL, write_size ); write_count = libewf_handle_write_buffer( pyewf_handle->handle, PyString_AsString( result_data ), (size_t) write_size, &error ); if( write_count != (ssize_t) write_size ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to write data.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to write data.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } return( result_data ); }
/* Free an error and its elements */ void libhmac_error_free( libhmac_error_t **error ) { liberror_error_free( (liberror_error_t **) error ); }
/* Open EWF file(s) * Returns a Python object if successful or NULL on error */ PyObject *pyewf_handle_open( pyewf_handle_t *pyewf_handle, PyObject *arguments, PyObject *keywords ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; char **filenames = NULL; static char *keyword_list[] = { "filenames", "access_flags", NULL }; PyObject *sequence_object = NULL; PyObject *string_object = NULL; static char *function = "pyewf_handle_open"; size_t filename_length = 0; int access_flags = 0; int filename_index = 0; int number_of_filenames = 0; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle.", function ); return( NULL ); } if( PyArg_ParseTupleAndKeywords( arguments, keywords, "O|i", keyword_list, &sequence_object, &access_flags ) == 0 ) { return( NULL ); } if( PySequence_Check( sequence_object ) == 0 ) { PyErr_Format( PyExc_TypeError, "%s: argument: files must be a list or tuple.", function ); return( NULL ); } number_of_filenames = PySequence_Size( sequence_object ); if( ( number_of_filenames <= 0 ) || ( number_of_filenames > (int) UINT16_MAX ) ) { PyErr_Format( PyExc_ValueError, "%s: invalid number of files.", function ); return( NULL ); } filenames = (char **) memory_allocate( sizeof( char * ) * number_of_filenames ); if( filenames == NULL ) { PyErr_Format( PyExc_MemoryError, "%s: unable to create filenames.", function ); return( NULL ); } if( memory_set( filenames, 0, sizeof( char * ) * number_of_filenames ) == NULL ) { PyErr_Format( PyExc_MemoryError, "%s: unable to clear filenames.", function ); memory_free( filenames ); return( NULL ); } for( filename_index = 0; filename_index < number_of_filenames; filename_index++ ) { string_object = PySequence_GetItem( sequence_object, filename_index ); filename_length = PyString_Size( string_object ); filenames[ filename_index ] = (char *) memory_allocate( sizeof( char ) * ( filename_length + 1 ) ); if( filenames[ filename_index ] == NULL ) { PyErr_Format( PyExc_MemoryError, "%s: unable to create filename: %d.", function, filename_index ); for( ; filename_index > 0; filename_index-- ) { memory_free( filenames[ filename_index - 1 ] ); } memory_free( filenames ); return( NULL ); } if( libcstring_narrow_string_copy( filenames[ filename_index ], PyString_AsString( string_object ), filename_length ) == NULL ) { PyErr_Format( PyExc_MemoryError, "%s: unable to set filename: %d.", function, filename_index ); for( ; filename_index > 0; filename_index-- ) { memory_free( filenames[ filename_index - 1 ] ); } memory_free( filenames ); return( NULL ); } ( filenames[ filename_index ] )[ filename_length ] = 0; Py_DecRef( string_object ); } if( libewf_handle_open( pyewf_handle->handle, filenames, number_of_filenames, access_flags, &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to open handle.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to open handle.\n%s", function, error_string ); } liberror_error_free( &error ); for( filename_index = 0; filename_index < number_of_filenames; filename_index++ ) { memory_free( filenames[ filename_index ] ); } memory_free( filenames ); return( NULL ); } for( filename_index = 0; filename_index < number_of_filenames; filename_index++ ) { memory_free( filenames[ filename_index ] ); } memory_free( filenames ); return( Py_None ); }
/* Free an error and its elements */ void libmapidb_error_free( libmapidb_error_t **error ) { liberror_error_free( (liberror_error_t **) error ); }
/* Reads a certain chunk of data * Adds a checksum error if the data is corrupted * Returns 1 if successful or -1 on error */ int libewf_read_io_handle_read_chunk_data( libewf_read_io_handle_t *read_io_handle, libbfio_pool_t *file_io_pool, libewf_media_values_t *media_values, libmfdata_list_t *chunk_table_list, libmfcache_cache_t *chunk_table_cache, int chunk_index, off64_t chunk_offset, libewf_chunk_data_t **chunk_data, liberror_error_t **error ) { static char *function = "libewf_read_io_handle_read_chunk_data"; size_t chunk_size = 0; uint64_t start_sector = 0; uint32_t number_of_sectors = 0; int result = 0; if( read_io_handle == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid read IO handle.", function ); return( -1 ); } if( media_values == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid media values.", function ); return( -1 ); } if( chunk_data == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid chunk data.", function ); return( -1 ); } /* This function will expand element groups */ result = libmfdata_list_get_element_value_by_index( chunk_table_list, file_io_pool, chunk_table_cache, chunk_index, (intptr_t **) chunk_data, 0, error ); if( result != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve chunk data: %d.", function, chunk_index ); #if defined( HAVE_DEBUG_OUTPUT ) if( libnotify_verbose != 0 ) { if( ( error != NULL ) && ( *error != NULL ) ) { libnotify_print_error_backtrace( *error ); } } #endif liberror_error_free( error ); chunk_size = media_values->chunk_size; if( (size64_t) ( chunk_offset + chunk_size ) > media_values->media_size ) { chunk_size = (size_t) ( media_values->media_size - chunk_offset ); } if( libewf_chunk_data_initialize( chunk_data, chunk_size, error ) != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create chunk data.", function ); return( -1 ); } if( *chunk_data == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: missing chunk data: %d.", function, chunk_index ); return( -1 ); } ( *chunk_data )->data_size = chunk_size; ( *chunk_data )->is_corrupt = 1; if( memory_set( ( *chunk_data )->data, 0, ( *chunk_data )->data_size ) == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to zero chunk data.", function ); libewf_chunk_data_free( chunk_data, NULL ); return( -1 ); } if( libmfdata_list_set_element_by_index( chunk_table_list, chunk_index, -1, chunk_offset, chunk_size, 0, error ) != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to set chunk: %d in table.", function, chunk_index ); libewf_chunk_data_free( chunk_data, NULL ); return( -1 ); } if( libmfdata_list_set_element_value_by_index( chunk_table_list, chunk_table_cache, chunk_index, (intptr_t *) *chunk_data, (int (*)(intptr_t **, liberror_error_t **)) &libewf_chunk_data_free, LIBMFDATA_LIST_ELEMENT_VALUE_FLAG_MANAGED, error ) != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to set chunk data: %d as element value.", function, chunk_index ); libewf_chunk_data_free( chunk_data, NULL ); return( -1 ); } } else { if( *chunk_data == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: missing chunk data: %d.", function, chunk_index ); return( -1 ); } if( libewf_chunk_data_unpack( *chunk_data, media_values->chunk_size, error ) != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_GENERIC, "%s: unable to unpack chunk data: %d.", function, chunk_index ); return( -1 ); } if( ( *chunk_data )->is_corrupt != 0 ) { if( read_io_handle->zero_on_error != 0 ) { if( memory_set( ( *chunk_data )->data, 0, ( *chunk_data )->data_size ) == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to zero chunk data.", function ); return( -1 ); } } } } if( ( *chunk_data )->is_corrupt != 0 ) { /* Add checksum error */ start_sector = (uint64_t) chunk_index * (uint64_t) media_values->sectors_per_chunk; number_of_sectors = media_values->sectors_per_chunk; if( ( start_sector + number_of_sectors ) > (uint64_t) media_values->number_of_sectors ) { number_of_sectors = (uint32_t) ( (uint64_t) media_values->number_of_sectors - start_sector ); } if( libewf_sector_list_append_sector( read_io_handle->checksum_errors, start_sector, number_of_sectors, 1, error ) != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_APPEND_FAILED, "%s: unable to append checksum error to sectors list.", function ); return( -1 ); } } return( 1 ); }
/* Seeks a certain offset in the media data * Returns a Python object holding the offset if successful or NULL on error */ PyObject *pyewf_handle_seek_offset( pyewf_handle_t *pyewf_handle, PyObject *arguments, PyObject *keywords ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; static char *function = "pyewf_handle_seek_offset"; static char *keyword_list[] = { "offset", "whence", NULL }; off64_t offset = 0; int whence = 0; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle.", function ); return( NULL ); } if( pyewf_handle->handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid pyewf handle - missing libewf handle.", function ); return( NULL ); } if( PyArg_ParseTupleAndKeywords( arguments, keywords, "L|i", keyword_list, &offset, &whence ) == 0 ) { return( NULL ); } if( libewf_handle_seek_offset( pyewf_handle->handle, offset, whence, &error ) < 0 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to seek offset.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to seek offset.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } return( Py_None ); }
/* Packs the chunk data * This function either adds the checksum or compresses the chunk data * Returns 1 if successful or -1 on error */ int libewf_chunk_data_pack( libewf_chunk_data_t *chunk_data, int8_t compression_level, uint8_t compression_flags, uint8_t ewf_format, size32_t chunk_size, const uint8_t *compressed_zero_byte_empty_block, size_t compressed_zero_byte_empty_block_size, liberror_error_t **error ) { static char *function = "libewf_chunk_data_pack"; void *reallocation = NULL; uint32_t calculated_checksum = 0; int is_empty_zero_block = 0; int result = 0; if( chunk_data == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid chunk data.", function ); return( -1 ); } if( chunk_data->data == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid chunk data - missing data.", function ); return( -1 ); } if( chunk_data->is_packed != 0 ) { return( 1 ); } chunk_data->is_compressed = 0; if( ( ewf_format != EWF_FORMAT_S01 ) && ( compression_flags & LIBEWF_FLAG_COMPRESS_EMPTY_BLOCK ) != 0 ) { result = libewf_empty_block_test( chunk_data->data, chunk_data->data_size, error ); if( result == -1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to determine if chunk data is empty an empty block.", function ); return( -1 ); } else if( result == 1 ) { if( compression_level == EWF_COMPRESSION_NONE ) { compression_level = EWF_COMPRESSION_DEFAULT; } if( ( chunk_data->data )[ 0 ] == 0 ) { is_empty_zero_block = 1; } } else { compression_level = EWF_COMPRESSION_NONE; } } if( ( ewf_format == EWF_FORMAT_S01 ) || ( compression_level != EWF_COMPRESSION_NONE ) ) { chunk_data->compressed_data_size = 2 * chunk_data->data_size; chunk_data->compressed_data = (uint8_t *) memory_allocate( sizeof( uint8_t ) * chunk_data->compressed_data_size ); if( chunk_data->compressed_data == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create compressed data.", function ); return( -1 ); } if( ( is_empty_zero_block != 0 ) && ( chunk_data->data_size == (size_t) chunk_size ) && ( compressed_zero_byte_empty_block != NULL ) ) { if( memory_copy( chunk_data->compressed_data, compressed_zero_byte_empty_block, compressed_zero_byte_empty_block_size ) == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_COPY_FAILED, "%s: unable to copy compressed zero byte empty block to compressed chunk buffer.", function ); return( -1 ); } chunk_data->compressed_data_size = compressed_zero_byte_empty_block_size; } else { result = libewf_compress( chunk_data->compressed_data, &( chunk_data->compressed_data_size ), chunk_data->data, chunk_data->data_size, compression_level, error ); /* Check if the compressed buffer was too small * and a new compressed data size buffer was passed back */ if( ( result == -1 ) && ( chunk_data->compressed_data_size > 0 ) ) { liberror_error_free( error ); reallocation = memory_reallocate( chunk_data->compressed_data, sizeof( uint8_t ) * chunk_data->compressed_data_size ); if( reallocation == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to resize compressed data.", function ); return( -1 ); } chunk_data->compressed_data = (uint8_t *) reallocation; result = libewf_compress( chunk_data->compressed_data, &( chunk_data->compressed_data_size ), chunk_data->data, chunk_data->data_size, compression_level, error ); } if( result != 1 ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_COMPRESSION, LIBERROR_COMPRESSION_ERROR_COMPRESS_FAILED, "%s: unable to compress chunk data.", function ); return( -1 ); } } if( ( ewf_format == EWF_FORMAT_S01 ) || ( chunk_data->compressed_data_size < chunk_data->data_size ) ) { memory_free( chunk_data->data ); chunk_data->data = chunk_data->compressed_data; chunk_data->data_size = chunk_data->compressed_data_size; chunk_data->compressed_data = NULL; chunk_data->compressed_data_size = 0; chunk_data->is_compressed = 1; } } if( chunk_data->is_compressed == 0 ) { if( ( chunk_data->data_size + sizeof( uint32_t ) ) > chunk_data->allocated_data_size ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_RUNTIME, LIBERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, "%s: chunk data size value out of bounds.", function ); return( -1 ); } calculated_checksum = ewf_checksum_calculate( chunk_data->data, chunk_data->data_size, 1 ); byte_stream_copy_from_uint32_little_endian( &( ( chunk_data->data )[ chunk_data->data_size ] ), calculated_checksum ); chunk_data->data_size += sizeof( uint32_t ); } chunk_data->is_packed = 1; return( 1 ); }
/* Tests reading and writing chunks at a specific offset * Returns 1 if successful, 0 if not or -1 on error */ int ewf_test_read_write_chunk_at_offset( libewf_handle_t *handle, size32_t chunk_size, off64_t input_offset, int input_whence, size64_t input_size, off64_t expected_offset, size64_t expected_size ) { liberror_error_t *error = NULL; uint8_t *chunk_buffer = NULL; uint8_t *data_buffer = NULL; const char *whence_string = NULL; size_t chunk_buffer_size = 0; size_t data_buffer_size = 0; int result = 0; if( chunk_size == 0 ) { return( -1 ); } #if SIZEOF_SIZE_T < 8 if( (size_t) chunk_size > (size_t) SSIZE_MAX ) { return( -1 ); } #endif if( input_whence == SEEK_CUR ) { whence_string = "SEEK_CUR"; } else if( input_whence == SEEK_END ) { whence_string = "SEEK_END"; } else if( input_whence == SEEK_SET ) { whence_string = "SEEK_SET"; } else { whence_string = "UNKNOWN"; } fprintf( stdout, "Testing reading range and writing with offset: %" PRIi64 ", whence: %s and size: %" PRIu64 "\t", input_offset, whence_string, input_size ); result = ewf_test_seek_offset( handle, input_offset, input_whence, expected_offset, &error ); data_buffer_size = chunk_size; data_buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * data_buffer_size ); /* The compressed data can become larger than the uncompressed data */ chunk_buffer_size = chunk_size * 2; chunk_buffer = (uint8_t *) memory_allocate( sizeof( uint8_t ) * chunk_buffer_size ); if( result == 1 ) { if( input_offset >= 0 ) { result = ewf_test_read_write_chunk( handle, data_buffer, data_buffer_size, chunk_buffer, chunk_buffer_size, input_size, expected_size, &error ); } } memory_free( chunk_buffer ); memory_free( data_buffer ); if( result != 0 ) { fprintf( stdout, "(PASS)" ); } else { fprintf( stdout, "(FAIL)" ); } fprintf( stdout, "\n" ); if( result == -1 ) { liberror_error_backtrace_fprint( error, stderr ); liberror_error_free( &error ); } return( result ); }
/* Free an error and its elements */ void libodraw_error_free( libodraw_error_t **error ) { liberror_error_free( (liberror_error_t **) error ); }
/* Frees a handle object */ void pyewf_handle_free( pyewf_handle_t *pyewf_handle ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; liberror_error_t *error = NULL; static char *function = "pyewf_handle_free"; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid handle.", function ); return; } if( pyewf_handle->ob_type == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid handle - missing ob_type.", function ); return; } if( pyewf_handle->ob_type->tp_free == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid handle - invalid ob_type - missing tp_free.", function ); return; } if( pyewf_handle->handle == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid handle - missing libewf handle.", function ); return; } if( libewf_handle_free( &( pyewf_handle->handle ), &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to free handle.", function ); } else { PyErr_Format( PyExc_MemoryError, "%s: unable to free handle.\n%s", function, error_string ); } liberror_error_free( &error ); } pyewf_handle->ob_type->tp_free( (PyObject*) pyewf_handle ); }
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 { libcstring_system_character_t *log_filename = NULL; libcstring_system_character_t *option_ascii_codepage = NULL; libcstring_system_character_t *option_export_mode = NULL; libcstring_system_character_t *option_table_name = NULL; libcstring_system_character_t *option_target_path = NULL; libcstring_system_character_t *path_separator = NULL; libcstring_system_character_t *source = NULL; liberror_error_t *error = NULL; log_handle_t *log_handle = NULL; char *program = "esedbexport"; size_t source_length = 0; size_t option_table_name_length = 0; libcstring_system_integer_t option = 0; int result = 0; int verbose = 0; libsystem_notify_set_stream( stderr, NULL ); libsystem_notify_set_verbose( 1 ); if( libsystem_initialize( "esedbtools", _IONBF, &error ) != 1 ) { fprintf( stderr, "Unable to initialize system values.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); return( EXIT_FAILURE ); } esedboutput_version_fprint( stdout, program ); while( ( option = libsystem_getopt( argc, argv, _LIBCSTRING_SYSTEM_STRING( "c:hl:m:t:T:vV" ) ) ) != (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 ); return( EXIT_FAILURE ); case (libcstring_system_integer_t) 'c': option_ascii_codepage = 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) 'm': option_export_mode = optarg; break; case (libcstring_system_integer_t) 't': option_target_path = optarg; break; case (libcstring_system_integer_t) 'T': option_table_name = optarg; break; case (libcstring_system_integer_t) 'v': verbose = 1; break; case (libcstring_system_integer_t) 'V': esedboutput_copyright_fprint( stdout ); return( EXIT_SUCCESS ); } } if( optind == argc ) { fprintf( stderr, "Missing source file.\n" ); usage_fprint( stdout ); return( EXIT_FAILURE ); } source = argv[ optind ]; if( option_target_path == NULL ) { source_length = libcstring_system_string_length( source ); path_separator = libcstring_system_string_search_character_reverse( source, (libcstring_system_character_t) LIBSYSTEM_PATH_SEPARATOR, source_length ); if( path_separator == NULL ) { path_separator = source; } else { path_separator++; } option_target_path = path_separator; } if( option_table_name != NULL ) { option_table_name_length = libcstring_system_string_length( option_table_name ); } libsystem_notify_set_verbose( verbose ); libesedb_notify_set_stream( stderr, NULL ); libesedb_notify_set_verbose( verbose ); if( log_handle_initialize( &log_handle, &error ) != 1 ) { fprintf( stderr, "Unable to initialize log handle.\n" ); goto on_error; } if( export_handle_initialize( &esedbexport_export_handle, &error ) != 1 ) { fprintf( stderr, "Unable to initialize export handle.\n" ); goto on_error; } /* TODO esedbexport_export_handle->print_status_information = print_status_information; */ if( option_export_mode != NULL ) { result = export_handle_set_export_mode( esedbexport_export_handle, option_export_mode, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set export mode.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported export mode defaulting to: tables.\n" ); } } if( option_ascii_codepage != NULL ) { result = export_handle_set_ascii_codepage( esedbexport_export_handle, option_ascii_codepage, &error ); if( result == -1 ) { fprintf( stderr, "Unable to set ASCII codepage in export handle.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "Unsupported ASCII codepage defaulting to: windows-1252.\n" ); } } if( export_handle_set_target_path( esedbexport_export_handle, option_target_path, &error ) != 1 ) { fprintf( stderr, "Unable to set target path.\n" ); goto on_error; } result = export_handle_create_items_export_path( esedbexport_export_handle, &error ); if( result == -1 ) { fprintf( stderr, "Unable to create items export path.\n" ); goto on_error; } else if( result == 0 ) { fprintf( stderr, "%" PRIs_LIBCSTRING_SYSTEM " already exists.\n", esedbexport_export_handle->items_export_path ); 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; } fprintf( stdout, "Opening file.\n" ); #ifdef TODO_SIGNAL_ABORT if( libsystem_signal_attach( esedbexport_signal_handler, &error ) != 1 ) { fprintf( stderr, "Unable to attach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } #endif if( export_handle_open( esedbexport_export_handle, source, &error ) != 1 ) { fprintf( stderr, "Unable to open: %" PRIs_LIBCSTRING_SYSTEM ".\n", source ); goto on_error; } /* TODO fprintf( stdout, "Exporting aliases.\n" ); */ result = export_handle_export_file( esedbexport_export_handle, option_table_name, option_table_name_length, log_handle, &error ); if( result == -1 ) { fprintf( stderr, "Unable to export file.\n" ); goto on_error; } #ifdef TODO_SIGNAL_ABORT if( libsystem_signal_detach( &error ) != 1 ) { fprintf( stderr, "Unable to detach signal handler.\n" ); libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } #endif if( export_handle_close( esedbexport_export_handle, &error ) != 0 ) { fprintf( stderr, "Unable to close export handle.\n" ); goto on_error; } if( export_handle_free( &esedbexport_export_handle, &error ) != 1 ) { fprintf( stderr, "Unable to free export handle.\n" ); goto on_error; } 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( esedbexport_abort != 0 ) { fprintf( stdout, "Export aborted.\n" ); return( EXIT_FAILURE ); } /* TODO export FAILED ? */ fprintf( stdout, "Export completed.\n" ); return( EXIT_SUCCESS ); on_error: if( error != NULL ) { libsystem_notify_print_error_backtrace( error ); liberror_error_free( &error ); } if( esedbexport_export_handle != NULL ) { export_handle_free( &esedbexport_export_handle, NULL ); } if( log_handle != NULL ) { log_handle_free( &log_handle, NULL ); } return( EXIT_FAILURE ); }