/* Detects if a file is an EWF file * Returns 1 if true, 0 otherwise */ uint8_t libewf_check_file_signature( const char *filename ) { uint8_t signature[ 8 ]; int file_descriptor; if( filename == NULL ) { return( 0 ); } file_descriptor = open( filename, O_RDONLY ); if( file_descriptor < 0 ) { LIBEWF_FATAL_PRINT( "libewf_check_file_signature: unable to open file: %s: %m", filename ); } if( read( file_descriptor, signature, 8 ) != 8 ) { LIBEWF_FATAL_PRINT( "libewf_check_file_signature: unable to read signature from file: %s: %m", filename ); } close( file_descriptor ); return( ewf_file_header_check_signature( signature ) ); }
/* 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 ) ); }
/* 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 ); }