libewf_file.c

来自「sleuthit-2.09 一个磁盘的工具集」· C语言 代码 · 共 1,186 行 · 第 1/3 页

C
1,186
字号
		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 );}#endif/* Closes the EWF handle and frees memory used within the handle * Returns 1 if successful, or -1 on error */int8_t libewf_close( LIBEWF_HANDLE *handle ){	LIBEWF_INTERNAL_HANDLE *internal_handle = NULL;#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )	wchar_t *filename                       = NULL;#else	char *filename                          = NULL;#endif	uint16_t iterator                       = 0;	int8_t result                           = 0;	if( handle == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_close: invalid handle.\n" );		return( -1 );	}	internal_handle = (LIBEWF_INTERNAL_HANDLE *) handle;/*	if( ( internal_handle->write != NULL ) && ( internal_handle->write->write_finalized == 0 ) )	{		LIBEWF_VERBOSE_PRINT( "libewf_close: write has not been finalized.\n" );		libewf_write_finalize( handle );	}*/	for( iterator = 0; iterator < internal_handle->segment_table->amount; iterator++ )	{		if( internal_handle->segment_table->file_descriptor[ iterator ] > 0 )		{			if( libewf_common_close( internal_handle->segment_table->file_descriptor[ iterator ] ) != 0 )			{#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )				filename = libewf_segment_table_wide_get_filename( internal_handle->segment_table, iterator );#else				filename = libewf_segment_table_get_filename( internal_handle->segment_table, iterator );#endif				if( filename == NULL )				{					LIBEWF_WARNING_PRINT( "libewf_close: unable to close segment file: %" PRIu32 ".\n",						iterator );				}				else				{#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )					LIBEWF_WARNING_PRINT( "libewf_close: unable to close segment file: %" PRIu32 " (%ls).\n",						iterator, filename );#else					LIBEWF_WARNING_PRINT( "libewf_close: unable to close segment file: %" PRIu32 " (%s).\n",						iterator, filename );#endif				}				result = -1;			}		}	}	libewf_internal_handle_free( internal_handle );	return( result );}/* Seeks a certain chunk offset within the EWF file(s) * Returns the segment file offset if seek is successful, or -1 on error */off_t libewf_seek_chunk( LIBEWF_INTERNAL_HANDLE *internal_handle, uint32_t chunk ){	off_t offset            = 0;	uint16_t segment_number = 0;	int file_descriptor     = 0;	if( internal_handle == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: invalid handle.\n" );		return( -1 );	}	if( internal_handle->segment_table == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: invalid handle - missing segment table.\n" );		return( -1 );	}	if( internal_handle->offset_table == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: invalid handle - missing offset table.\n" );		return( -1 );	}	if( internal_handle->chunk_cache == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: invalid handle - missing chunk cache.\n" );		return( -1 );	}	if( internal_handle->index_build == 0 )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: index was not build.\n" );		return( -1 );	}	if( chunk >= internal_handle->offset_table->amount )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: chunk: %" PRIu32 " not in offset table.\n", chunk );		return( -1 );	}	file_descriptor = internal_handle->offset_table->file_descriptor[ chunk ];	offset          = internal_handle->offset_table->offset[ chunk ];	segment_number  = internal_handle->offset_table->segment_number[ chunk ];	LIBEWF_VERBOSE_PRINT( "libewf_seek_chunk: seek file descriptor: %d, for segment: %" PRIu16 " for offset: %jd.\n",	                      file_descriptor, segment_number, offset );	if( offset > (off_t) INT32_MAX )	{		LIBEWF_WARNING_PRINT( "libewf_seek_chunk: invalid chunk offset only values below 2^32 are supported.\n" );		return( -1 );	}	if( internal_handle->segment_table->file_offset[ segment_number ] != offset )	{		if( libewf_common_lseek( file_descriptor, offset, SEEK_SET ) == -1 )		{			LIBEWF_WARNING_PRINT( "libewf_seek_chunk: cannot find offset: %jd.\n", offset );			return( -1 );		}		internal_handle->segment_table->file_offset[ segment_number ] = offset;	}	internal_handle->current_chunk = chunk;	return( offset );}/* Seeks a certain offset of the media data within the EWF file(s) * It will set the related file offset to the specific chunk offset * Returns the offset if seek is successful, or -1 on error */off_t libewf_seek_offset( LIBEWF_HANDLE *handle, off_t offset ){	LIBEWF_INTERNAL_HANDLE *internal_handle = NULL;	uint32_t chunk                          = 0;	uint32_t chunk_offset                   = 0;	if( handle == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: invalid handle.\n" );		return( -1 );	}	internal_handle = (LIBEWF_INTERNAL_HANDLE *) handle;	if( internal_handle->media == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: invalid handle - missing subhandle media.\n" );		return( -1 );	}	if( internal_handle->offset_table == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: invalid handle - missing offset table.\n" );		return( -1 );	}	if( internal_handle->chunk_cache == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: invalid handle - missing chunk cache.\n" );		return( -1 );	}	if( internal_handle->index_build == 0 )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: index was not build.\n" );		return( -1 );	}	if( offset <= -1 )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: invalid offset value cannot be negative.\n" );		return( -1 );	}	if( offset >= (off_t) internal_handle->media->media_size )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: attempting to read past the end of the file.\n" );		return( -1 );	}	/* Determine the chunk that is requested	 */	chunk = (uint32_t) ( offset / internal_handle->media->chunk_size );	if( libewf_seek_chunk( internal_handle, chunk ) == -1 )	{		LIBEWF_WARNING_PRINT( "libewf_seek_offset: unable to seek chunk offset.\n" );		return( -1 );	}	/* Determine the offset within the decompressed chunk that is requested	 */	chunk_offset = (uint32_t) ( offset % internal_handle->media->chunk_size );	internal_handle->current_chunk_offset = chunk_offset;	return( offset );}/* Returns the amount of bytes per sector from the media information, 0 if not set, -1 on error */int32_t libewf_get_bytes_per_sector( LIBEWF_HANDLE *handle ){	return( libewf_internal_handle_get_media_bytes_per_sector( (LIBEWF_INTERNAL_HANDLE *) handle ) );}/* Returns the amount of sectors from the media information, 0 if not set, -1 on error */int32_t libewf_get_amount_of_sectors( LIBEWF_HANDLE *handle ){	return( libewf_internal_handle_get_media_amount_of_sectors( (LIBEWF_INTERNAL_HANDLE *) handle ) );}/* Returns the chunk size from the media information, 0 if not set, -1 on error */int32_t libewf_get_chunk_size( LIBEWF_HANDLE *handle ){	return( libewf_internal_handle_get_media_chunk_size( (LIBEWF_INTERNAL_HANDLE *) handle ) );}/* Returns the error granularity from the media information, 0 if not set, -1 on error */int32_t libewf_get_error_granularity( LIBEWF_HANDLE *handle ){	return( libewf_internal_handle_get_media_error_granularity( (LIBEWF_INTERNAL_HANDLE *) handle ) );}/* Returns the compression level value, or -1 on error */int8_t libewf_get_compression_level( LIBEWF_HANDLE *handle ){	return( libewf_internal_handle_get_compression_level( (LIBEWF_INTERNAL_HANDLE *) handle ) );}/* Returns the size of the contained media data, 0 if not set, -1 on error */int64_t libewf_get_media_size( LIBEWF_HANDLE *handle ){	return( libewf_internal_handle_get_media_size( (LIBEWF_INTERNAL_HANDLE *) handle ) );}/* Returns the media type value, or -1 on error */int8_t libewf_get_media_type( LIBEWF_HANDLE *handle ){

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?