Esempio n. 1
0
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 );
}
Esempio n. 2
0
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 );
}