/* Opens a file for reading * Returns 1 if successful or -1 on error */ int libmsiecf_file_open_read( libmsiecf_internal_file_t *internal_file, libbfio_handle_t *file_io_handle, libcerror_error_t **error ) { static char *function = "libmsiecf_file_open_read"; uint32_t hash_table_offset = 0; if( internal_file == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid file.", function ); return( -1 ); } if( internal_file->io_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid file - missing IO handle.", function ); return( -1 ); } if( internal_file->directory_array != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid file - directory array already set.", function ); return( -1 ); } if( internal_file->item_array != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid file - item array already set.", function ); return( -1 ); } if( internal_file->recovered_item_array != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid file - recovered item array already set.", function ); return( -1 ); } if( internal_file->unallocated_block_list != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid file - unallocated block list already set.", function ); return( -1 ); } if( libcdata_array_initialize( &( internal_file->directory_array ), 0, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create directory array.", function ); goto on_error; } if( libcdata_array_initialize( &( internal_file->item_array ), 0, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create item array.", function ); goto on_error; } if( libcdata_array_initialize( &( internal_file->recovered_item_array ), 0, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create recovered item array.", function ); goto on_error; } if( libcdata_range_list_initialize( &( internal_file->unallocated_block_list ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create unallocated data block list.", function ); goto on_error; } #if defined( HAVE_DEBUG_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "Reading file header:\n" ); } #endif if( libmsiecf_io_handle_read_file_header( internal_file->io_handle, file_io_handle, &hash_table_offset, internal_file->directory_array, internal_file->unallocated_block_list, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: unable to read file header.", function ); goto on_error; } #if defined( HAVE_DEBUG_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "Reading hash table:\n" ); } #endif if( libmsiecf_io_handle_read_hash_table( internal_file->item_array, internal_file->io_handle, file_io_handle, hash_table_offset, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: unable to read hash table.", function ); goto on_error; } #if defined( HAVE_DEBUG_OUTPUT ) if( libcnotify_verbose != 0 ) { libcnotify_printf( "Scanning for records:\n" ); } #endif if( libmsiecf_io_handle_read_record_scan( internal_file->item_array, internal_file->recovered_item_array, internal_file->io_handle, file_io_handle, hash_table_offset, internal_file->unallocated_block_list, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_READ_FAILED, "%s: unable to perform record scan.", function ); goto on_error; } return( 1 ); on_error: if( internal_file->unallocated_block_list != NULL ) { libcdata_range_list_free( &( internal_file->unallocated_block_list ), NULL, NULL ); } if( internal_file->recovered_item_array != NULL ) { libcdata_array_free( &( internal_file->recovered_item_array ), (int (*)(intptr_t **, libcerror_error_t **)) &libmsiecf_item_descriptor_free, NULL ); } if( internal_file->item_array != NULL ) { libcdata_array_free( &( internal_file->item_array ), (int (*)(intptr_t **, libcerror_error_t **)) &libmsiecf_item_descriptor_free, NULL ); } if( internal_file->directory_array != NULL ) { libcdata_array_free( &( internal_file->directory_array ), (int (*)(intptr_t **, libcerror_error_t **)) &libmsiecf_directory_descriptor_free, NULL ); } return( -1 ); }
/* Tests the libpff_allocation_table_read_data function * Returns 1 if successful or 0 if not */ int pff_test_allocation_table_read_data( void ) { libcdata_range_list_t *unallocated_block_list = NULL; libcerror_error_t *error = NULL; int result = 0; /* Initialize test */ result = libcdata_range_list_initialize( &unallocated_block_list, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "unallocated_block_list", unallocated_block_list ); PFF_TEST_ASSERT_IS_NULL( "error", error ); /* Test regular cases */ result = libpff_allocation_table_read_data( unallocated_block_list, pff_test_allocation_table_data_32bit, 512, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NULL( "error", error ); result = libpff_allocation_table_read_data( unallocated_block_list, pff_test_allocation_table_data_64bit, 512, LIBPFF_FILE_TYPE_64BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NULL( "error", error ); result = libpff_allocation_table_read_data( unallocated_block_list, pff_test_allocation_table_data_64bit_4k_page, 4096, LIBPFF_FILE_TYPE_64BIT_4K_PAGE, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NULL( "error", error ); /* Test error cases */ result = libpff_allocation_table_read_data( NULL, pff_test_allocation_table_data_32bit, 512, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_data( unallocated_block_list, NULL, 512, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_data( unallocated_block_list, pff_test_allocation_table_data_32bit, (size_t) SSIZE_MAX + 1, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_data( unallocated_block_list, pff_test_allocation_table_data_32bit, 512, 0xff, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_data( unallocated_block_list, pff_test_allocation_table_data_32bit, 0, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); /* Clean up */ result = libcdata_range_list_free( &unallocated_block_list, NULL, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NULL( "unallocated_block_list", unallocated_block_list ); PFF_TEST_ASSERT_IS_NULL( "error", error ); return( 1 ); on_error: if( error != NULL ) { libcerror_error_free( &error ); } if( unallocated_block_list != NULL ) { libcdata_range_list_free( &unallocated_block_list, NULL, NULL ); } return( 0 ); }
/* Tests the libpff_allocation_table_read_file_io_handle function * Returns 1 if successful or 0 if not */ int pff_test_allocation_table_read_file_io_handle( void ) { libbfio_handle_t *file_io_handle = NULL; libcdata_range_list_t *unallocated_block_list = NULL; libcerror_error_t *error = NULL; int result = 0; /* Initialize test */ result = libcdata_range_list_initialize( &unallocated_block_list, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "unallocated_block_list", unallocated_block_list ); PFF_TEST_ASSERT_IS_NULL( "error", error ); /* Initialize file IO handle */ result = pff_test_open_file_io_handle( &file_io_handle, pff_test_allocation_table_data_32bit, 512, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "file_io_handle", file_io_handle ); PFF_TEST_ASSERT_IS_NULL( "error", error ); /* Test regular cases */ result = libpff_allocation_table_read_file_io_handle( unallocated_block_list, file_io_handle, 0, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NULL( "error", error ); /* Test error cases */ result = libpff_allocation_table_read_file_io_handle( NULL, file_io_handle, 0, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_file_io_handle( unallocated_block_list, NULL, 0, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_file_io_handle( unallocated_block_list, file_io_handle, -1, LIBPFF_FILE_TYPE_32BIT, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); result = libpff_allocation_table_read_file_io_handle( unallocated_block_list, file_io_handle, 0, 0xff, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); #if defined( HAVE_PFF_TEST_MEMORY ) /* Test libpff_allocation_table_read_file_io_handle with malloc failing */ pff_test_malloc_attempts_before_fail = 0; result = libpff_allocation_table_read_file_io_handle( unallocated_block_list, file_io_handle, 0, LIBPFF_FILE_TYPE_32BIT, &error ); if( pff_test_malloc_attempts_before_fail != -1 ) { pff_test_malloc_attempts_before_fail = -1; } else { PFF_TEST_ASSERT_EQUAL_INT( "result", result, -1 ); PFF_TEST_ASSERT_IS_NOT_NULL( "error", error ); libcerror_error_free( &error ); } #endif /* defined( HAVE_PFF_TEST_MEMORY ) */ /* Clean up file IO handle */ result = pff_test_close_file_io_handle( &file_io_handle, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 0 ); PFF_TEST_ASSERT_IS_NULL( "error", error ); /* Clean up */ result = libcdata_range_list_free( &unallocated_block_list, NULL, &error ); PFF_TEST_ASSERT_EQUAL_INT( "result", result, 1 ); PFF_TEST_ASSERT_IS_NULL( "unallocated_block_list", unallocated_block_list ); PFF_TEST_ASSERT_IS_NULL( "error", error ); return( 1 ); on_error: if( error != NULL ) { libcerror_error_free( &error ); } if( file_io_handle != NULL ) { libbfio_handle_free( &file_io_handle, NULL ); } if( unallocated_block_list != NULL ) { libcdata_range_list_free( &unallocated_block_list, NULL, NULL ); } return( 0 ); }
/* Creates a chunk table * Make sure the value chunk_table is referencing, is set to NULL * Returns 1 if successful or -1 on error */ int libewf_chunk_table_initialize( libewf_chunk_table_t **chunk_table, libewf_io_handle_t *io_handle, libcerror_error_t **error ) { static char *function = "libewf_chunk_table_initialize"; if( chunk_table == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid chunk table.", function ); return( -1 ); } if( *chunk_table != NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, "%s: invalid chunk table value already set.", function ); return( -1 ); } if( io_handle == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid IO handle.", function ); return( -1 ); } *chunk_table = memory_allocate_structure( libewf_chunk_table_t ); if( *chunk_table == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create chunk table.", function ); goto on_error; } if( memory_set( *chunk_table, 0, sizeof( libewf_chunk_table_t ) ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear chunk table.", function ); memory_free( *chunk_table ); *chunk_table = NULL; return( -1 ); } if( libcdata_range_list_initialize( &( ( *chunk_table )->checksum_errors ), error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED, "%s: unable to create checksum errors range list.", function ); goto on_error; } ( *chunk_table )->io_handle = io_handle; return( 1 ); on_error: if( *chunk_table != NULL ) { memory_free( *chunk_table ); *chunk_table = NULL; } return( -1 ); }