Пример #1
0
Файл: file.c Проект: anhkgg/temu
/* Opens an EWF file for reading
 */
LIBEWF_HANDLE *libewf_open_read( LIBEWF_HANDLE *handle, const char *filename )
{
	int file_descriptor;
	EWF_FILE_HEADER *file_header;
	uint16_t fields_segment;

	if( handle == NULL )
	{
		LIBEWF_FATAL_PRINT( "libewf_open_read: incorrect handle.\n" );
	}
	file_descriptor = open( filename, O_RDONLY );

	if( file_descriptor == -1 )
	{
		LIBEWF_FATAL_PRINT( "libewf_open: unable to open file: %s\n", filename );
	}
	file_header = ewf_file_header_read( file_descriptor );

	if( file_header == NULL )
	{
		LIBEWF_FATAL_PRINT( "libewf_open_read: incorrect file header in: %s.\n", filename );
	}
	fields_segment = convert_16bit( file_header->fields_segment );

	LIBEWF_VERBOSE_PRINT( "libewf_open_read: added segment file: %s with file descriptor: %d with segment number: %" PRIu16 ".\n", filename, file_descriptor, fields_segment );

	handle->segment_table = libewf_segment_table_set_values( handle->segment_table, fields_segment, filename, file_descriptor );

	ewf_file_header_free( file_header );

	LIBEWF_VERBOSE_PRINT( "libewf_open_read: open successful.\n" );

	return( handle );
}
Пример #2
0
/* Opens EWF file(s)
 * For reading files should contain all filenames that make up an EWF image
 * For writing files should contain the base of the filename, extentions like .e01 will be automatically added
 * Returns a pointer to the new instance of handle, NULL on error
 */
LIBEWF_HANDLE *libewf_open( char * const filenames[], uint16_t file_amount, uint8_t flags )
{
	EWF_FILE_HEADER file_header;

	LIBEWF_INTERNAL_HANDLE *internal_handle = NULL;
	char *error_string                      = NULL;
	uint32_t iterator                       = 0;
	uint16_t fields_segment                 = 0;
	int file_descriptor                     = 0;

	if( ( flags == LIBEWF_OPEN_READ ) || ( flags == LIBEWF_OPEN_READ_WRITE ) )
	{
		/* 1 additional entry required because
		 * entry [ 0 ] is not used for reading
		 */
		internal_handle = libewf_internal_handle_alloc( ( file_amount + 1 ), flags );

		if( internal_handle == NULL )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: unable to create handle.\n" );

			return( NULL );
		}
		if( internal_handle->segment_table == NULL )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: invalid handle - missing segment table.\n" );

			libewf_internal_handle_free( internal_handle );

			return( NULL );
		}
		for( iterator = 0; iterator < file_amount; iterator++ )
		{
			file_descriptor = libewf_common_open( filenames[ iterator ], flags );

			if( file_descriptor == -1 )
			{
				error_string = libewf_common_strerror( errno );

				if( error_string == NULL )
				{
					LIBEWF_WARNING_PRINT( "libewf_open: unable to open file: %s.\n", filenames[ iterator ] );
				}
				else
				{
					LIBEWF_WARNING_PRINT( "libewf_open: unable to open file: %s with error: %s.\n", filenames[ iterator ], error_string );

					libewf_common_free( error_string );
				}
				libewf_internal_handle_free( internal_handle );

				return( NULL );
			}
			if( ewf_file_header_read( &file_header, file_descriptor ) <= -1 )
			{
				LIBEWF_WARNING_PRINT( "libewf_open: invalid file header in: %s.\n", filenames[ iterator ] );

				libewf_internal_handle_free( internal_handle );

				return( NULL );
			}
			if( ewf_file_header_check_signature( file_header.signature ) == 0 )
			{
				LIBEWF_WARNING_PRINT( "libewf_open: file signature does not match.\n" );

				libewf_internal_handle_free( internal_handle );

				return( NULL );
			}
			if( libewf_endian_convert_16bit( &fields_segment, file_header.fields_segment ) != 1 )
			{
				LIBEWF_WARNING_PRINT( "libewf_open: unable to convert fields segment value.\n" );

				libewf_internal_handle_free( internal_handle );

				return( NULL );
			}
			LIBEWF_VERBOSE_PRINT( "libewf_open: added segment file: %s with file descriptor: %d with segment number: %" PRIu16 ".\n",
				filenames[ iterator ], file_descriptor, fields_segment );

			if( libewf_segment_table_set_filename( internal_handle->segment_table, fields_segment, filenames[ iterator ], libewf_common_string_length( filenames[ iterator ] ) ) != 1 )
			{
				LIBEWF_WARNING_PRINT( "libewf_open: unable to set filename in segment table.\n" );

				libewf_internal_handle_free( internal_handle );

				return( NULL );
			}
			if( libewf_segment_table_set_file_descriptor( internal_handle->segment_table, fields_segment, file_descriptor ) != 1 )
			{
				LIBEWF_WARNING_PRINT( "libewf_open: unable to set file descriptor in segment table.\n" );

				libewf_internal_handle_free( internal_handle );

				return( NULL );
			}
		}
		if( libewf_read_build_index( internal_handle ) != 1 )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: unable to create index.\n" );

			libewf_internal_handle_free( internal_handle );

			return( NULL );
		}
	}
	else if( flags == LIBEWF_OPEN_WRITE )
	{
		/* Allocate 2 entries
		 * entry [ 0 ] is used for the base filename
		 */
		internal_handle = libewf_internal_handle_alloc( 1, flags );

		if( internal_handle == NULL )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: unable to create handle.\n" );

			return( NULL );
		}
		if( internal_handle->segment_table == NULL )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: invalid handle - missing segment table.\n" );

			libewf_internal_handle_free( internal_handle );

			return( NULL );
		}
		if( libewf_segment_table_set_filename( internal_handle->segment_table, 0, filenames[ iterator ], libewf_common_string_length( filenames[ iterator ] ) ) != 1 )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: unable to set filename in segment table.\n" );

			libewf_internal_handle_free( internal_handle );

			return( NULL );
		}
		if( libewf_segment_table_set_file_descriptor( internal_handle->segment_table, 0, -1 ) != 1 )
		{
			LIBEWF_WARNING_PRINT( "libewf_open: unable to set file descriptor in segment table.\n" );

			libewf_internal_handle_free( internal_handle );

			return( NULL );
		}
	}
	else
	{
		LIBEWF_WARNING_PRINT( "libewf_open: unsupported flags.\n" );

		return( NULL );
	}
	LIBEWF_VERBOSE_PRINT( "libewf_open: open successful.\n" );

	return( (LIBEWF_HANDLE *) internal_handle );
}