libewf_file.c

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

C
1,186
字号
/* * libewf file handling * * Copyright (c) 2006-2007, Joachim Metz <forensics@hoffmannbv.nl>, * Hoffmann Investigations. All rights reserved. * * Refer to AUTHORS for acknowledgements. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, *   this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, *   this list of conditions and the following disclaimer in the documentation *   and/or other materials provided with the distribution. * - Neither the name of the creator, related organisations, nor the names of *   its contributors may be used to endorse or promote products derived from *   this software without specific prior written permission. * - All advertising materials mentioning features or use of this software *   must acknowledge the contribution by people stated in the acknowledgements. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER, COMPANY AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include "libewf_includes.h"#if HAVE_UNISTD_H#include <unistd.h>#endif#include <errno.h>#include <libewf/libewf_definitions.h>#include "libewf_common.h"#include "libewf_endian.h"#include "libewf_notify.h"#include "libewf_file.h"#include "libewf_offset_table.h"#include "libewf_read.h"#include "libewf_section_list.h"#include "libewf_segment_table.h"#include "libewf_string.h"#include "libewf_write.h"#include "ewf_compress.h"#include "ewf_crc.h"#include "ewf_digest_hash.h"#include "ewf_file_header.h"#include "ewf_header.h"#include "ewf_section.h"#include "ewf_volume.h"#include "ewf_table.h"/* Return the library version */const LIBEWF_CHAR *libewf_get_version( void ){	return( (const LIBEWF_CHAR *) LIBEWF_VERSION );}#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )/* Detects if a file is an EWF file (check for the EWF file signature) * Returns 1 if true, 0 if not, or -1 on error */int8_t libewf_check_file_signature( const wchar_t *filename ){	uint8_t signature[ 8 ];	wchar_t *error_string = NULL;	ssize_t count         = 0;	int file_descriptor   = 0;	if( filename == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_check_file_signature: invalid filename.\n" );		return( -1 );	}	file_descriptor = libewf_common_wide_open( filename, LIBEWF_OPEN_READ );	if( file_descriptor < 0 )	{		error_string = libewf_common_wide_strerror( errno );		if( error_string == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to open file: %ls.\n", filename );		}		else		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to open file: %ls with error: %ls.\n", filename, error_string );			libewf_common_free( error_string );		}		return( -1 );	}	count = libewf_common_read( file_descriptor, signature, 8 );	if( libewf_common_close( file_descriptor ) != 0 )	{		error_string = libewf_common_strerror( errno );		if( error_string == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to close file: %ls.\n", filename );		}		else		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to close file: %ls with error: %ls.\n", filename, error_string );			libewf_common_free( error_string );		}		return( -1 );	}	if( count <= -1 )	{		LIBEWF_WARNING_PRINT( "libewf_check_file_signature: error reading signature from file: %ls.\n", filename );		return( -1 );	}	else if( count != 8 )	{		LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to read signature from file: %ls.\n", filename );		return( -1 );	}	return( ewf_file_header_check_signature( signature ) );}#else/* Detects if a file is an EWF file (check for the EWF file signature) * Returns 1 if true, 0 if not, or -1 on error */int8_t libewf_check_file_signature( const char *filename ){	uint8_t signature[ 8 ];	char *error_string  = NULL;	ssize_t count       = 0;	int file_descriptor = 0;	if( filename == NULL )	{		LIBEWF_WARNING_PRINT( "libewf_check_file_signature: invalid filename.\n" );		return( -1 );	}	file_descriptor = libewf_common_open( filename, LIBEWF_OPEN_READ );	if( file_descriptor < 0 )	{		error_string = libewf_common_strerror( errno );		if( error_string == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to open file: %s.\n", filename );		}		else		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to open file: %s with error: %s.\n", filename, error_string );			libewf_common_free( error_string );		}		return( -1 );	}	count = libewf_common_read( file_descriptor, signature, 8 );	if( libewf_common_close( file_descriptor ) != 0 )	{		error_string = libewf_common_strerror( errno );		if( error_string == NULL )		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to close file: %s.\n", filename );		}		else		{			LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to close file: %s with error: %s.\n", filename, error_string );			libewf_common_free( error_string );		}		return( -1 );	}	if( count <= -1 )	{		LIBEWF_WARNING_PRINT( "libewf_check_file_signature: error reading signature from file: %s.\n", filename );		return( -1 );	}	else if( count != 8 )	{		LIBEWF_WARNING_PRINT( "libewf_check_file_signature: unable to read signature from file: %s.\n", filename );		return( -1 );	}	return( ewf_file_header_check_signature( signature ) );}#endif#if defined( HAVE_WIDE_CHARACTER_TYPE ) && defined( HAVE_WIDE_CHARACTER_SUPPORT_FUNCTIONS )/* 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( wchar_t * const filenames[], uint16_t file_amount, uint8_t flags ){	EWF_FILE_HEADER file_header;	LIBEWF_INTERNAL_HANDLE *internal_handle = NULL;	wchar_t *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		 */		handle = libewf_internal_handle_alloc( ( file_amount + 1 ), flags );		if( 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( handle );			return( NULL );		}		for( iterator = 0; iterator < file_amount; iterator++ )		{			file_descriptor = libewf_common_wide_open( filenames[ iterator ], flags );			if( file_descriptor == -1 )			{				error_string = libewf_common_wide_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( 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( 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( 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( 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_wide_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( 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( handle );				return( NULL );			}		}		if( libewf_read_build_index( handle ) != 1 )		{			LIBEWF_WARNING_PRINT( "libewf_open: unable to create index.\n" );			libewf_internal_handle_free( handle );			return( NULL );		}	}	else if( flags == LIBEWF_OPEN_WRITE )	{		/* Allocate 2 entries		 * entry [ 0 ] is used for the base filename		 */		handle = libewf_internal_handle_alloc( 1, flags );		if( 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( handle );			return( NULL );		}		if( libewf_segment_table_set_wide_filename( internal_handle->segment_table, 0, filenames[ iterator ], libewf_common_wide_string_length( filenames[ iterator ] ) ) != 1 )		{			LIBEWF_WARNING_PRINT( "libewf_open: unable to set filename in segment table.\n" );			libewf_internal_handle_free( 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( 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 *) handle );}#else/* 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 );

⌨️ 快捷键说明

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