/* Opens the info handle * Returns 1 if successful or -1 on error */ int info_handle_open_input( info_handle_t *info_handle, const libcstring_system_character_t *filename, libcerror_error_t **error ) { static char *function = "info_handle_open_input"; if( info_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid info handle.", function ); return( -1 ); } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libvhdi_file_open_wide( info_handle->input_file, filename, LIBVHDI_OPEN_READ, error ) != 1 ) #else if( libvhdi_file_open( info_handle->input_file, filename, LIBVHDI_OPEN_READ, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open input file.", function ); return( -1 ); } return( 1 ); }
/* Opens the parent input file * Returns 1 if successful, 0 if no parent or -1 on error */ int mount_handle_open_input_parent_file( mount_handle_t *mount_handle, libvhdi_file_t *input_file, libcerror_error_t **error ) { uint8_t guid[ 16 ]; libcstring_system_character_t *parent_basename_end = NULL; libcstring_system_character_t *parent_filename = NULL; libcstring_system_character_t *parent_path = NULL; libvhdi_file_t *parent_input_file = NULL; static char *function = "mount_handle_open_input_parent_file"; size_t parent_basename_length = 0; size_t parent_filename_size = 0; size_t parent_path_size = 0; int entry_index = 0; int result = 0; if( mount_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mount handle.", function ); return( -1 ); } result = libvhdi_file_get_parent_identifier( input_file, guid, 16, error ); if( result == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve parent content identifier.", function ); goto on_error; } else if( result != 1 ) { return( 0 ); } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) result = libvhdi_file_get_utf16_parent_filename_size( input_file, &parent_filename_size, error ); #else result = libvhdi_file_get_utf8_parent_filename_size( input_file, &parent_filename_size, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve parent filename size.", function ); goto on_error; } if( parent_filename_size == 0 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: missing parent filename.", function ); goto on_error; } if( ( parent_filename_size > (size_t) SSIZE_MAX ) || ( ( sizeof( libcstring_system_character_t ) * parent_filename_size ) > (size_t) SSIZE_MAX ) ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM, "%s: invalid parent filename size value exceeds maximum.", function ); goto on_error; } parent_filename = libcstring_system_string_allocate( parent_filename_size ); if( parent_filename == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create parent filename string.", function ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) result = libvhdi_file_get_utf16_parent_filename( input_file, (uint16_t *) parent_filename, parent_filename_size, error ); #else result = libvhdi_file_get_utf8_parent_filename( input_file, (uint8_t *) parent_filename, parent_filename_size, error ); #endif if( result != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve parent filename.", function ); goto on_error; } parent_basename_end = libcstring_system_string_search_character_reverse( parent_filename, (libcstring_system_character_t) '\\', parent_filename_size ); if( parent_basename_end != NULL ) { parent_basename_length = (size_t) ( parent_basename_end - parent_filename ) + 1; } if( mount_handle->basename == NULL ) { parent_path = &( parent_filename[ parent_basename_length ] ); parent_path_size = parent_filename_size - ( parent_basename_length + 1 ); } else { #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libcpath_path_join_wide( &parent_path, &parent_path_size, mount_handle->basename, mount_handle->basename_size - 1, &( parent_filename[ parent_basename_length ] ), parent_filename_size - ( parent_basename_length + 1 ), error ) != 1 ) #else if( libcpath_path_join( &parent_path, &parent_path_size, mount_handle->basename, mount_handle->basename_size - 1, &( parent_filename[ parent_basename_length ] ), parent_filename_size - ( parent_basename_length + 1 ), error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create parent path.", function ); goto on_error; } } if( libvhdi_file_initialize( &parent_input_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize parent input file.", function ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libvhdi_file_open_wide( parent_input_file, parent_path, LIBVHDI_OPEN_READ, error ) != 1 ) #else if( libvhdi_file_open( parent_input_file, parent_path, LIBVHDI_OPEN_READ, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open parent input file: %" PRIs_LIBCSTRING_SYSTEM ".", function, parent_path ); goto on_error; } if( mount_handle_open_input_parent_file( mount_handle, parent_input_file, error ) == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open parent input file.", function ); return( -1 ); } if( libvhdi_file_set_parent_file( input_file, parent_input_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set parent input file.", function ); goto on_error; } if( libcdata_array_append_entry( mount_handle->input_files_array, &entry_index, (intptr_t *) parent_input_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, "%s: unable to append parent input file to array.", function ); goto on_error; } if( parent_path != NULL ) { if( mount_handle->basename != NULL ) { memory_free( parent_path ); } parent_path = NULL; } if( parent_filename != NULL ) { memory_free( parent_filename ); parent_filename = NULL; } return( 1 ); on_error: if( parent_input_file != NULL ) { libvhdi_file_free( &parent_input_file, NULL ); } if( ( parent_path != NULL ) && ( mount_handle->basename != NULL ) ) { memory_free( parent_path ); } if( parent_filename != NULL ) { memory_free( parent_filename ); } return( -1 ); }
/* Opens the input of the mount handle * Returns 1 if successful, 0 if the keys could not be read or -1 on error */ int mount_handle_open_input( mount_handle_t *mount_handle, const libcstring_system_character_t *filename, libcerror_error_t **error ) { libcstring_system_character_t *basename_end = NULL; libvhdi_file_t *input_file = NULL; static char *function = "mount_handle_open_input"; size_t basename_length = 0; size_t filename_length = 0; int entry_index = 0; if( mount_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid mount handle.", function ); return( -1 ); } if( filename == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid filename.", function ); return( -1 ); } filename_length = libcstring_system_string_length( filename ); basename_end = libcstring_system_string_search_character_reverse( filename, (libcstring_system_character_t) LIBCPATH_SEPARATOR, filename_length + 1 ); if( basename_end != NULL ) { basename_length = (size_t) ( basename_end - filename ) + 1; } if( basename_length > 0 ) { if( mount_handle_set_basename( mount_handle, filename, basename_length, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_SET_FAILED, "%s: unable to set basename.", function ); goto on_error; } } if( libvhdi_file_initialize( &input_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to initialize input file.", function ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libvhdi_file_open_wide( input_file, filename, LIBVHDI_OPEN_READ, error ) != 1 ) #else if( libvhdi_file_open( input_file, filename, LIBVHDI_OPEN_READ, error ) != 1 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open input file.", function ); goto on_error; } if( mount_handle_open_input_parent_file( mount_handle, input_file, error ) == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_OPEN_FAILED, "%s: unable to open parent input file.", function ); goto on_error; } if( libcdata_array_append_entry( mount_handle->input_files_array, &entry_index, (intptr_t *) input_file, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_APPEND_FAILED, "%s: unable to append input file to array.", function ); goto on_error; } return( 1 ); on_error: if( input_file != NULL ) { libvhdi_file_free( &input_file, NULL ); } libcdata_array_empty( mount_handle->input_files_array, (int (*)(intptr_t **, libcerror_error_t **)) &libvhdi_file_free, NULL ); return( -1 ); }
/* Tests single open and close of a file * Returns 1 if successful, 0 if not or -1 on error */ int vhdi_test_single_open_close_file( libcstring_system_character_t *filename, int access_flags, int expected_result ) { libcerror_error_t *error = NULL; libvhdi_file_t *file = NULL; static char *function = "vhdi_test_single_open_close_file"; char *access_string = NULL; int result = 0; if( access_flags == LIBVHDI_OPEN_READ ) { access_string = "read"; } else if( access_flags == LIBVHDI_OPEN_WRITE ) { access_string = "write"; } else { access_string = "UNKNOWN"; } fprintf( stdout, "Testing single open close of: " ); if( filename != NULL ) { fprintf( stdout, "%" PRIs_LIBCSTRING_SYSTEM "", filename ); } else { fprintf( stdout, "NULL" ); } fprintf( stdout, " with access: %s\t", access_string ); if( libvhdi_file_initialize( &file, &error ) != 1 ) { libcerror_error_set( &error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create file.", function ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) result = libvhdi_file_open_wide( file, filename, access_flags, &error ); #else result = libvhdi_file_open( file, filename, access_flags, &error ); #endif if( result == 1 ) { if( libvhdi_file_close( file, &error ) != 0 ) { libcerror_error_set( &error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_CLOSE_FAILED, "%s: unable to close file.", function ); goto on_error; } } if( libvhdi_file_free( &file, &error ) != 1 ) { libcerror_error_set( &error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED, "%s: unable to free file.", function ); goto on_error; } result = ( expected_result == result ); if( result == 1 ) { fprintf( stdout, "(PASS)" ); } else { fprintf( stdout, "(FAIL)" ); } fprintf( stdout, "\n" ); if( error != NULL ) { libcerror_error_backtrace_fprint( error, stderr ); libcerror_error_free( &error ); } return( result ); on_error: if( error != NULL ) { libcerror_error_backtrace_fprint( error, stderr ); libcerror_error_free( &error ); } if( file != NULL ) { libvhdi_file_free( &file, NULL); } return( -1 ); }
/* Tests reading a file * Returns 1 if successful, 0 if not or -1 on error */ int vhdi_test_read( libcstring_system_character_t *source, libcerror_error_t **error ) { libvhdi_file_t *file = NULL; libvhdi_file_t *parent_file = NULL; size64_t media_size = 0; int result = 0; if( libvhdi_file_initialize( &file, error ) != 1 ) { fprintf( stderr, "Unable to create file.\n" ); goto on_error; } #if defined( LIBCSTRING_HAVE_WIDE_SYSTEM_CHARACTER ) if( libvhdi_file_open_wide( file, source, LIBVHDI_OPEN_READ, error ) != 1 ) #else if( libvhdi_file_open( file, source, LIBVHDI_OPEN_READ, error ) != 1 ) #endif { fprintf( stderr, "Unable to open file.\n" ); goto on_error; } if( vhdi_test_read_open_parent( source, file, &parent_file, error ) == -1 ) { fprintf( stderr, "Unable to open parent file.\n" ); goto on_error; } if( libvhdi_file_get_media_size( file, &media_size, error ) != 1 ) { fprintf( stderr, "Unable to retrieve media size.\n" ); goto on_error; } fprintf( stdout, "Media size: %" PRIu64 " bytes\n", media_size ); result = vhdi_test_read_from_file( file, media_size ); if( result == -1 ) { fprintf( stderr, "Unable to read from file.\n" ); goto on_error; } if( libvhdi_file_close( file, error ) != 0 ) { fprintf( stderr, "Unable to close file.\n" ); goto on_error; } if( libvhdi_file_free( &file, error ) != 1 ) { fprintf( stderr, "Unable to free file.\n" ); goto on_error; } if( parent_file != NULL ) { if( libvhdi_file_close( parent_file, error ) != 0 ) { fprintf( stderr, "Unable to close parent file.\n" ); goto on_error; } if( libvhdi_file_free( &parent_file, error ) != 1 ) { fprintf( stderr, "Unable to free parent file.\n" ); goto on_error; } } return( result ); on_error: if( file != NULL ) { libvhdi_file_close( file, NULL ); libvhdi_file_free( &file, NULL ); } if( parent_file != NULL ) { libvhdi_file_close( parent_file, NULL ); libvhdi_file_free( &parent_file, NULL ); } return( -1 ); }